Discover how tiny switches inside your computer can be controlled with simple magic tricks called bit manipulation!
Why Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA Typescript?
Imagine you have a huge stack of light switches, each representing a number. You want to quickly turn some switches on or off, flip them, or move their positions without checking each one manually.
Manually changing each switch one by one is slow and tiring. It's easy to make mistakes, especially when you have many switches. You might forget which ones to flip or how to move them correctly.
Bit manipulation lets you control all these switches at once using simple commands. You can turn on, turn off, flip, or shift bits quickly and without errors, making your work fast and reliable.
let number = 5; // binary 0101 // Manually check and flip each bit if ((number & 1) === 1) { number = number & (~1); // turn off bit 0 } else { number = number | 1; // turn on bit 0 }
let number = 5; // binary 0101 number = number ^ 1; // flip bit 0 with XOR
Bit manipulation enables lightning-fast control over data at the smallest level, unlocking powerful tricks for optimization and problem solving.
In video games, bit manipulation is used to quickly check which keys are pressed or which game features are active, making gameplay smooth and responsive.
Manual bit handling is slow and error-prone.
Bit manipulation uses simple operations to control bits efficiently.
This skill is essential for fast, low-level programming tasks.
Practice
5 and 3? (5 = 0101, 3 = 0011 in binary)Solution
Step 1: Convert numbers to binary
5 in binary is 0101, 3 in binary is 0011.Step 2: Perform bitwise AND operation
0101 AND 0011 = 0001 (only bits set in both numbers remain set).Final Answer:
1 -> Option AQuick Check:
5 & 3 = 1 [OK]
- Confusing AND with OR operation
- Not converting numbers to binary before operation
- Assuming decimal addition instead of bitwise operation
a and b?Solution
Step 1: Identify bitwise XOR operator in TypeScript
The bitwise XOR operator is the caret symbol (^).Step 2: Check each option for correct syntax
let result = a ^ b; uses ^ correctly. let result = a && b; uses logical AND (&&), let result = a | b; uses bitwise OR (|), let result = a % b; uses modulus (%).Final Answer:
let result = a ^ b; -> Option DQuick Check:
Bitwise XOR uses ^ operator [OK]
- Using logical operators instead of bitwise
- Confusing OR (|) with XOR (^)
- Using modulus operator (%) by mistake
const x = 8; // binary 1000 const y = x >> 2; console.log(y);
Solution
Step 1: Understand right shift operation
Right shift (>>) moves bits to the right by the specified number, dropping rightmost bits.Step 2: Apply right shift to 8 (1000 binary)
Shifting 1000 right by 2 bits gives 0010 binary, which is 2 in decimal.Final Answer:
2 -> Option BQuick Check:
8 >> 2 = 2 [OK]
- Confusing right shift with left shift
- Not converting binary to decimal after shift
- Assuming shift adds zeros on left without dropping bits
let a = 5; let b = 3; let c = a <<< b; console.log(c);
Solution
Step 1: Check the bitwise operators in TypeScript
Valid bitwise shift operators are << (left shift) and >> (right shift). There is no <<< operator.Step 2: Identify the invalid operator usage
The code uses <<< which is invalid syntax and will cause an error.Final Answer:
<<< is not a valid bitwise operator -> Option AQuick Check:
Only << and >> are valid shift operators [OK]
- Using triple shift operator like in some other languages
- Assuming semicolons are mandatory for correctness
- Thinking bitwise operators require specific variable types
n = 29 (binary 11101), which expression will toggle the 3rd bit (counting from 0 at right) using bitwise XOR in TypeScript?Choose the correct code snippet.
Solution
Step 1: Understand toggling a bit with XOR
XOR with 1 flips the bit; XOR with 0 leaves it unchanged.Step 2: Create mask for 3rd bit
1 << 3 shifts 1 to the 3rd bit position (binary 1000).Step 3: Apply XOR to toggle the bit
n ^ (1 << 3) flips the 3rd bit of n.Final Answer:
let result = n ^ (1 << 3); -> Option CQuick Check:
XOR with mask toggles bit [OK]
- Using AND or OR instead of XOR to toggle bits
- Incorrect bit position shift
- Using NOT operator incorrectly with XOR
Ready to prove it?
Pass the test and this pattern moves unknown
