1.
Consider the following:
unsigned int a, b;
b = a >> ((sizeof a) * CHAR_BIT);
/* or 2nd operand greater than ((sizeof a) * CHAR_BIT) */
Is this is defined, undefined behavior or implementation dependent behavior?
2.
Also another sub-question:
In the case a is signed int and it is shifted less than its bit length, is the signed bit shifting implementation defined or undefined behavior. In both the cases:
- When shifting right :
a >> 5 - When shifting left :
a << 5
EDIT question edited
1.
From C99 standard, section 6.5.7:
So it’s undefined.
2.
From the same section:
So for left-shift, it’s well-defined if
ais signed and positive. It’s undefined ifais signed and negative.For right-shift, it’s well-defined if
ais signed and positive. It’s implementation-defined ifais signed and negative.