Unexpected bitshift behavior in C

3

I am currently trying to extract some bits from an address called addr with a 32 bit mask called mask into another variable called result as follows

int addr = 7;
int x = 0;
uint32_t mask = 0xFFFFFFFF;
result = addr & (mask >> (32 - x));

I am expecting result to be 0 when x = 0, and this is confirmed on online bitshift calculators. however in C code, result is 1. Why is that?

c
undefined-behavior
bit-shift
asked on Stack Overflow Dec 11, 2019 by DanielJomaa • edited Dec 11, 2019 by VillageTech

2 Answers

2

You're performing an illegal bitshift.

Shifting by a value greater or equal than the size in bits of the left operand results in undefined behavior. This is documented in section 6.5.7p3 of the C standard:

The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. 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.

This means you need to check the value of x, and if it is 0 then just use 0 for the bitmask.

int x = 0;
uint32_t mask = 0xFFFFFFFF;
...
if (x == 0) {
    result = 0;
} else {
    result = addr & (mask >> (32 - x));
}
answered on Stack Overflow Dec 11, 2019 by dbush
1

From the C standard (6.5.7 Bitwise shift operators)

3 The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. 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 undeļ¬ned

answered on Stack Overflow Dec 11, 2019 by Vlad from Moscow

User contributions licensed under CC BY-SA 3.0