Nearby lessons

31 of 124

C - Bitwise Operators

Bitwise Operators is one of the foundational topics in C programming. This lesson explains Bitwise Operators — Working on Binary Bits with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Bitwise Operators — Working on Binary Bits

Bitwise operators work on the binary bits of a number. They are used in system-level programming (flags, permissions, graphics, compression). Beginners should know they exist, but not worry about them too early.

OperatorMeaning
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT (ones complement)
<<Shift bits left
>>Shift bits right
In simple words: a normal operator works on the whole number; a bitwise operator looks at each 1 and 0 inside the number and works on those bits one by one.

AND, OR, XOR — How the Bits Behave

The three bit-by-bit rules to remember:

OperatorRule
AND (&)1 & 1 = 1, everything else is 0
OR (|)0 | 0 = 0, everything else is 1
XOR (^)Same bits give 0, different bits give 1
Trainer's Note: These rules apply per bit position. For example 5 & 3: 5 = 101, 3 = 011, so 101 & 011 = 001 = 1.
Example02
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int a = 5; // binary 0101
6 int b = 3; // binary 0011
7 
8 printf("a & b = %d\n", a & b); // 0101 & 0011 = 0001 = 1
9 printf("a | b = %d\n", a | b); // 0101 | 0011 = 0111 = 7
10 printf("a ^ b = %d\n", a ^ b); // 0101 ^ 0011 = 0110 = 6
11}
Output

a & b  = 1
a | b  = 7
a ^ b  = 6
      

Truth Tables — AND, OR, XOR and NOT

A truth table shows the result of an operator for every possible combination of bits. These four tables are the complete behaviour of the bitwise operators:

ABA & B (AND)
000
010
100
111
ABA | B (OR)
000
011
101
111
ABA ^ B (XOR)
000
011
101
110
AResult (~A / NOT)
01
10
In simple words: AND is greedy — it needs two 1s to give 1. OR is generous — one 1 is enough. XOR is a difference detector — different bits give 1. NOT is a flip — it turns every 0 into 1 and every 1 into 0.
Example03
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int a = 5; // binary 0101
6 
7 printf("~a = %d\n", ~a); // flips all bits: ...1010 = -6
8 printf("!(a) = %d\n", !a); // logical NOT: 0 (a is not zero)
9}
Output

~a     = -6
!(a)   = 0
      

Shift Operators — << and >>

Shifting moves all the bits left or right. Each shift left multiplies by 2; each shift right divides by 2:

Example04
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int a = 5; // binary 0101
6 
7 printf("a << 1 = %d\n", a << 1); // 1010 = 10 (5 * 2)
8 printf("a << 2 = %d\n", a << 2); // 10100 = 20 (5 * 4)
9 printf("a >> 1 = %d\n", a >> 1); // 0010 = 2 (5 / 2)
10}
Output

a << 1 = 10
a << 2 = 20
a >> 1 = 2
      
📝 Key Takeaways
  • Bitwise operators work on the binary bits of a number, not the number itself.
  • & AND, | OR, ^ XOR, ~ NOT, << shift left, >> shift right.
  • AND truth table: 1 & 1 = 1, everything else is 0.
  • OR truth table: 0 | 0 = 0, everything else is 1.
  • XOR truth table: same bits give 0, different bits give 1.
  • NOT (~) flips every bit: ~0 = 1 and ~1 = 0.
  • Shift left (<<) multiplies by 2; shift right (>>) divides by 2.
  • Used in system-level programming — beginners should know they exist.

🧠 Test Your Knowledge

5 Questions
Progress: 0 / 5