0
0
Cprogramming~10 mins

Bitwise AND, OR, XOR in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Bitwise AND, OR, XOR
Start with two integers A and B
Apply Bitwise AND (&)
Result: bits set only if both bits are 1
Apply Bitwise OR (|)
Result: bits set if either bit is 1
Apply Bitwise XOR (^)
Result: bits set if bits differ
End
Start with two numbers, then apply AND, OR, XOR bit by bit to get new results.
Execution Sample
C
int a = 6;  // 0110 in binary
int b = 11; // 1011 in binary
int and_result = a & b;
int or_result = a | b;
int xor_result = a ^ b;
This code calculates bitwise AND, OR, and XOR of 6 and 11.
Execution Table
StepOperationa (binary)b (binary)Bitwise OperationResult (binary)Result (decimal)
1Bitwise AND011010110110 & 101100102
2Bitwise OR011010110110 | 1011111115
3Bitwise XOR011010110110 ^ 1011110113
4End-----
💡 All bitwise operations completed on inputs a=6 and b=11.
Variable Tracker
VariableStartAfter ANDAfter ORAfter XOR
a6 (0110)6 (0110)6 (0110)6 (0110)
b11 (1011)11 (1011)11 (1011)11 (1011)
and_resultN/A2 (0010)2 (0010)2 (0010)
or_resultN/AN/A15 (1111)15 (1111)
xor_resultN/AN/AN/A13 (1101)
Key Moments - 3 Insights
Why does bitwise AND result in 2 instead of 6 or 11?
Because bitwise AND sets bits only where both a and b have 1s. See execution_table row 1 where 0110 & 1011 = 0010 (decimal 2).
Why is the OR result 15, which is larger than both inputs?
Bitwise OR sets bits if either input bit is 1, combining all bits from both numbers. Row 2 shows 0110 | 1011 = 1111 (decimal 15).
What does XOR do differently from OR?
XOR sets bits only where bits differ, not where both are 1. Row 3 shows 0110 ^ 1011 = 1101 (decimal 13), different from OR.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the decimal result of the bitwise AND operation?
A6
B2
C11
D15
💡 Hint
Check execution_table row 1 under 'Result (decimal)' for AND operation.
At which step does the bitwise OR operation occur?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Operation' column in execution_table to find OR operation.
If variable a was 7 (0111) instead of 6, what would be the new bitwise AND result with b=11?
A2 (0010)
B1 (0001)
C3 (0011)
D4 (0100)
💡 Hint
Bitwise AND sets bits where both numbers have 1s; compare 0111 & 1011.
Concept Snapshot
Bitwise AND (&), OR (|), XOR (^) work on each bit of two integers.
AND sets bit if both bits are 1.
OR sets bit if either bit is 1.
XOR sets bit if bits differ.
Used for low-level data manipulation.
Full Transcript
This lesson shows how bitwise AND, OR, and XOR work in C. We start with two integers, 6 and 11, and look at their binary forms. Bitwise AND compares each bit and sets the result bit to 1 only if both bits are 1, resulting in 2. Bitwise OR sets bits to 1 if either bit is 1, resulting in 15. Bitwise XOR sets bits to 1 only if the bits differ, resulting in 13. The execution table traces each step with binary and decimal results. The variable tracker shows how variables change after each operation. Key moments clarify common confusions about how these operators work. The quiz tests understanding by asking about results and changes. This helps beginners see exactly how bits combine in these operations.