移位操作右操作数为负数或右操作数大于或等于左操作数的位数,结果为为定义
来源:互联网 发布:sql删除全部数据 编辑:程序博客网 时间:2024/05/14 02:12
其中1<<63,1<<32这两行有警告:“<<”: Shift 计数为负或过大,其行为未定义
输出:
8
0
0
0
9223372036854775808
-0
为什么会这样呢?查了MSDN文档,
个人理解如下(如有不妥,请指正)
1<<63,编译器用个32位的临时变量,也许是int类型的,移位时超过了31位(结果为为定义的),在vs2008上试验全变为0,因1已被移出去。故1<<63,1<<32为0
而a为64位,a<<63,1移到最高位
还有在double下(最高位为符号位),
0x8000000000000000 为 -0
0x0000000000000000 为 0
相关Msdn文档如下:
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
The bitwise shift operators are:
Right shift (>>)
Left shift (<<)
These binary operators have left-to-right associativity.
Both operands of the shift operators must be of integral types. Integral promotions are performed according to the rules described in Integral Promotions. The type of the result is the same as the type of the left operand. The value of a right-shift expression e1 >> e2 is e1 / 2e2, and the value of a left-shift expression e1 << e2 is e1 * 2e2.
The results are undefined if the right operand of a shift expression is negative or if the right operand is greater than or equal to the number of bits in the (promoted) left operand.
The left shift operator causes the bit pattern in the first operand to be shifted left the number of bits specified by the second operand. Bits vacated by the shift operation are zero-filled. This is a logical shift, as opposed to a shift-and-rotate operation.
The right shift operator causes the bit pattern in the first operand to be shifted right the number of bits specified by the second operand. Bits vacated by the shift operation are zero-filled for unsigned quantities. For signed quantities, the sign bit is propagated into the vacated bit positions. The shift is a logical shift if the left operand is an unsigned quantity; otherwise, it is an arithmetic shift.
Microsoft Specific
The result of a right shift of a signed negative quantity is implementation dependent. Although Microsoft C++ propagates the most-significant bit to fill vacated bit positions, there is no guarantee that other implementations will do likewise.
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
- 移位操作右操作数为负数或右操作数大于或等于左操作数的位数,结果为为定义
- 关于 C++ 左操作数和右操作数
- 对于左操作数必须为左值的问题
- 整型数组左移或右移位简单操作
- 如何理解左操作数必须为左值
- 如何理解左操作数必须为左值
- error C2679: 二进制“”类型的右操作数的运算符(或没有可接受的转换)
- 移位运算符操作数是负数的处理方式
- 由“左操作数必须为左值”引起的反思
- MOV操作数为立即数时的个例说明
- Java中负数的移位操作——右移
- 移位操作---左移和右移
- 重学C++Primer笔记2---输入/输出 操作符 左操作数 右操作数 深入理解
- 如何使用位操作得到大于N且为2的次方的最小的数
- 二进制“<<”: 没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换)
- 二进制“<<”: 没有找到接受“std::string”类型的右操作数的运算符(或没有可接受
- java的移位操作详解,左移和右移
- java的移位操作详解,左移和右移
- 杂想
- 进程与线程
- 对象与类型
- 25匹马,跑道一次可以有5匹马比赛,至少需要多少次比赛,可以计算出前5名
- 宏的一些副作用
- 移位操作右操作数为负数或右操作数大于或等于左操作数的位数,结果为为定义
- extjs 3.0 radioGroup的问题解决办法
- hypertable学习中段
- 获取时间日期
- java解析xml
- 努力
- MTK--世界时钟
- 分页算法
- 将blob类型转换为文件