<tfoot id='4zoCV'></tfoot>
    <legend id='4zoCV'><style id='4zoCV'><dir id='4zoCV'><q id='4zoCV'></q></dir></style></legend>
    <i id='4zoCV'><tr id='4zoCV'><dt id='4zoCV'><q id='4zoCV'><span id='4zoCV'><b id='4zoCV'><form id='4zoCV'><ins id='4zoCV'></ins><ul id='4zoCV'></ul><sub id='4zoCV'></sub></form><legend id='4zoCV'></legend><bdo id='4zoCV'><pre id='4zoCV'><center id='4zoCV'></center></pre></bdo></b><th id='4zoCV'></th></span></q></dt></tr></i><div id='4zoCV'><tfoot id='4zoCV'></tfoot><dl id='4zoCV'><fieldset id='4zoCV'></fieldset></dl></div>

  • <small id='4zoCV'></small><noframes id='4zoCV'>

        <bdo id='4zoCV'></bdo><ul id='4zoCV'></ul>

        算术右移给出虚假结果?

        时间:2023-09-18
          <tbody id='9mgY3'></tbody>

        <i id='9mgY3'><tr id='9mgY3'><dt id='9mgY3'><q id='9mgY3'><span id='9mgY3'><b id='9mgY3'><form id='9mgY3'><ins id='9mgY3'></ins><ul id='9mgY3'></ul><sub id='9mgY3'></sub></form><legend id='9mgY3'></legend><bdo id='9mgY3'><pre id='9mgY3'><center id='9mgY3'></center></pre></bdo></b><th id='9mgY3'></th></span></q></dt></tr></i><div id='9mgY3'><tfoot id='9mgY3'></tfoot><dl id='9mgY3'><fieldset id='9mgY3'></fieldset></dl></div>

            <legend id='9mgY3'><style id='9mgY3'><dir id='9mgY3'><q id='9mgY3'></q></dir></style></legend>
              <bdo id='9mgY3'></bdo><ul id='9mgY3'></ul>
            • <small id='9mgY3'></small><noframes id='9mgY3'>

                <tfoot id='9mgY3'></tfoot>
                • 本文介绍了算术右移给出虚假结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我一定是疯了,但是我机器上的 gcc 4.7.3 给出了最荒谬的结果.这是我正在测试的确切代码:

                  I must be absolutely crazy here, but gcc 4.7.3 on my machine is giving the most absurd result. Here is the exact code that I'm testing:

                  #include <iostream>
                  
                  using namespace std;
                  
                  int main(){
                    unsigned int b = 100000;
                    cout << (b>>b) << endl;
                    b = b >> b;
                    cout << b << endl;
                    b >>= b;
                    cout << b << endl;
                    return 0;
                  }
                  

                  现在,任何自行右移的数字都应该导致 0 (n/(2^n) == 0整数除法n>1positive/unsigned),但不知何故这是我的输出:

                  Now, any number that's right shifted by itself should result in 0 (n/(2^n) == 0 with integer divide, n>1, and positive/unsigned), but somehow here is my output:

                  100000
                  100000
                  100000
                  

                  我疯了吗?可能会发生什么?

                  Am I crazy? What could possibly be going on?

                  推荐答案

                  在 C++ 中与在 C 中一样,移位仅限于移位值的大小(以位为单位).例如,如果 unsigned int 是 32 位,那么超过 31 位的移位是未定义的.

                  In C++ as in C, shifts are limited to the size (in bits) of the value shifted. For instance, if unsigned int is 32 bits, then a shift of more than 31 is undefined.

                  在实践中,一个常见的结果是使用移位量的最低5位,忽略高位;这是因为编译器生成了一条完全符合此要求的机器指令(例如 x86 上的 SHR).

                  In practice, a common result is that the 5 least-significant bits of the shift amount are used and the higher order bits are ignored; this is due to the compiler producing a machine instruction which does exactly that (eg SHR on x86).

                  在这种情况下,移位值是 100000(十进制),恰好是二进制的 11000011010100000 - 低 5 位为零.所以,你实际上得到了一个 0 的转变.但是你不应该依赖这个;从技术上讲,您看到的是未定义行为.

                  In this case, the shift value is 100000 (decimal) which happens to be 11000011010100000 in binary - the lower 5 bits are zero. So, you're effectively getting a shift by 0. You shouldn't rely on this however; technically, what you're seeing is undefined behaviour.

                  参考文献:

                  对于 C,N1570 6.5 节.7:

                  For C, N1570 section 6.5.7:

                  如果右操作数的值为负或大于或等于提升的左操作数的宽度,行为是未定义.

                  If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

                  对于 C++,N3690第 5.8 节[expr.shift]":

                  For C++, N3690 section 5.8 "[expr.shift]":

                  如果右操作数为负数或更大,则行为未定义大于或等于提升的左操作数的位长度.

                  The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand.

                  N1570 是一个草案,与已发布的 ISO C11 标准几乎相同;该条款自 1989 年 ANSI C 标准以来几乎相同.

                  N1570 is a draft, nearly identical to the released ISO C11 standard; this clause has been pretty much the same since the 1989 ANSI C standard.

                  N3690 是 C++ 标准的最新草案;我不确定它是否是最好用的,但同样,这个条款没有改变.

                  N3690 is a recent draft of the C++ standard; I'm not sure whether it's the best one to use, but again, this clause hasn't changed.

                  这篇关于算术右移给出虚假结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么“字符数组的初始化字符串太长"?在 下一篇:gcc - 如何创建目标文件的映射文件

                  相关文章

                  最新文章

                    <legend id='u8J8S'><style id='u8J8S'><dir id='u8J8S'><q id='u8J8S'></q></dir></style></legend>
                    <tfoot id='u8J8S'></tfoot>
                    <i id='u8J8S'><tr id='u8J8S'><dt id='u8J8S'><q id='u8J8S'><span id='u8J8S'><b id='u8J8S'><form id='u8J8S'><ins id='u8J8S'></ins><ul id='u8J8S'></ul><sub id='u8J8S'></sub></form><legend id='u8J8S'></legend><bdo id='u8J8S'><pre id='u8J8S'><center id='u8J8S'></center></pre></bdo></b><th id='u8J8S'></th></span></q></dt></tr></i><div id='u8J8S'><tfoot id='u8J8S'></tfoot><dl id='u8J8S'><fieldset id='u8J8S'></fieldset></dl></div>

                    <small id='u8J8S'></small><noframes id='u8J8S'>

                    • <bdo id='u8J8S'></bdo><ul id='u8J8S'></ul>