Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Operators and Expressions

Rust bitwise operators

Reference for Rust bitwise operators: AND, OR, XOR, NOT, shifts, overflow rules, the bool versions and the traits behind them, with live examples.

Rust has six bitwise operators: & (AND), | (OR), ^ (XOR), ! (NOT), << (shift left) and >> (shift right). They work on every integer type and compare or move the individual bits of their operands. &, |, ^ and ! also work on bool, where they are plain logical operations that do not short-circuit.

Unlike C, bitwise AND, XOR and OR bind more tightly than the comparison operators, so x & 1 == 0 means (x & 1) == 0. The binary operators need two operands of the same integer type (shifts are the exception: the right side may be any integer type). Each operator has a trait in std::ops (BitAnd, BitOr, BitXor, Not, Shl, Shr) and a compound-assignment version such as &= or <<=.

Rust
let masked = flags & 0b0000_1111;
let flipped = !value;
let shifted = 1u32 << 5;

Try it

Rust AND, OR, XOR, NOT and shifts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Bit flags and shift overflow

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
a & b      // BitAnd::bitand(a, b)
a | b      // BitOr::bitor(a, b)
a ^ b      // BitXor::bitxor(a, b)
!a         // Not::not(a)
a << n     // Shl::shl(a, n)
a >> n     // Shr::shr(a, n)
Name Description
a, b Integers of the same type (or two bool values) for &, |, ^.
a for ! An integer or bool; every bit is inverted.
n The shift amount: any integer type, independent of the type of a.

Returns: a value of the same type as the left operand (Output of the trait).

Edge cases:

  • A shift amount greater than or equal to the bit width of the left operand overflows. With overflow checks on (debug builds) it panics with attempt to shift left with overflow; in release builds the amount is masked to the width. A constant shift that is obviously too big is rejected at compile time.
  • << discards bits shifted out on the left without any overflow error. Only the amount itself is checked.
  • >> on signed integers is an arithmetic shift (the sign bit is copied in); on unsigned integers it fills with zeros.

Usage notes

  • Precedence: from high to low it is <</>>, then &, then ^, then |, all above comparisons and &&. See Operator precedence.
  • ! is overloaded: on bool it is logical NOT, on integers it flips every bit. For signed integers !x == -x - 1.
  • | has other jobs: |a, b| a + b is a closure (Closure syntax), and A | B inside a pattern means "either". They never clash because the parser knows whether it expects an expression or a pattern.
  • Non-short-circuit booleans: a & b and a | b on bool always evaluate both sides. Use && and || when the right side should be skipped.
  • Explicit control of overflow: use checked_shl, wrapping_shl, overflowing_shl and saturating_* helpers when the shift amount comes from input.
  • Integer helpers: count_ones, leading_zeros, trailing_zeros, rotate_left, swap_bytes and is_power_of_two cover most bit tricks without hand-written masks.
  • References work too: &a & &b and a & &b are implemented for the primitive integers, so iterator code with &u8 items needs fewer dereferences.
  • Sets of flags: small const masks like the example are enough for a few flags; the bitflags crate (not part of std) adds a typed wrapper.