[英]How to manage unions and bit-fields
I have been trying to create a program that adds two positive fixed point numbers whose whole part is 5 bits and its decimal part is 3 bits but I have not succeeded, I know that it can be achieved with the handling of bit fields and unions but I have not found the goal.我一直在尝试创建一个程序,将两个正定点数相加,其整个部分为 5 位,小数部分为 3 位,但我没有成功,我知道可以通过处理位域和联合来实现,但是我还没有找到目标。 I would appreciate if someone could help me with this.
如果有人可以帮助我,我将不胜感激。
Let a
be an integer object that represents a number a using a fixed-point representation with three fraction bits.让
a
是一个整数对象,它使用具有三个小数位的定点表示来表示数字a 。 Then a
= 8• a , or, conversely, a = a
/8.那么
a
= 8• a ,或者反过来, a = a
/8。
Similarly, let b
be an integer object that represents a number b with the same representation.类似地,设
b
是一个整数对象,表示具有相同表示的数字b 。
Then, since a = a
/8 and b = b
/8, we have a + b = a
/8 + b
/8 = a+b
/8.然后,由于a =
a
/8 和b = b
/8,我们有a + b = a
/8 + b
/8 = a+b
/8。
Therefore, the representation of the sum of the number represented by a
and the number represented by b
can be computed with a+b
.因此,数目的总和的表示所表示由
a
和由表示的数目b
可以与计算a+b
。
The bit fields in C
programming can be defined using struct
as shown below for task you want to perform. C
编程中的位域可以使用struct
定义,如下所示,用于您要执行的任务。
struct decimalPoint
{
unsigned int wholePart:5;
unsigned int decimalPart:3;
};
Now since the way to definition of your data is ready you can perform addition of this data and store the result back to the same structure.现在,由于定义数据的方式已准备就绪,您可以执行此数据的添加并将结果存储回相同的结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.