It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                7年前关闭。
            
                    
我对按位运算有一些疑问,我们将不胜感激。

1.)

i = 0x000a;
printf( "2: %x %x %x\n", i, i << 1, i << 2 );
i = 0x0010;
printf( "3: %x %x %x\n", i, i >> 1, i >> 2 );


2)

unsigned int i = 1;
printf( "1: %x %x %x\n", i, ~i, ~~i );
printf( "2: %x %x \n", i, ( 0x0100 & ( 1 << 8 ) ) >> 8 );
printf( "3: %x %x \n", i, 0x0100 ^ ( 1 << 8 ) );
printf( "4: %x %x \n", i, 0x0100 | ( 1 << 4 ) );


我对这些问题有答案,但不了解其工作原理。有人可以逐步解释吗?

提前致谢

最佳答案

x<<SOME_VALUE将x SOME_VALUE次的所有位向左移动。因此,当0x000a=1010(2)向左移动1位时,将变为101000x0014。再次向左移动该数字将其乘以2或产生0x0028。右移(>>)的逻辑类似,在此不再赘述。我认为这可以解释您在评论中的要求。

~否定整数的所有位,因此所有0位变为1,反之亦然。因此~0x0001(1(2))fffffffe11111111111111111111111111111110(2)(在32位计算机上)。双重否定产生输入。

&是按位“和”运算符,并且是二进制运算符-它需要两个操作数,并且仅当两个数字在该位置均具有1时,结果数字在给定位置才具有1。

^或xor是互斥或运算符,再次是二进制。在这里,当且仅当其中一个操作数在给定位置具有1时,您才会有1。

最后是“ |”是逻辑运算符,还是二进制运算符,当且仅当其操作数中的至少一个在该位置具有1时,才在给定位置具有1。

关于c - 按位移位步骤,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13816015/

10-11 23:21