Bitwise Operators

Bitwise operators let you manipulate data directly at the bit level.

📖 What you'll learn on this page
✅ Must-know essentials
  • & (AND), | (OR), ^ (XOR), ~ (NOT)
  • << (left shift) multiplies by 2, >> (right shift) divides by 2 (for non-negative values)
⭐ Read if you have time
  • Managing on/off flags
  • Using bit masks to extract specific bits
  • Avoid shifting negative values or by the bit width or more

What are bitwise operators?

Computers represent all data internally as binary (sequences of bits). Bitwise operators let you manipulate each bit of that representation directly.
Logical
& | ^ ~
Shift
<< >>
What are they used for? Flag management, masking, arithmetic with powers of two, hardware control, cryptography, and more. They're essential in embedded and systems programming.
Prerequisite: Decimal 12 is 00001100 in binary. Each digit (0 or 1) is one bit of the value.

AND, OR, XOR, NOT

These operators apply a logical operation to each bit independently.
AND ( & )
ABA&B
000
010
100
111
1 only when both bits are 1
OR ( | )
ABA|B
000
011
101
111
1 if either bit is 1
XOR ( ^ )
ABA^B
000
011
101
110
1 only when the bits differ
NOT ( ~ )
A~A
01
10
Flips every bit

Example

  0b11001010  (= 202)
& 0b10101010  (= 170)
-----------
  0b10001010  (= 138)  // Only bits set in both operands remain

Shift Operations (<<, >>)

These operators shift a sequence of bits to the left or right.
Left shift ( << )
a << n = a × 2n
5 << 1 → 10 (5×2)
5 << 3 → 40 (5×8)
Right shift ( >> )
a >> n = a ÷ 2n (truncating; when a is non-negative)
40 >> 1 → 20 (40÷2)
40 >> 3 → 5 (40÷8)
// 5 in binary
//   00000101 = 5
// 5 << 2
//   00010100 = 20  (5 x 4)
// 20 >> 1
//   00001010 = 10  (20 / 2)
Shift left by 1 = multiply by 2. Shift right by 1 = divide by 2 (for non-negative values). Modern compilers already turn x * 8 into a shift, so there's no need to rewrite arithmetic as shifts for speed. Use shifts when you actually want to move bits around.
Shift pitfalls:

Use Cases

Odd/even check

if (n & 1) printf("odd\n");
else        printf("even\n");
// The lowest bit is 1 for odd numbers and 0 for even numbers

Flag management

#define FLAG_READ   (1 << 0)  // 0001 = 1
#define FLAG_WRITE  (1 << 1)  // 0010 = 2
#define FLAG_EXEC   (1 << 2)  // 0100 = 4

int perm = 0;
perm |= FLAG_READ;              // turn the flag ON
perm |= FLAG_WRITE;             // turn the flag ON
if (perm & FLAG_READ) ...       // check whether the flag is set
perm &= ~FLAG_WRITE;            // turn the flag OFF
Linux file permissions (rwx for read, write, and execute) are exactly this kind of bit-flag scheme.

Extracting specific bits (masking)

int color = 0xFF5733;  // RGB color
int r = (color >> 16) & 0xFF;  // 255
int g = (color >> 8)  & 0xFF;  // 87
int b =  color        & 0xFF;  // 51

Bitwise Calculator

Enter two numbers to see the result of each bitwise operation.
Press Calculate to see the results.

Try it yourself: bitwise operations

Inspect each result in binary. Begin with values from 0 to 255 so the bit patterns are easy to follow.
Task 1: Even or odd
n & 1 determines whether the entered integer is even or odd.
Task 2: Flag management
Define READ, WRITE, and EXEC flags, then write functions to enable, disable, and test them.
Task 3: Extract RGB components
0xRRGGBB: extract R, G, and B from an integer in this format using shifts and masks.
Lessons to revisit: For binary representation, review Lesson 3: Variables ; for conditions, see Lesson 12: if statements ; to group flags with other data, see Lesson 38: Structures .

Review Quiz

What is the result of 0b1100 & 0b1010?

0b1000 (8)
0b1110 (14)
0b0110 (6)
0b1010 (10)
Explanation: AND produces 1 only where both bits are 1.
1100 & 1010 → 1000 (= 8). Only bit 3 (the fourth bit from the right, counting bit indices from zero) is set in both operands.

By what factor does n << 3 multiply n?

x3
x6
x8
x16
Explanation: Shifting left by n positions multiplies by 2n.
n << 3 = n × 23 = n × 8.

Related Lessons

Introduction
Lesson 3: Variables
Review the premise that integers are stored in memory as sequences of bits.
Advanced
Lesson 38: Structures (struct)
Think about when to group several states in a struct versus managing them as flags.
Advanced Topics
Image Processing
Leads into practical uses of bitwise operations such as RGB values and masking.
← Previous lesson
Lesson 43: Quiz (Dynamic Memory)
Next lesson →
Lesson 45: Data Structures
Share this article
Share on X Share on Facebook