Bitwise Operations: Concepts, Formulas, and Clear Examples
Technical aptitude tests bitwise logic on integers at the bit level—AND, OR, XOR, NOT, left/right shifts—without running code. Know truth tables and how shifts multiply/divide by powers of two.
Questions ask for output of expressions, count set bits, or detect tricks ( clears lowest set bit). Think in binary for small n; use algebraic identities for speed on MCQs.
Truth tables
AND
OR
XOR
NOT
Shifts
Left shift
Right shift
Example — Evaluate 5 & 3
5 = 101, 3 = 011 → AND = 001 = 1.
Useful identities
- ; .
- removes lowest 1-bit.
- Check even/odd: .
Masking and flags
Set bit k: . Clear bit k: . Toggle: .
Signed vs unsigned (concept)
- Right shift of negative numbers may sign-extend—read question’s machine model.
Exam pitfalls
- Operator precedence: shift before additive in C-like languages.
- Width: 8-bit result vs 32-bit promotion.
Step-by-step method for exam questions
- Convert operands to binary (8–16 bits enough).
- Apply operator row-wise per bit or use identities.
- For shifts, compute power-of-two multiply/divide.
- Watch unsigned vs signed if stated.
- Convert result back to decimal.
- Sanity-check magnitude (AND ≤ min operands).
Formula cheat sheet
| Situation | Formula | When to use |
|---|---|---|
| Clear lowest set bit | Count bits trick | |
| Power of 2? | Identify | |
| Set bit k | $n | (1<<k)$ |
| XOR swap idea | Concept MCQ | |
| Left shift k | Quick eval |
Frequently asked questions
Do I need to know two's complement deeply?
Basics for NOT width and signed >>; most items use small positive integers.
Language-specific?
Follow C/Java rules unless question specifies Python (& on int).
Fast parity?
XOR all bits or use pattern on paper for tiny n.
Practice on this platform
Use 4 practice sets (10 MCQs, detailed solution after each answer) and 2 timed mocks (20 minutes) on this topic to convert formulas into speed and accuracy.