Bitwise operators in Rust - Time & Space Complexity
Bitwise operators work directly on the bits of numbers. Understanding their time complexity helps us know how fast these operations run as numbers get bigger.
We want to see how the time to do bitwise operations changes when the size of the number changes.
Analyze the time complexity of the following code snippet.
fn bitwise_and(a: u32, b: u32) -> u32 {
a & b
}
fn bitwise_or(a: u32, b: u32) -> u32 {
a | b
}
fn bitwise_xor(a: u32, b: u32) -> u32 {
a ^ b
}
This code performs simple bitwise AND, OR, and XOR operations on two 32-bit unsigned integers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Bitwise operation on each bit of the input numbers.
- How many times: Once per bit, so 32 times for u32 numbers.
Each bitwise operation checks each bit once. If the number size doubles, the bits double, so the work doubles too.
| Input Size (bits) | Approx. Operations |
|---|---|
| 8 | 8 |
| 32 | 32 |
| 64 | 64 |
Pattern observation: The operations grow linearly with the number of bits in the input.
Time Complexity: O(n)
This means the time to do bitwise operations grows in a straight line with the number of bits in the numbers.
[X] Wrong: "Bitwise operations always run instantly, no matter the number size."
[OK] Correct: Bitwise operations run once per bit, so bigger numbers with more bits take more time.
Knowing bitwise operation time helps you understand low-level code speed and is useful when optimizing or working with hardware-level tasks.
"What if we used 128-bit numbers instead of 32-bit? How would the time complexity change?"
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
