number left or right. This is useful for tasks like multiplying or dividing by powers of two. 0 0 0 0 0 0 1 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 1 = 2 * 1 = 1
number left or right. This is useful for tasks like multiplying or dividing by powers of two. 0 0 0 0 0 1 0 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 2 = 2 * 2 = 4
number left or right. This is useful for tasks like multiplying or dividing by powers of two. 0 0 0 0 1 0 0 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 3 = 2 * 4 = 8
number left or right. This is useful for tasks like multiplying or dividing by powers of two. 0 0 0 1 0 0 0 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 4 = 2 * 8 = 16
number left or right. This is useful for tasks like multiplying or dividing by powers of two. 0 0 1 0 0 0 0 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 5 = 2 * 16 = 32
number left or right. This is useful for tasks like multiplying or dividing by powers of two. 0 1 0 0 0 0 0 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 6 = 2 * 32 = 64
number left or right. This is useful for tasks like multiplying or dividing by powers of two. 1 0 0 0 0 0 0 0 2 4 8 16 32 64 128 1 0 1 2 3 4 5 6 7 8 2 7 = 2 * 64 = 128
8 16 32 64 128 1 value = 3 << 2 value = 3 * 4 value = 12 The Value to Shift 2bit to the left Shift Left Multiply by Powers of Two 3 = 0b11 0 1 2 3 4 5 6 7 8
3 << 4 value = 3 * 16 value = 48 The Value to Shift 4bit to the left Shift Left Multiply by Powers of Two 2 4 8 16 32 64 128 1 3 = 0b11 0 1 2 3 4 5 6 7 8
8 16 32 64 128 1 value = 5 << 2 value = 5 * 4 value = 20 Shift Left Multiply by Powers of Two 5 = 0b101 The Value to Shift 2bit to the left 0 1 2 3 4 5 6 7 8
5 << 4 value = 5 * 16 value = 80 The Value to Shift 4bit to the left Shift Left Multiply by Powers of Two 2 4 8 16 32 64 128 1 5 = 0b101 0 1 2 3 4 5 6 7 8
0 Monday Tuesday Wednesday value |= (1 << 1); // Monday value |= (1 << 2); // Tuesday value |= (1 << 3); // Wednesday On which days is the shop open? 0 1 2 3 4 5 6 7 8 Bit Shifting
0 Is the shop open on Weekends? 1 1 0 0 0 0 0 0 Saturday Sunday r = value & (3 << 6); if (r == (3 << 6)) { // Open on Weekends (Saturday and Sunday) } else if (r != 0) { // Open Saturday or Sunday } 0 1 2 3 4 5 6 7 8 Bit Masking 0 1 0 0 0 0 0 0