# BitField in bit_field - Rust
A generic trait which provides methods for extracting and setting specific bits or ranges of bits.
Keywords: rust, rustlang, rust-lang, BitField

# Trait bit_field::BitField

source · [−]

    pub trait BitField {
        const BIT_LENGTH: usize;

        fn get_bit(&self, bit: usize) -> bool;
        fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self;
        fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self;
        fn set_bits<T: RangeBounds<usize>>(
            &mut self,
            range: T,
            value: Self
        ) -> &mut Self;
    }

Expand description

A generic trait which provides methods for extracting and setting specific bits or ranges of bits.

## Associated Constants

source

#### const BIT_LENGTH: usize

The number of bits in this bit field.

    use bit_field::BitField;

    assert_eq!(u32::BIT_LENGTH, 32);
    assert_eq!(u64::BIT_LENGTH, 64);

## Required methods

source

#### fn get_bit(&self, bit: usize) -> bool

Obtains the bit at the index `bit`; note that index 0 is the least significant bit, while index `length() - 1` is the most significant bit.

    use bit_field::BitField;

    let value: u32 = 0b110101;

    assert_eq!(value.get_bit(1), false);
    assert_eq!(value.get_bit(2), true);

###### Panics

This method will panic if the bit index is out of bounds of the bit field.

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

Obtains the range of bits specified by `range`; note that index 0 is the least significant bit, while index `length() - 1` is the most significant bit.

    use bit_field::BitField;

    let value: u32 = 0b110101;

    assert_eq!(value.get_bits(0..3), 0b101);
    assert_eq!(value.get_bits(2..6), 0b1101);
    assert_eq!(value.get_bits(..), 0b110101);
    assert_eq!(value.get_bits(3..=3), value.get_bit(3) as u32);

###### Panics

This method will panic if the start or end indexes of the range are out of bounds of the bit field.

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

Sets the bit at the index `bit` to the value `value` (where true means a value of ‘1’ and false means a value of ‘0’); note that index 0 is the least significant bit, while index `length() - 1` is the most significant bit.

    use bit_field::BitField;

    let mut value = 0u32;

    value.set_bit(1, true);
    assert_eq!(value, 2u32);

    value.set_bit(3, true);
    assert_eq!(value, 10u32);

    value.set_bit(1, false);
    assert_eq!(value, 8u32);

###### Panics

This method will panic if the bit index is out of the bounds of the bit field.

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

Sets the range of bits defined by the range `range` to the lower bits of `value`; to be specific, if the range is N bits long, the N lower bits of `value` will be used; if any of the other bits in `value` are set to 1, this function will panic.

    use bit_field::BitField;

    let mut value = 0u32;

    value.set_bits(0..2, 0b11);
    assert_eq!(value, 0b11);

    value.set_bits(2..=3, 0b11);
    assert_eq!(value, 0b1111);

    value.set_bits(..4, 0b1010);
    assert_eq!(value, 0b1010);

###### Panics

This method will panic if the range is out of bounds of the bit field, or if there are `1`s not in the lower N bits of `value`.

## Implementations on Foreign Types

source

### impl BitField for u8

source

#### const BIT_LENGTH: usize

source

#### fn get_bit(&self, bit: usize) -> bool

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

source

### impl BitField for u16

source

#### const BIT_LENGTH: usize

source

#### fn get_bit(&self, bit: usize) -> bool

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

source

### impl BitField for u32

source

#### const BIT_LENGTH: usize

source

#### fn get_bit(&self, bit: usize) -> bool

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

source

### impl BitField for u64

source

#### const BIT_LENGTH: usize

source

#### fn get_bit(&self, bit: usize) -> bool

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

source

### impl BitField for u128

source

#### const BIT_LENGTH: usize

source

#### fn get_bit(&self, bit: usize) -> bool

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

source

### impl BitField for usize

source

#### const BIT_LENGTH: usize

source

#### fn get_bit(&self, bit: usize) -> bool

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

source

### impl BitField for i8

source

#### const BIT_LENGTH: usize

source

#### fn get_bit(&self, bit: usize) -> bool

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

source

### impl BitField for i16

source

#### const BIT_LENGTH: usize

source

#### fn get_bit(&self, bit: usize) -> bool

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

source

### impl BitField for i32

source

#### const BIT_LENGTH: usize

source

#### fn get_bit(&self, bit: usize) -> bool

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

source

### impl BitField for i64

source

#### const BIT_LENGTH: usize

source

#### fn get_bit(&self, bit: usize) -> bool

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

source

### impl BitField for i128

source

#### const BIT_LENGTH: usize

source

#### fn get_bit(&self, bit: usize) -> bool

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

source

### impl BitField for isize

source

#### const BIT_LENGTH: usize

source

#### fn get_bit(&self, bit: usize) -> bool

source

#### fn get_bits<T: RangeBounds<usize>>(&self, range: T) -> Self

source

#### fn set_bit(&mut self, bit: usize, value: bool) -> &mut Self

source

#### fn set_bits<T: RangeBounds<usize>>(
&mut self,
range: T,
value: Self
) -> &mut Self

## Implementors