39

How do I set, clear and toggle a bit in Rust?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
zzeroo
  • 5,466
  • 4
  • 33
  • 49

3 Answers3

61

Like many other languages, the bitwise operators & (bitwise AND), | (bitwise OR), ^ (bitwise XOR) exist:

fn main() {
    let mut byte: u8 = 0b0000_0000;

    byte |= 0b0000_1000; // Set a bit
    println!("0b{:08b}", byte);

    byte &= 0b1111_0111; // Unset a bit
    println!("0b{:08b}", byte);

    byte ^= 0b0000_1000; // Toggle a bit
    println!("0b{:08b}", byte);
}

The main difference from other languages is in bitwise NOT, which uses ! instead of ~:

fn main() {
    let mut byte: u8 = 0b0000_0000;

    byte = !byte; // Flip all bits
    println!("0b{:08b}", byte);
}

You can also shift bits left or right:

fn main() {
    let mut byte: u8 = 0b0000_1000;

    byte <<= 1; // shift left one bit
    println!("0b{:08b}", byte);

    byte >>= 1; // shift right one bit
    println!("0b{:08b}", byte);
}

There are many other conceptual things that ultimately do bit-level manipulation that are not expressed with operators. Check out the documentation for an integer for examples. One interesting example is leading_zeros. Here is how to rotate by a certain number of bits:

fn main() {
    let mut byte: u8 = 0b1000_0000;

    byte = byte.rotate_left(1); // rotate left one bit
    println!("0b{:08b}", byte);

    byte = byte.rotate_right(1); // rotate right one bit
    println!("0b{:08b}", byte);
}

The book has some more information

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 1
    Interesting bit about `!` versus `~`. I would probably have tried the latter first. – Xavier T. Nov 08 '16 at 14:57
  • 2
    @XavierT. yeah, it's definitely different, but IMO it does make more sense in the end. It is just a negation, after all. – Shepmaster Nov 08 '16 at 15:06
  • 1
    fyi, the notation `"{:#08b}"` is designed to print the '0b' (or '0x', etc) before the binary number – Djzin Nov 08 '16 at 21:28
  • 1
    @Djzin TIL! However, the zero-padding doesn't feel "right"; you actually have to use `{:#010b}` because it's an overall width :-( – Shepmaster Nov 08 '16 at 21:31
13

Rust has both bit-twiddling operators and binary format printing (very helpful for debugging):

fn bit_twiddling(original: u8, bit: u8) {
    let mask = 1 << bit;

    println!(
        "Original: {:b}, Set: {:b}, Cleared: {:b}, Toggled: {:b}",
        original,
        original |  mask,
        original & !mask,
        original ^  mask
    );
}

fn main() {
    bit_twiddling(0, 3);
    bit_twiddling(8, 3);
}

It also has the compound assignment variants (|=, &= and ^=).

The book has some more information

zzeroo
  • 5,466
  • 4
  • 33
  • 49
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
2

Additionally, I'd like to add the following use case: If you have a bitfield like 0b10001000 and you want to update bit 3 in any case to true or false, you can do it like this:

    const SHIFT: u8 = 3;
    let set_bit_3 = false;
    let bits = 0b10001000;
    let bits = bits & !(1 << SHIFT) | (u16::from(set_bit_3) << SHIFT);
phip1611
  • 5,460
  • 4
  • 30
  • 57