Bitwise operators let you work directly with the bits inside numbers. This helps you do fast, low-level tasks like setting flags or checking options.
Bitwise operators in Rust
fn main() {
let a: u8 = 0b1010_1100; // 172 in decimal
let b: u8 = 0b0101_1010; // 90 in decimal
let and_result = a & b; // bitwise AND
let or_result = a | b; // bitwise OR
let xor_result = a ^ b; // bitwise XOR
let not_result = !a; // bitwise NOT
let left_shift = a << 2; // shift bits left by 2
let right_shift = a >> 3; // shift bits right by 3
}Bitwise operators work on the binary form of numbers.
Rust uses standard symbols: & for AND, | for OR, ^ for XOR, ! for NOT, << and >> for shifts.
let empty: u8 = 0b0000_0000; let all_ones: u8 = 0b1111_1111; let single_bit: u8 = 0b0000_1000;
let a: u8 = 0b0000_1111; let b: u8 = 0b1111_0000; let and_result = a & b; // 0b0000_0000 (no bits overlap)
let a: u8 = 0b0000_1111; let b: u8 = 0b1111_0000; let or_result = a | b; // 0b1111_1111 (all bits combined)
let a: u8 = 0b0000_1111; let shifted_left = a << 2; // 0b0011_1100 let shifted_right = a >> 1; // 0b0000_0111
This program shows how to check a bit, flip bits, and shift bits left and right. It prints the binary form so you can see the bits clearly.
fn main() {
let number: u8 = 0b1010_1100; // 172 decimal
println!("Original number in binary: {:08b}", number);
let mask: u8 = 0b0000_1000; // mask to check 4th bit
let is_bit_set = (number & mask) != 0;
println!("Is the 4th bit set? {}", is_bit_set);
let flipped = !number;
println!("Flipped bits: {:08b}", flipped);
let shifted_left = number << 2;
println!("Shifted left by 2: {:08b}", shifted_left);
let shifted_right = number >> 3;
println!("Shifted right by 3: {:08b}", shifted_right);
}Bitwise operations run very fast because they work directly on bits.
Time complexity is O(1) since operations are fixed-time on numbers.
Common mistake: forgetting that shifting left can lose bits if they move past the size limit.
Use bitwise operators when you need precise control over bits, not for general math.
Bitwise operators let you work with the bits inside numbers.
They include AND (&), OR (|), XOR (^), NOT (!), and shifts (<<, >>).
Use them to check, set, flip, or move bits efficiently.
Practice
Example: 5 & 3Solution
Step 1: Understand the AND operator
The AND operator (&) compares each bit of two numbers and returns 1 only if both bits are 1.Step 2: Compare with other operators
OR (|) returns 1 if either bit is 1, XOR (^) returns 1 if bits differ, NOT (!) flips bits.Final Answer:
AND (&) -> Option AQuick Check:
AND returns 1 only if both bits are 1 [OK]
- Confusing AND with OR which returns 1 if either bit is 1
- Thinking XOR returns 1 if both bits are 1
- Using NOT (!) as a binary operator instead of unary
a and b?Solution
Step 1: Recall XOR operator symbol
In Rust, the bitwise XOR operator is represented by the caret symbol (^).Step 2: Check other operators
& is AND, | is OR, and ! is unary NOT, so onlya ^ bis correct for XOR.Final Answer:
a ^ b -> Option DQuick Check:
XOR uses ^ symbol in Rust [OK]
- Using & or | instead of ^ for XOR
- Trying to use ! as a binary operator
- Confusing syntax with other languages
let x = 0b1010; // binary 10
let y = 0b1100; // binary 12
let result = x | y;
println!("{:04b}", result);Solution
Step 1: Convert binary to bits and apply OR
x = 1010, y = 1100. OR (|) returns 1 if either bit is 1. 1010 | 1100 = 1110Step 2: Print result in 4-bit binary
Result is 1110 in binary, which matches 1110.Final Answer:
1110 -> Option BQuick Check:
Bitwise OR of 1010 and 1100 = 1110 [OK]
- Mixing up OR with AND results
- Reading binary digits incorrectly
- Forgetting to print in binary format
let a = 0b1010;
let b = 0b0101;
let c = a && b;
println!("{}", c);Solution
Step 1: Check operator usage
The code uses && which is a logical AND, not bitwise AND. Bitwise AND uses &.Step 2: Verify other syntax
Semicolons are present, binary literals are correct, and println! has a format specifier, so no errors there.Final Answer:
Using && instead of & for bitwise AND -> Option CQuick Check:
Bitwise AND uses & not && [OK]
- Confusing logical AND (&&) with bitwise AND (&)
- Assuming binary literals need prefix changes
- Missing format specifier in println! macro
n, which Rust expression flips (toggles) the 3rd bit (counting from 0) of n using bitwise operators?Solution
Step 1: Understand bit flipping with XOR
XOR (^) with 1 flips the bit: if bit is 0 it becomes 1, if 1 it becomes 0. Shifting 1 by 3 sets the mask for the 3rd bit.Step 2: Analyze other options
& with mask clears bits except the 3rd bit, | sets the 3rd bit to 1, ~n & mask is unrelated to flipping a single bit.Final Answer:
n ^ (1 << 3) -> Option AQuick Check:
XOR with bit mask flips that bit [OK]
- Using & or | which clear or set bits instead of flipping
- Confusing ~ (NOT) usage
- Not shifting 1 to correct bit position
Ready to prove it?
Pass the test and this pattern moves unknown
