Bird
Raised Fist0
DSA Javascriptprogramming~5 mins

Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA Javascript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the AND (&) bitwise operator do between two bits?
It returns 1 only if both bits are 1; otherwise, it returns 0.
Example: 1 & 1 = 1, 1 & 0 = 0, 0 & 0 = 0.
Click to reveal answer
beginner
Explain the OR (|) bitwise operator with a simple example.
OR returns 1 if at least one of the bits is 1; otherwise, it returns 0.
Example: 1 | 0 = 1, 0 | 0 = 0, 1 | 1 = 1.
Click to reveal answer
beginner
What is the result of XOR (^) between two bits?
XOR returns 1 if the bits are different, and 0 if they are the same.
Example: 1 ^ 0 = 1, 1 ^ 1 = 0, 0 ^ 0 = 0.
Click to reveal answer
beginner
Describe the NOT (~) bitwise operator and its effect on a bit.
NOT flips the bit: 0 becomes 1, and 1 becomes 0.
Example: ~0 = 1, ~1 = 0 (in single bit context).
Click to reveal answer
beginner
What happens when you perform a left shift (<<) on a binary number?
Left shift moves all bits to the left by the given number of positions, adding zeros on the right.
It effectively multiplies the number by 2 for each shift.
Example: 0010 (2) << 1 = 0100 (4).
Click to reveal answer
What is the result of 5 & 3 in binary? (5 = 0101, 3 = 0011)
A0010 (2)
B0111 (7)
C0001 (1)
D0100 (4)
Which operator returns 1 only when bits are different?
AOR (|)
BXOR (^)
CAND (&)
DNOT (~)
What does the right shift (>>) operator do?
AMoves bits to the left, adding zeros on the right
BFlips all bits
CMoves bits to the right, discarding bits on the right
DMoves bits to the right, adding zeros on the left
What is the result of ~0 in a 4-bit system?
A1111
B0000
C1000
D0111
Which bitwise operator would you use to set a specific bit to 1?
AOR (|)
BNOT (~)
CXOR (^)
DAND (&)
Explain how the AND, OR, and XOR bitwise operators work with examples.
Think about how bits combine in each operator.
You got /4 concepts.
    Describe what happens when you perform left and right bit shifts on a binary number.
    Imagine moving bits like sliding blocks left or right.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the result of the bitwise AND operation between 5 and 3? (5 & 3)
      easy
      A. 1
      B. 7
      C. 0
      D. 8

      Solution

      1. Step 1: Convert numbers to binary

        5 in binary is 0101, 3 in binary is 0011.
      2. Step 2: Perform bitwise AND

        0101 & 0011 = 0001 (only bits set in both numbers remain set).
      3. Final Answer:

        1 -> Option A
      4. Quick Check:

        5 & 3 = 1 [OK]
      Hint: AND keeps bits set in both numbers only [OK]
      Common Mistakes:
      • Confusing AND with OR operation
      • Not converting numbers to binary before operation
      • Assuming decimal addition instead of bitwise
      2. Which of the following is the correct syntax for performing a bitwise XOR operation between variables a and b in JavaScript?
      easy
      A. let result = a | b;
      B. let result = a && b;
      C. let result = a % b;
      D. let result = a ^ b;

      Solution

      1. Step 1: Identify bitwise XOR operator in JavaScript

        The XOR operator is represented by the caret symbol (^).
      2. Step 2: Check syntax correctness

        let result = a ^ b; uses a ^ b, which is the correct syntax for bitwise XOR.
      3. Final Answer:

        let result = a ^ b; -> Option D
      4. Quick Check:

        XOR operator is ^ in JavaScript [OK]
      Hint: XOR uses ^ symbol in JavaScript [OK]
      Common Mistakes:
      • Using && which is logical AND, not bitwise
      • Using | which is bitwise OR, not XOR
      • Using % which is modulo, unrelated to bits
      3. What will be the output of the following JavaScript code?
      let x = 8; // binary 1000
      let y = x >> 2;
      console.log(y);
      medium
      A. 2
      B. 4
      C. 1
      D. 0

      Solution

      1. Step 1: Understand right shift operation

        Right shift by 2 means move bits 2 places to the right, dropping the rightmost bits.
      2. Step 2: Apply right shift on binary 1000

        1000 >> 2 becomes 0010, which is decimal 2.
      3. Final Answer:

        2 -> Option A
      4. Quick Check:

        8 >> 2 = 2 [OK]
      Hint: Right shift divides by 2 for each shift [OK]
      Common Mistakes:
      • Confusing right shift with left shift
      • Not converting binary to decimal after shift
      • Assuming shift adds zeros on left without dropping bits
      4. Find the error in the following code snippet that attempts to flip all bits of a number n using bitwise NOT:
      let n = 5;
      let flipped = ~n;
      console.log(flipped);

      What is the issue?
      medium
      A. Missing parentheses around ~n
      B. Bitwise NOT returns a negative number due to two's complement representation
      C. Variable n must be a string for bitwise NOT
      D. Bitwise NOT operator (~) is not supported in JavaScript

      Solution

      1. Step 1: Understand bitwise NOT in JavaScript

        Bitwise NOT flips all bits and returns the two's complement negative number.
      2. Step 2: Explain why output is negative

        For 5 (00000101), ~5 flips bits to 11111010 which is -6 in two's complement.
      3. Final Answer:

        Bitwise NOT returns a negative number due to two's complement representation -> Option B
      4. Quick Check:

        ~n flips bits and returns negative number [OK]
      Hint: Bitwise NOT flips bits and returns negative in JS [OK]
      Common Mistakes:
      • Expecting positive flipped bits as output
      • Thinking ~ is unsupported operator
      • Assuming input must be string for bitwise ops
      5. You want to set the 3rd bit (0-indexed from right) of a number num to 1 without changing other bits. Which expression correctly does this in JavaScript?
      hard
      A. num = num ^ (1 << 3);
      B. num = num & (1 << 3);
      C. num = num | (1 << 3);
      D. num = num << 3;

      Solution

      1. Step 1: Understand setting a bit using OR and shift

        Left shift 1 by 3 gives 00001000, which targets the 3rd bit.
      2. Step 2: Use OR to set the bit without changing others

        OR with this mask sets the 3rd bit to 1, keeping others unchanged.
      3. Final Answer:

        num = num | (1 << 3); -> Option C
      4. Quick Check:

        Use OR with shifted 1 to set bit [OK]
      Hint: Use OR with (1 << bit_position) to set bit [OK]
      Common Mistakes:
      • Using AND which clears bits instead of setting
      • Using XOR which toggles bits and may unset
      • Using left shift alone which changes number value
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard