0
0
Rustprogramming~5 mins

Bitwise operators in Rust

Choose your learning style9 modes available
Introduction

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.

You want to check if a specific option or flag is turned on in a number.
You need to combine multiple settings into one number using bits.
You want to quickly multiply or divide by powers of two.
You are working with hardware or network data where bits matter.
You want to flip or clear certain bits in a number.
Syntax
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.

Examples
Examples of numbers with no bits set, all bits set, and one bit set.
Rust
let empty: u8 = 0b0000_0000;
let all_ones: u8 = 0b1111_1111;
let single_bit: u8 = 0b0000_1000;
AND of two numbers with no overlapping bits results in zero.
Rust
let a: u8 = 0b0000_1111;
let b: u8 = 0b1111_0000;
let and_result = a & b; // 0b0000_0000 (no bits overlap)
OR combines all bits from both numbers.
Rust
let a: u8 = 0b0000_1111;
let b: u8 = 0b1111_0000;
let or_result = a | b; // 0b1111_1111 (all bits combined)
Shifting bits left or right moves them, adding or removing zeros.
Rust
let a: u8 = 0b0000_1111;
let shifted_left = a << 2; // 0b0011_1100
let shifted_right = a >> 1; // 0b0000_0111
Sample Program

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.

Rust
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);
}
OutputSuccess
Important Notes

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.

Summary

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.