0
0
Rustprogramming~15 mins

Bitwise operators in Rust - Deep Dive

Choose your learning style9 modes available
Overview - Bitwise operators
What is it?
Bitwise operators are special tools in programming that work directly on the tiny parts inside numbers called bits. Each bit is like a tiny switch that can be either off (0) or on (1). Bitwise operators let you turn these switches on or off, flip them, or compare them in different ways. This helps programmers do fast and low-level tasks like controlling hardware or optimizing calculations.
Why it matters
Without bitwise operators, programmers would have to handle numbers only as whole units, missing the chance to control or check individual bits quickly. This would make some tasks slower or impossible, like setting flags, encoding data, or working with colors and permissions. Bitwise operators make these tasks efficient and precise, saving time and resources.
Where it fits
Before learning bitwise operators, you should understand basic data types like integers and how numbers are stored in computers. After mastering bitwise operators, you can explore topics like binary arithmetic, low-level programming, and optimization techniques.
Mental Model
Core Idea
Bitwise operators let you manipulate each tiny on/off switch (bit) inside a number directly to control or check its details.
Think of it like...
Imagine a row of light switches in your house, each controlling a single light bulb. Bitwise operators are like flipping, turning on, or off each switch individually to create different lighting patterns.
Number bits:  1 0 1 1 0 0 1 0
Operators:    & (AND), | (OR), ^ (XOR), ! (NOT), << (left shift), >> (right shift)
Example:      10110010 & 11001100 = 10000000
              (only switches ON in both stay ON)
Build-Up - 7 Steps
1
FoundationUnderstanding bits and binary numbers
๐Ÿค”
Concept: Learn what bits are and how numbers are represented in binary form.
Every number in a computer is stored as a series of bits, each bit being 0 or 1. For example, the number 6 in binary is 00000110 (in 8 bits). Each position represents a power of two, starting from the right (1, 2, 4, 8, etc.).
Result
You can see how numbers are built from bits and understand the base for bitwise operations.
Understanding bits is essential because bitwise operators work directly on these tiny parts, not on whole numbers.
2
FoundationBasic bitwise operators in Rust
๐Ÿค”
Concept: Introduce the main bitwise operators: AND (&), OR (|), XOR (^), NOT (!), and shifts (<<, >>).
In Rust, you use & for AND, | for OR, ^ for XOR, ! for NOT, << for left shift, and >> for right shift. For example, 5 & 3 means compare bits of 5 and 3, keeping only bits that are 1 in both.
Result
You can write expressions like 5 & 3 and get the result 1.
Knowing these operators lets you start manipulating bits directly, which is faster and more precise than normal math.
3
IntermediateUsing bitwise AND, OR, XOR operators
๐Ÿค”Before reading on: do you think 5 | 3 will be larger, smaller, or equal to both 5 and 3? Commit to your answer.
Concept: Learn how AND, OR, and XOR combine bits differently to produce new numbers.
AND (&) keeps bits that are 1 in both numbers. OR (|) keeps bits that are 1 in either number. XOR (^) keeps bits that are 1 in one number but not both. For example: 5 (0101) & 3 (0011) = 1 (0001) 5 (0101) | 3 (0011) = 7 (0111) 5 (0101) ^ 3 (0011) = 6 (0110)
Result
You can predict how combining bits changes the number.
Understanding these operators helps you control exactly which bits to keep, add, or flip.
4
IntermediateBitwise NOT and shift operators
๐Ÿค”Before reading on: do you think shifting bits left by 1 doubles the number or halves it? Commit to your answer.
Concept: Learn how NOT flips bits and how shifting moves bits left or right, changing the number's value.
NOT (!) flips every bit: 0 becomes 1, 1 becomes 0. Left shift (<<) moves bits to the left, adding zeros on the right, usually doubling the number. Right shift (>>) moves bits to the right, dropping bits on the right, usually halving the number. Example: 5 (00000101) << 1 = 10 (00001010) 5 (00000101) >> 1 = 2 (00000010)
Result
You can change numbers quickly by moving bits instead of adding or subtracting.
Knowing shifts lets you do fast multiplication or division by powers of two.
5
IntermediateApplying bitwise operators to flags and masks
๐Ÿค”
Concept: Use bitwise operators to set, clear, and check specific bits in a number, useful for flags.
Flags are bits that represent on/off states. To set a flag, use OR with a mask. To clear a flag, use AND with the mask's NOT. To check a flag, use AND and compare to zero. Example: let flags = 0b0000_0101; // flags 0 and 2 set flags |= 0b0000_0010; // set flag 1 flags &= !0b0000_0100; // clear flag 2 let is_set = (flags & 0b0000_0001) != 0; // check flag 0
Result
You can control multiple on/off settings inside one number efficiently.
Using bitwise operators for flags saves memory and speeds up checks compared to separate variables.
6
AdvancedBitwise operators with signed integers in Rust
๐Ÿค”Before reading on: do you think shifting a negative number right fills with zeros or ones? Commit to your answer.
Concept: Understand how bitwise operations behave differently with signed (negative) numbers due to two's complement representation.
Rust uses two's complement for signed integers. Right shifting a negative number fills with ones (arithmetic shift) to keep the sign. For example: let x: i8 = -4; // binary 11111100 let y = x >> 1; // result 11111110 (-2) This preserves the negative sign. Left shift works the same as unsigned but beware of overflow.
Result
You can predict how bitwise operations affect negative numbers safely.
Knowing two's complement and shift behavior prevents bugs when manipulating signed integers.
7
ExpertOptimizing code with bitwise tricks
๐Ÿค”Before reading on: do you think x & (x - 1) clears the lowest set bit or sets it? Commit to your answer.
Concept: Learn advanced bitwise tricks used in real-world code for fast calculations and checks.
One trick: x & (x - 1) clears the lowest set bit of x. For example, if x = 12 (1100), x & (x - 1) = 8 (1000). This helps count bits or check powers of two. Another trick: x & -x isolates the lowest set bit. These tricks speed up algorithms like bit counting or flag handling.
Result
You can write faster, more efficient code using clever bitwise patterns.
Understanding these tricks unlocks powerful optimizations hidden in simple bitwise operations.
Under the Hood
Bitwise operators work by directly manipulating the binary digits stored in the computer's memory. Each integer is stored as a fixed number of bits (like 8, 16, 32, or 64). The CPU has instructions that perform bitwise operations very quickly by applying logic gates (AND, OR, XOR, NOT) on each bit in parallel. Shifts move bits left or right inside the register, sometimes filling with zeros or sign bits depending on the type.
Why designed this way?
Bitwise operators were designed to give programmers low-level control over data, enabling efficient manipulation of hardware registers, flags, and compact data formats. Early computers worked directly with bits, so these operators map closely to hardware instructions. Alternatives like arithmetic operations are slower or less precise for bit-level control.
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Integer bits โ”‚
โ”‚  0 1 0 1 1 0 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
      โ”‚
      โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Bitwise AND &  โ”‚
โ”‚ 0 1 1 0 1 0   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
      โ”‚
      โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Result bits   โ”‚
โ”‚ 0 1 0 0 0 0  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
Myth Busters - 4 Common Misconceptions
Quick: Does the bitwise OR operator (|) always produce a number larger than both inputs? Commit to yes or no.
Common Belief:Bitwise OR always makes a bigger number because it turns on more bits.
Tap to reveal reality
Reality:Bitwise OR sets bits that are on in either number, but the result can be smaller if the inputs are negative or in two's complement form.
Why it matters:Assuming OR always increases value can cause bugs when working with signed integers or flags.
Quick: Does shifting bits right always divide the number by two? Commit to yes or no.
Common Belief:Right shifting a number always divides it by two exactly.
Tap to reveal reality
Reality:Right shift divides by two only for positive numbers. For negative numbers, it performs an arithmetic shift, preserving the sign, which may not be exact division.
Why it matters:Misusing shifts for division on signed numbers can lead to incorrect results and subtle bugs.
Quick: Does the bitwise NOT operator (!) simply flip all bits and produce the positive opposite number? Commit to yes or no.
Common Belief:Bitwise NOT flips bits and gives the positive opposite of the number.
Tap to reveal reality
Reality:Bitwise NOT flips bits but does not produce the positive opposite; it produces the bitwise complement, which is related to negative numbers in two's complement.
Why it matters:Misunderstanding NOT leads to wrong assumptions about negation and can cause errors in calculations.
Quick: Does the expression x & (x - 1) set the lowest set bit of x? Commit to yes or no.
Common Belief:x & (x - 1) sets the lowest set bit of x to 1.
Tap to reveal reality
Reality:x & (x - 1) actually clears the lowest set bit of x, turning it off.
Why it matters:Not knowing this trick can cause confusion in bit counting or flag manipulation algorithms.
Expert Zone
1
Bitwise operations on signed integers must consider two's complement and sign extension to avoid unexpected results.
2
Shift operations in Rust are masked by the bit width, so shifting by more than the bit size wraps around instead of causing errors.
3
Combining multiple bitwise operations can create compact and efficient state machines or parsers, but readability suffers without clear comments.
When NOT to use
Avoid bitwise operators when working with floating-point numbers or complex data structures where bit-level control is meaningless. Use arithmetic or higher-level abstractions instead. Also, avoid bitwise tricks in code that prioritizes readability over performance.
Production Patterns
Bitwise operators are widely used in embedded systems to control hardware registers, in graphics programming for color manipulation, in cryptography for fast transformations, and in performance-critical code like compression algorithms or network protocols.
Connections
Boolean algebra
Bitwise operators implement Boolean logic at the bit level.
Understanding Boolean algebra helps grasp how bitwise AND, OR, and XOR combine bits logically.
Digital electronics
Bitwise operations mirror logic gate functions in hardware circuits.
Knowing digital electronics clarifies why bitwise operators are fast and how CPUs execute them.
Set theory
Bitwise operations correspond to set operations on collections of elements.
Viewing bits as membership flags in sets helps understand how AND is intersection, OR is union, and XOR is symmetric difference.
Common Pitfalls
#1Using bitwise NOT (!) on unsigned integers expecting a positive negation.
Wrong approach:let x: u8 = 5; let y = !x; // expecting y to be -5 or positive negation
Correct approach:let x: u8 = 5; let y = (!x).wrapping_add(1); // to get two's complement negation if needed
Root cause:Confusing bitwise NOT with arithmetic negation; NOT flips bits but does not negate the number.
#2Shifting bits by a value equal or larger than the bit width causing unexpected results.
Wrong approach:let x: u8 = 1; let y = x << 8; // shifting by 8 bits on 8-bit integer
Correct approach:let x: u8 = 1; let y = x << 7; // shift by max 7 bits for u8
Root cause:Not knowing Rust masks shift amounts by the bit width, causing shifts to wrap around silently.
#3Using bitwise operators on floating-point numbers directly.
Wrong approach:let x: f32 = 1.5; let y = x & 0xFF; // invalid operation
Correct approach:let x: f32 = 1.5; let bits = x.to_bits(); let y = bits & 0xFF; // operate on bits representation
Root cause:Bitwise operators only work on integers; floating-point numbers must be converted to bits first.
Key Takeaways
Bitwise operators let you control individual bits inside numbers, enabling precise and fast data manipulation.
Understanding how AND, OR, XOR, NOT, and shifts work helps you perform tasks like flag management, fast math, and hardware control.
Rust's bitwise operators work on integer types and respect two's complement for signed numbers, so be careful with negative values.
Advanced bitwise tricks can optimize code but require deep understanding to avoid subtle bugs.
Bitwise operations connect deeply to Boolean logic, digital electronics, and set theory, showing their fundamental role in computing.