Битовые сдвиги

Простые типы / Числа / Integer

//shift left by n multiplies by 2 to the power n
unsigned a = 1u << 3;
//a is 8

//shift right by n divides by 2 to the power n
unsigned b = 200u >> 2;
//b is 50

//a negative number shifted right keeps its sign in gcc,
//the standard leaves it to the compiler
int c = -16 >> 2;
//c is -4

//shifting by the width of the type or more is undefined:
//1u << 32 is an error, a wide shift needs a wide type
unsigned long long d = 1ull << 40;
//d is 1099511627776

//there is no rotation operator: two shifts and an Or
uint8_t v = 0b1001'0110;
uint8_t rotated = (uint8_t)(v << 3 | v >> 5);
//rotated is 180 (10110100)

printf("a is %u\n", a);
printf("b is %u\n", b);
printf("c is %d\n", c);
printf("d is %llu\n", d);
printf("rotated is %d\n", rotated);