简体   繁体   English

为什么x和y的值为1

[英]Why the value of x and y is 1

union test{  
    unsigned int x: 3; 
    unsigned int y: 3; 
    int z;}; 
int main(){      
 union test t;   
 t.x = 5;  
 t.y = 4;   
 t.z = 1; 
 printf("t.x = %d, t.y = %d, t.z = %d", t.x, t.y, t.z);  
 return 0;
}

I can not understand why the value of x and y is 1, any help is appreciated thanks . 我不明白为什么x和y的值为1,感谢您的帮助。

EDIT: thanks all of you for the inputs ,you have asked for outputs I don't have a compiler at present anyway way i compiled the code online and these are the outputs 编辑:谢谢大家的输入,您已经要求输出我现在还没有编译器,我以在线方式编译了代码,这些是输出

for z= 1  :      t.x = 1, t.y = 1, t.z = 1

for z= 2  :      t.x = 2, t.y = 2, t.z = 2

for z= 10 :      t.x = 2, t.y = 2, t.z = 10

for z= 30 :      t.x = 6, t.y = 6, t.z = 30 

why these strange outputs ?? 为什么这些奇怪的输出?

Compiler allocate memory for largest union member. 编译器为最大的union成员分配内存。 Memory for z will be allocated in this case. 在这种情况下,将分配z内存。 Since last assignment make z to have value 1 , only this value will be there in memory overriding the previous values. 由于最后一次赋值使z具有值1 ,因此内存中仅此值将覆盖先前的值。 So, the last 3 bits stored in allocated memory is 001 . 因此,存储在分配的内存中的最后3位是001 Accessing other 3 bit members will give result 1 . 访问其他3位成员将得出结果1

For z equals to 2 , 10 , and 30 , last 3 bits are 010 , 010 and 110 respectively which are equal to 2 , 2 and 6 respectively in decimal. 对于z等于210 ,和30 ,最后3位是010010110分别其是等于226分别以十进制。

It's a union, that's the way unions work. 这是一个工会,这就是工会的工作方式。 All of the member variables (x,y,z) are at the same address in memory, so if you change one, you change them all. 所有成员变量(x,y,z)都在内存中的同一地址,因此,如果更改一个,则全部更改。 Technically you should only read the last one that you wrote. 从技术上讲,您应该只阅读您写的最后一篇。 Writing to z and then reading from x or y is unspecified behavior. 写入z ,然后从xy读取是未指定的行为。

From the C-11 draft specification §6.2.6.1 根据C-11规范草案§6.2.6.1

When a value is stored in a member of an object of union type, the bytes of the object representation that do not correspond to that member but do correspond to other members take unspecified values. 当值存储在联合类型的对象的成员中时,与该成员不对应但与其他成员对应的对象表示形式的字节采用未指定的值。


In response to the edit: 响应编辑:
The lower three bits of 10 decimal are 010 binary which is 2 decimal. 10个十进制的低三位是010二进制,即2个十进制。
The lower three bits of 30 decimal are 110 binary which is 6 decimal. 30十进制的低三位是110二进制,即6十进制。

In Union, we can use one member at a time. 在Union中,我们一次只能使用一个成员。 So at last you assign the value as 1.Refer here. 因此,最后将值分配为1。请在此处参考。

Union will take the memory which having the highest size in that member. 联合会占用该成员中具有最大大小的内存。 So from that memory all the members have to work in that. 因此,从该内存中,所有成员都必须在其中工作。 At last z will access the memory then make the value as one. 最后, z将访问内存,然后将该值设为1。

All the members will use the same memory so first two members got the value as one. 所有成员将使用相同的内存,因此前两个成员将值设为1。

You can verify like this. 您可以像这样验证。 Make a printf after assigning every value. 分配每个值后进行printf

t.x=5;
printf("t.x = %d, t.y = %d, t.z = %d", t.x, t.y, t.z);
t.y=4
printf("t.x = %d, t.y = %d, t.z = %d", t.x, t.y, t.z);
t.z=1;
printf("t.x = %d, t.y = %d, t.z = %d", t.x, t.y, t.z);

Nothing wrong in it. 没错。

Last 3 bits of z are shared with x and y. z的最后3位与x和y共享。

for z= 30 tx = 6, ty = 6, tz = 30 对于z = 30 tx = 6,ty = 6,tz = 30

z= 0x1E = 00011110. z = 0x1E = 00011110。

tx = 110 = 6 tx = 110 = 6

ty = 110 = 6 ty = 110 = 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM