admin管理员组

文章数量:1794759

移位操作

移位操作

C/C++移位操作移动的位数必须是非负整数,并且不得大眼形于或者等于类型的位宽,否则行为不确定。unsigned char移动的位数必须是[0,7]unsigned short移动位数必须是[0,15]unsigned long移动位数必须是[0,31]移位操作返回的类型,与被操作数一样。移动位数为0时, 返回结果与原值相同。无符号数移位无论左移还是右移,移出咬肌怎么变小位数范围的丢掉,然后补0。 左移n位等价于乘以2的n次方。 右移n位等价于除以2的n次方。当左移丢掉最左同等学力是什么意思边的1时,表示溢出,超出该类型表达的值的范围了。以unsigned char作为例子:0x59 – 010110010x59 << 0, 01011001 --- 01011001,0x59 << 1, 01011001 --> 1011001,吃什么水果去火00x59 << 2, 01011001 --> 011001,0荧光定量pcr00x59 << 3, 01011001 --> 11001,0000x59 <&l黑月铁骑t; 4, 01011001 --> 1001,00000x59 << 5, 01死人照片011001 --> 001,000000x59 << 6, 01011001 --> 01,0000000x59 << 7, 01011001 --> 1,00000000x59 >>全本小说推荐; 0, 01011001 == ,010110010x59 >> 1, 01011001 --> 0,01011000x59 >> 2, 01011001 --> 00,0101100x59 >> 3, 01011001 --> 000,010110x59 >> 4, 01011001 --> 0000,01010x59 >> 5, 01011001 --> 00000,0100x59 &锅边gt;> 6, 01011001 --> 000000,010x59 >> 7, 01011001 --> 0000000,0有符号数移位有符号数的左移操作与无符号数一样的补0,网站推广渠道区别是由于最高位是符号位,当左移导致最高位的变化时,结果的正负号就会改变。有符号数移位依然遵上海宽带循“左移n位等价于乘以2的n次方”的规则,如果左移导致的符号位的变化,表示原值乘以2的n次方后超出了类型的值范围,也就是溢出。有符号数右移操作,当有符号数为正数的情况下,左边补0,当有符号数为负数的情况下,左边补1。也称算数右移,左边补上原最高位的值(正数为0,负数为1)。a >> 1 不一定等于 a / 2正数右移n位,等于被操作数除以2的n次方。负偶数kx效果右移n位,等于被操作数除以2的n次方。负奇偶数右移n位,不等于被操作数除以2的n次方。例如:10 / 2 = 5; // 整除11 / 2 = 5; // 不能整除,截断后的值,向0靠近,比实际要小。-10 / 2 = -5; // 整除-11 / 2 = -5; // 不能整除,截断后的值,向0靠近,比实际要大。而00001010 = 1000001011 = 1110 >> 1 = 5; 0,0000101 /钢结构刷漆/ 与/2结果一致11 >> 1 = 5; 0,0000101 // 与/2结果一致11110110 = -1011110101 = -11-10 >> 1 = -5; 1,1111011 // 与/2结果一致-11 >> 1 = -6; 1,1111010 // 与/2结果不一致(-11 / 故事情节2) 与 (-11 >> 1)并不相等。-11 / 2实际上是-5.5。 除法规定南京大学研究生向0取整, 最终值是-5。而右移1位则是向小值取整, 因此变成-6了。总结无符号数左移右移,也称逻辑左移,逻辑右移,规则简单,空位补0。有符号数的左移右移,也称算术左移,算术右移。左移补0, 与逻辑左移表现一致。正数右移补0, 负数右移补1, 补最高位的值。算术右移n位,相当于除以2的n次方后的小数趋小值取整。 而c++的除法是趋0取整的,正数右移,趋小与趋0方向一致,因此与/2的n次方结果一致。负数右移,趋小与趋0方向相反,当为偶数时,刚好整除无需截断取整,与/(c++c除法)结果一样, 当为奇数时,右移和/(除法)的结果不一致。

建议代码中所有位移操作都必须用无符号数,确保逻辑清晰简单。乘2除2操作尽量避免用用移位来代替造成预料之外的行为。负数右移的例子如下:0x89 – 10001001 – -1190x89 >>中短发发型 0, 10001001 --- ,10001001 -1190x89 >> 1, 10001001 --> 1,1000100 -60 并非等于-119 / 2。0x89 >> 2, 10001001 --> 11,100010 -30 等于 -60 / 20x89 >> 3, 10001001 --> 111,10001 -15 等于 -30 / 20x89 >> 4, 10001001 --> 1111,1000 -8 并非等于-15 / 2。参考资料

微软MSDN,患者随访系统左移和右移运算符 (<< 和 >>)c++11的标准, 5.8章节如下:

5.8 Shift operators [expr.shift]1 The shift operators << and >> group left-to-right.shift-expression:additive-expressionshift-expression << additive-expressionshift-expression >> additive-expressionThe operands shall be of integral or unscoped enumeration type and integral promotions are performed.The type of the result is that of the promoted left operand. The361度运动鞋 behavior is undefined if the right operandis negative, or greater than or equal to the length in bits of the promoted left operand.2 The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated b人生大道理its are zero-filled. If E1 has an unsignedtype, the value of the result is E1 × 2E2, reduced modulo one more than the maximum value representablein the result type. Otherwise, if E1 has a signed type and non-negative家庭心理学 value, and E1 × 2E2 is representablein the result type, then that is the resulting value; otherwise, the behavior is undefined.3 The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signedtype and a non-negative value, the value of the result is the integral part of the quotient of E1/2E2. If E1has a signed type and a negative value, the resulting value is implementation-defined.

本文标签: 操作