Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's very probably <em>not</em> attempting to shift by some large number of bits.</p> <p><code>INT_MAX</code> on your system is <em>probably</em> <code>2**31-1</code>, or <code>0x7fffffff</code> (I'm using <code>**</code> to denote exponentiation). If that's the case, then In the declaration:</p> <pre><code>int b = 0x80000000; </code></pre> <p>(which was missing a semicolon in the question; please copy-and-paste your <em>exact</em> code) the constant <code>0x80000000</code> is of type <code>unsigned int</code>, not <code>int</code>. The value is implicitly converted to <code>int</code>. Since the result is outside the bounds of <code>int</code>, the result is implementation-defined (or, in C99, may raise an implementation-defined signal, but I don't know of any implementation that does that).</p> <p>The most common way this is done is to reinterpret the bits of the unsigned value as a 2's-complement signed value. The result in this case is <code>-2**31</code>, or <code>-2147483648</code>.</p> <p>So the behavior isn't undefined because you're shifting by value that equals or exceeds the width of type <code>int</code>, it's undefined because you're shifting by a (very large) <em>negative</em> value.</p> <p>Not that it matters, of course; undefined is undefined.</p> <p>NOTE: The above assumes that <code>int</code> is 32 bits on your system. If <code>int</code> is wider than 32 bits, then most of it doesn't apply (but the behavior is still undefined).</p> <p>If you really wanted to attempt to shift by <code>0x80000000</code> bits, you could do it like this:</p> <pre><code>unsigned long b = 0x80000000; unsigned long a = 1 &gt;&gt; b; // *still* undefined </code></pre> <p><code>unsigned long</code> is guaranteed to be big enough to hold the value <code>0x80000000</code>, so you avoid part of the problem.</p> <p>Of course, the behavior of the shift is just as undefined as it was in your original code, since 0x80000000 is greater than or equal to the width of <code>unsigned long</code>. (Unless your compiler has a <em>really</em> big <code>unsigned long</code> type, but no real-world compiler does that.)</p> <p>The only way to avoid undefined behavior is not to do what you're trying to do.</p> <p>It's possible<strike>, but vanishingly unlikely,</strike> that your original code's behavior is not undefined. That can only happen if the implementation-defined conversion of <code>0x80000000</code> from <code>unsigned int</code> to <code>int</code> yields a value in the range 0 .. 31. IF <code>int</code> is smaller than 32 bits, the conversion is likely to yield 0.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload