0Pricing
C Academy · Lesson

Shifts

Left and right shift.

Shifting Bits

The shift operators move all bits of an integer left or right by a number of positions.

Left shift is << and right shift is >>.

#include <stdio.h>

int main(void) {
    unsigned x = 1;
    printf("1 << 3 = %u\n", x << 3);
    return 0;
}

Left Shift Multiplies

Shifting left by n positions multiplies an unsigned value by 2 to the power n.

1 << 3 is 8, and 5 << 1 is 10.

#include <stdio.h>

int main(void) {
    printf("5 << 1 = %u\n", 5u << 1);
    printf("3 << 4 = %u\n", 3u << 4);
    return 0;
}

All lessons in this course

  1. Bitwise Operators
  2. Shifts
  3. Bit Masks and Flags
  4. Practical Bit Tricks
← Back to C Academy