Bit Manipulation Basics AND OR XOR NOT Left Right Shift
📖 Scenario: You are working with a simple device that stores a small number as bits. You want to learn how to change these bits using basic bit operations like AND, OR, XOR, NOT, and shifting bits left or right.
🎯 Goal: Build a Rust program that starts with a number and applies different bit operations step-by-step. You will see how each operation changes the bits of the number.
📋 What You'll Learn
Create a variable called number with the value 0b1010_1100 (172 in decimal).
Create a variable called mask with the value 0b1111_0000 (240 in decimal).
Use the bitwise AND operation between number and mask and store the result in and_result.
Use the bitwise OR operation between number and mask and store the result in or_result.
Use the bitwise XOR operation between number and mask and store the result in xor_result.
Use the bitwise NOT operation on number and store the result in not_result.
Shift number left by 2 bits and store the result in left_shift.
Shift number right by 3 bits and store the result in right_shift.
Print all results in binary format with a 0b prefix.
💡 Why This Matters
🌍 Real World
Bit manipulation is used in low-level programming, device control, and optimizing performance by working directly with bits.
💼 Career
Understanding bit operations is important for embedded systems developers, systems programmers, and anyone working with hardware or performance-critical code.
Progress0 / 4 steps
1
Create the initial number and mask
Create a variable called number and set it to 0b1010_1100. Create another variable called mask and set it to 0b1111_0000.
DSA Rust
Hint
Use let to create variables and assign the binary values exactly as shown.
2
Calculate bitwise AND, OR, and XOR
Using the variables number and mask, create variables and_result, or_result, and xor_result by applying bitwise AND (&), OR (|), and XOR (^) operations respectively.
DSA Rust
Hint
Use & for AND, | for OR, and ^ for XOR between number and mask.
3
Calculate bitwise NOT and shifts
Create variables not_result by applying bitwise NOT (!) on number. Create left_shift by shifting number left by 2 bits using <<. Create right_shift by shifting number right by 3 bits using >>.
DSA Rust
Hint
Use ! for NOT, << for left shift, and >> for right shift.
4
Print all results in binary format
Print the variables and_result, or_result, xor_result, not_result, left_shift, and right_shift in binary format with a 0b prefix using println!. Use the format specifier {:#010b} to show 8 bits with leading zeros.
DSA Rust
Hint
Use println!("{:#010b}", variable) to print 8-bit binary with 0b prefix and leading zeros.
Practice
(1/5)
1. What is the result of the bitwise AND operation between 0b1101 and 0b1011?
easy
A. 0b1001
B. 0b1111
C. 0b0101
D. 0b0011
Solution
Step 1: Understand bitwise AND operation
Bitwise AND compares each bit of two numbers and returns 1 only if both bits are 1.
Step 2: Apply AND to each bit of 0b1101 and 0b1011
Bits: 1&1=1, 1&0=0, 0&1=0, 1&1=1, so result is 0b1001.
Final Answer:
0b1001 -> Option A
Quick Check:
1101 & 1011 = 1001 [OK]
Hint: AND keeps bits set only if both are 1 [OK]
Common Mistakes:
Confusing AND with OR operation
Mixing bit positions when calculating
Forgetting binary format in answer
2. Which of the following is the correct Rust syntax to perform a bitwise XOR between variables a and b?
easy
A. let c = a & b;
B. let c = a && b;
C. let c = a ^ b;
D. let c = a | b;
Solution
Step 1: Recall Rust bitwise XOR operator
In Rust, the caret symbol (^) is used for bitwise XOR.
Step 2: Check each option's operator
The options use: & (bitwise AND), && (logical AND), | (bitwise OR), ^ (bitwise XOR). Only ^ is correct.
Final Answer:
let c = a ^ b; -> Option C
Quick Check:
Bitwise XOR in Rust uses ^ [OK]
Hint: XOR uses ^ symbol in Rust [OK]
Common Mistakes:
Using && instead of ^ for XOR
Confusing bitwise OR (|) with XOR
Using logical operators instead of bitwise
3. What is the output of the following Rust code?
let x: u8 = 0b0011_1100;
let y: u8 = 0b0000_1101;
let z = x | y;
println!("{:08b}", z);
medium
A. 00111101
B. 00110001
C. 00001101
D. 00111100
Solution
Step 1: Understand bitwise OR operation
Bitwise OR returns 1 if at least one bit is 1 in either number.
Step 2: Apply OR to each bit of x and y
x = 00111100, y = 00001101; OR bits: 0|0=0,0|0=0,1|0=1,1|0=1,1|1=1,1|1=1,0|0=0,0|1=1; result = 00111101.
Final Answer:
00111101 -> Option A
Quick Check:
00111100 | 00001101 = 00111101 [OK]
Hint: OR sets bit if either bit is 1 [OK]
Common Mistakes:
Mixing OR with AND operation
Misreading binary digits
Ignoring leading zeros in output
4. Identify the error in this Rust code snippet that attempts to left shift a variable:
let a: u8 = 0b0001_0001;
let b = a << 8
println!("{:08b}", b);
medium
A. Incorrect use of << operator, should be >>
B. Shift amount exceeds variable bit size causing overflow
C. Cannot print binary format with {:08b}
D. Missing semicolon after variable declaration
Solution
Step 1: Check for syntax errors
The declaration 'let b = a << 8' is missing a semicolon (;).
Step 2: Verify other parts
<< is correct for left shift; Rust masks large shift amounts without panic/overflow error; {:08b} works for binary output.
Final Answer:
Missing semicolon after variable declaration -> Option D
Quick Check:
Missing ; after let causes compile error [OK]
Hint: Rust statements must end with semicolon [OK]
Common Mistakes:
Omitting semicolons after let statements
Confusing left shift with right shift
Assuming shifts panic on large amounts
5. Given a 16-bit unsigned integer num, which Rust expression correctly clears (sets to 0) the 5th bit (counting from 0 at the right) without affecting other bits?
hard
A. num | (1 << 5)
B. num & !(1 << 5)
C. num ^ (1 << 5)
D. num << 5
Solution
Step 1: Understand clearing a bit
To clear a bit, use AND with a mask that has 0 at target bit and 1 elsewhere.
Step 2: Create mask for 5th bit
1 << 5 creates a mask with 1 at 5th bit; negating it with ! flips bits, making 0 at 5th bit.