Nearby lessons

21 of 72

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
Loading editor…

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
Loading editor…

Shift Operators — << and >>

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

Example04
Loading editor…
📝 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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.