I would like to clear some doubts on bit-shifting:
Using
unsigned int:unsigned int i = 500;i << 24;As far as I know this causes
unsigned intto overflow, is this perfectly fine?
C++17 (8.5.7/2) - The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are zero-filled. If E1 has an unsigned type, the value of the result is E1 × 2^E2, reduced modulo one more than the maximum value representable in the result type.
Is using right-shift on
signed intperfectly fine as long as I am shifting less than '32 bit' because 'int' is 32 bits on my platform.int i = 500;i >> 31;
Is that an overflow?
C++17 (8.5.7/3) The value 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 non-negative value, the value of the result is the integral part of the quotient of E1/2^E2.
unsignedoverflow. When overflow would occur on anunsignedvalue, the number is reduced modulo by the largest value for type + 1. There issignedoverflow. - David C. Rankin