Company LogoCompany Logo
Company LogoCompany Logo
Back to Technical

Bitwise Operations

    • Theory
    • Set 1
    • Set 2
    • Set 3
    • Set 4
    • Set 1
    • Set 2
  1. Aptitude
  2. Technical
  3. Bitwise Operations
  4. Theory

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 (n&(n−1)n \& (n-1)n&(n−1) clears lowest set bit). Think in binary for small n; use algebraic identities for speed on MCQs.

Truth tables

AND

0&0=0, 1&1=1, else 00\&0=0,\ 1\&1=1,\ \text{else } 00&0=0, 1&1=1, else 0

OR

0∣0=0, any 1 ⇒10|0=0,\ \text{any 1 } \Rightarrow 10∣0=0, any 1 ⇒1

XOR

a⊕b=1 iff exactly one of a,b is 1a \oplus b = 1 \text{ iff exactly one of } a,b \text{ is } 1a⊕b=1 iff exactly one of a,b is 1

NOT

∼0=1, ∼1=0 (within bit width)\sim 0 = 1,\ \sim 1 = 0 \text{ (within bit width)}∼0=1, ∼1=0 (within bit width)

Shifts

Left shift

x<<k=x×2k (unsigned; overflow wraps mod 2w)x << k = x \times 2^k \text{ (unsigned; overflow wraps mod } 2^w)x<<k=x×2k (unsigned; overflow wraps mod 2w)

Right shift

x>>k=⌊x/2k⌋ for non-negative xx >> k = \lfloor x / 2^k \rfloor \text{ for non-negative } xx>>k=⌊x/2k⌋ for non-negative x

Example — Evaluate 5 & 3

5 = 101, 3 = 011 → AND = 001 = 1.

Useful identities

  • a⊕a=0a \oplus a = 0a⊕a=0; a⊕0=aa \oplus 0 = aa⊕0=a.
  • a&(a−1)a \& (a-1)a&(a−1) removes lowest 1-bit.
  • Check even/odd: n&1n \& 1n&1.

Masking and flags

Set bit k: n∣(1<<k)n | (1 << k)n∣(1<<k). Clear bit k: n&∼(1<<k)n \& \sim(1 << k)n&∼(1<<k). Toggle: n⊕(1<<k)n \oplus (1 << k)n⊕(1<<k).

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

  1. Convert operands to binary (8–16 bits enough).
  2. Apply operator row-wise per bit or use identities.
  3. For shifts, compute power-of-two multiply/divide.
  4. Watch unsigned vs signed if stated.
  5. Convert result back to decimal.
  6. Sanity-check magnitude (AND ≤ min operands).

Formula cheat sheet

SituationFormulaWhen to use
Clear lowest set bitn&(n−1)n \&(n-1)n&(n−1)Count bits trick
Power of 2?n>0∧(n&(n−1))=0n>0 \land (n\&(n-1))=0n>0∧(n&(n−1))=0Identify
Set bit k$n(1<<k)$
XOR swap ideaa⊕b⊕b=aa\oplus b\oplus b=aa⊕b⊕b=aConcept MCQ
Left shift k×2k\times 2^k×2kQuick 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 n⊕(n>>1)⊕…n \oplus (n>>1) \oplus …n⊕(n>>1)⊕… 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.

Start practiceAll Technical topics