Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about</p> <pre><code>unsigned int zerofill(unsigned int a, int b) { return a &gt;&gt; b; } </code></pre> <p>Or, </p> <pre><code>int zerofill(int a, int b) { return ((unsigned int)a) &gt;&gt; b; } </code></pre> <p>In C, the right shift operator acts as an arithmetic shift if its left operand is signed, or a logical shift if unsigned. It appears that the original PHP code is going to some lengths to get an unsigned shift using only a signed shift.</p> <p>The C99 standard at section 6.5.7 says:</p> <blockquote> <p>The result of <code>E1 &gt;&gt; E2</code> is <code>E1</code> right-shifted <code>E2</code> bit positions. If <code>E1</code> has an unsigned type or if <code>E1</code> has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of <code>E1 / 2^E2</code>. If <code>E1</code> has a signed type and a negative value, the resulting value is implementation-defined.</p> </blockquote> <p>This says clearly that to get zero bits shifted in, the operand must be either unsigned, or signed and positive. The only question is the behavior when the operand is signed and negative where the result is implementation defined. In practice, every implementation I've encountered has treated that case by sign extension so that the result still makes sense as a division by a power of two when integer values are stored as two's complement.</p> <p>I don't have the reference at hand, but C89 said effectively the same thing, and established practice before the C standard was also consistent with this interpretation.</p>
    singulars
    1. This table or related slice is empty.
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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