I am trying to understand the behavior of bitwise operators on signed and unsigned types. As per the ISO/IEC document, following are my understandings.
Left shift Operator
The result of
E1 << E2
, is E1 left-shifted E2 bit positionsThe vacated bits on the account of left shift will be filled by zeros.
E1 as signed non-negative:
E1 << E2
will result to E1 multiplied by 2 power of E2, if the value is representable by the result type.Q1: What about signed negatives?
Q2: I am not able to understand on what's meant by "reduced modulo" in the following context. "If E1 has an unsigned type, the value of the result is E1 × 2E2 , reduced modulo one more than the maximum value representable in the result type".
Right Shift Operator
The result of
E1 >> E2
is E1 right-shifted E2 bit positions.E1 as signed non-negative/unsigned:The value of the result is the integral part of the quotient of E1 / 2E2
Q3: For signed negative integers I see, some books defining that the vacant positions will be filled with
1
.Please elaborate more on the use of right shift operator on signed negative int.