Right shift and signed integer

27,768

Solution 1

From the following link:
INT34-C. Do not shift an expression by a negative number of bits or by greater than or equal to the number of bits that exist in the operand

Noncompliant Code Example (Right Shift)
The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2E2. If E1 has a signed type and a negative value, the resulting value is implementation defined and can be either an arithmetic (signed) shift:
Arithmetic (signed) shift
or a logical (unsigned) shift:
Logical (unsigned) shift
This noncompliant code example fails to test whether the right operand is greater than or equal to the width of the promoted left operand, allowing undefined behavior.

unsigned int ui1;
unsigned int ui2;
unsigned int uresult;

/* Initialize ui1 and ui2 */

uresult = ui1 >> ui2;

Making assumptions about whether a right shift is implemented as an arithmetic (signed) shift or a logical (unsigned) shift can also lead to vulnerabilities. See recommendation INT13-C. Use bitwise operators only on unsigned operands.

Solution 2

No, you can't rely on this behaviour. Right shifting of negative quantities (which I assume your example is dealing with) is implementation defined.

Solution 3

In C++, no. It is implementation and/or platform dependent.

In some other languages, yes. In Java, for example, the >> operator is precisely defined to always fill using the left most bit (thereby preserving sign). The >>> operator fills using 0s. So if you want reliable behavior, one possible option would be to change to a different language. (Although obviously, this may not be an option depending on your circumstances.)

Solution 4

AFAIK integers may be represented as sign-magnitude in c++, in which case sign extension would fill with 0s. So you can't rely on this.

Share:
27,768
Anne Quinn
Author by

Anne Quinn

Updated on February 02, 2020

Comments

  • Anne Quinn
    Anne Quinn about 4 years

    On my compiler, the following pseudo code (values replaced with binary):

    sint32 word = (10000000 00000000 00000000 00000000);
    word >>= 16;
    

    produces a word with a bitfield that looks like this:

    (11111111 11111111 10000000 00000000)
    

    My question is, can I rely on this behaviour for all platforms and C++ compilers?