toggle_bits

Function toggle_bits 

Source
pub const fn toggle_bits(base: usize, bits: usize, shift: usize) -> usize
Expand description

Toggles (flips) the specified contiguous bits.

§Parameters

  • base: Base value to alter.
  • bits: Amount of bits of base that are relevant.
  • shift: Relevant position of bits inside base, starting from the right/LSB (0).

§Example

use bit_ops::bitops_usize::toggle_bits;

assert_eq!(toggle_bits(0, 1, 0), 1);
assert_eq!(toggle_bits(0, 1, 1), 2);
assert_eq!(toggle_bits(0, 1, 2), 4);
assert_eq!(toggle_bits(0, 1, 7), 128);
assert_eq!(toggle_bits(1, 2, 1), 0b111);
assert_eq!(toggle_bits(0b1000_0100, 4, 2), 0b1011_1000);

§Panics

This function panics for overflowing shifts and bit positions that are outside the range of the underlying type.