Struct brainfoamkit_lib::Byte

source ·
pub struct Byte { /* private fields */ }
Expand description

A Byte is an 8-bit unsigned integer (u8).

This is a wrapper around eight Bit instances. The least significant bit is bit_0 and the most significant bit is bit_7. This struct is used to conveniently manipulate 8-bit values.

Note that the Bit instances are stored in reverse (LSB to MSB) order, but the constructor takes them in the correct order (MSB to LSB) to provide a predictable and intuitive interface.

Examples

Create a byte from primitive Bit values

An easy way create a byte is to use the Byte::new() method. This method takes eight Bit instances as arguments. The least significant bit is bit_0 and the most significant bit is bit_7. The order of the arguments is the same as the order of the bits in the byte.

use brainfoamkit_lib::{
    Bit,
    Byte,
};

let byte = Byte::new(
    Bit::one(),
    Bit::zero(),
    Bit::one(),
    Bit::zero(),
    Bit::one(),
    Bit::zero(),
    Bit::one(),
    Bit::zero(),
);
assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(byte.to_string(), "0xAA");

Create a byte from a primitive u8 value

use brainfoamkit_lib::Byte;

let byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(byte.to_string(), "0xAA");

Create a byte from two Nybbles

use brainfoamkit_lib::{
    Byte,
    Nybble,
};

let high_nybble = Nybble::from(0b1011); // Dec: 11; Hex: 0x0B; Oct: 0o13
let low_nybble = Nybble::from(0b0101); // Dec: 5; Hex: 0x05; Oct: 0o5
let byte = Byte::from_nybbles(high_nybble, low_nybble);
assert_eq!(u8::from(&byte), 0b10110101); // Dec: 181; Hex: 0xB5; Oct: 0o265
assert_eq!(byte.to_string(), "0xB5");

Set and Unset bits to generate desired byte

use brainfoamkit_lib::Byte;
use brainfoamkit_lib::Bit;

let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
byte.set_bit(0); // Byte: 0b00000001; Dec: 1; Hex: 0x01; Oct: 0o1
byte.set_bit(1); // Byte: 0b00000011; Dec: 3; Hex: 0x03; Oct: 0o3
byte.set_bit(2); // Byte: 0b00000111; Dec: 7; Hex: 0x07; Oct: 0o7
assert_eq!(u8::from(&byte), 0b00000111); // Dec: 7; Hex: 0x07; Oct: 0o7
assert_eq!(byte.to_string(), "0x07");
byte.unset_bit(1); // Byte: 0b00000101; Dec: 5; Hex: 0x05; Oct: 0o5
assert_eq!(u8::from(&byte), 0b00000101); // Dec: 5; Hex: 0x05; Oct: 0o5
assert_eq!(byte.to_string(), "0x05");

Get the Bit value at a given index

use brainfoamkit_lib::{
    Bit,
    Byte,
};

let byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(byte.get_bit(0), Bit::Zero); // Least significant bit
assert_eq!(byte.get_bit(1), Bit::One);
assert_eq!(byte.get_bit(2), Bit::Zero);
assert_eq!(byte.get_bit(3), Bit::One);
assert_eq!(byte.get_bit(4), Bit::Zero);
assert_eq!(byte.get_bit(5), Bit::One);
assert_eq!(byte.get_bit(6), Bit::Zero);
assert_eq!(byte.get_bit(7), Bit::One); // Most significant bit

Flip the Bit value at a given index

use brainfoamkit_lib::Byte;
use brainfoamkit_lib::Bit;

let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
byte.set_bit(0); // Byte: 0b00000001; Dec: 1; Hex: 0x01; Oct: 0o1
byte.set_bit(2); // Byte: 0b00000101; Dec: 5; Hex: 0x05; Oct: 0o5
byte.set_bit(4); // Byte: 0b00010101; Dec: 21; Hex: 0x15; Oct: 0o25
assert_eq!(u8::from(&byte), 0b00010101); // Dec: 21; Hex: 0x15; Oct: 0o25
assert_eq!(byte.to_string(), "0x15");
byte.flip_bit(2); // Byte: 0b00010001; Dec: 17; Hex: 0x11; Oct: 0o21
assert_eq!(u8::from(&byte), 0b00010001); // Dec: 17; Hex: 0x11; Oct: 0o21
assert_eq!(byte.to_string(), "0x11");
byte.flip_bit(7); // Byte: 0b10010001; Dec: 145; Hex: 0x91; Oct: 0o221
assert_eq!(u8::from(&byte), 0b10010001); // Dec: 145; Hex: 0x91; Oct: 0o221
assert_eq!(byte.to_string(), "0x91");

Panics

The methods set_bit(), unset_bit() and get_bit() will panic if the index is out of bounds.

See Also

  • Bit: A single bit.
  • Nybble: A 4-bit unsigned integer (u4).

Implementations§

source§

impl Byte

source

pub const fn new( zeroth: Bit, first: Bit, second: Bit, third: Bit, fourth: Bit, fifth: Bit, sixth: Bit, seventh: Bit ) -> Self

Creates a new Byte instance with the specified Bit values.

This method takes eight Bit instances as arguments. The least significant bit is bit_0 and the most significant bit is bit_7.

Note that the Bit instances are stored in reverse (LSB to MSB) order, but the constructor takes them in the correct order (MSB to LSB) to provide a predictable and intuitive interface.

Arguments
  • zeroth - The value of the most significant bit.
  • first - The value of the second most significant bit.
  • second - The value of the third most significant bit.
  • third - The value of the fourth most significant bit.
  • fourth - The value of the fifth most significant bit.
  • fifth - The value of the sixth most significant bit.
  • sixth - The value of the seventh most significant bit.
  • seventh - The value of the least significant bit.
Examples
Single Digit
use brainfoamkit_lib::Byte;
use brainfoamkit_lib::Bit;

let byte_single = Byte::new(Bit::zero(), Bit::zero(), Bit::zero(), Bit::zero(), Bit::one(), Bit::zero(), Bit::one(), Bit::zero());
assert_eq!(u8::from(&byte_single), 0b00001010); // Dec: 10; Hex: 0x0A; Oct: 0o12
assert_eq!(byte_single.to_string(), "0x0A");
Double Digit
use brainfoamkit_lib::Byte;
use brainfoamkit_lib::Bit;

let byte_double = Byte::new(Bit::one(), Bit::zero(), Bit::one(), Bit::zero(), Bit::one(), Bit::zero(), Bit::one(), Bit::zero());
assert_eq!(u8::from(&byte_double), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(byte_double.to_string(), "0xAA");
Returns

A Byte containing the specified Bit values.

See Also
source

pub fn from_nybbles(high_nybble: Nybble, low_nybble: Nybble) -> Self

Creates a new Byte from two Nybbles.

This method takes two Nybbles as arguments. The first Nybble (bit_7 to bit_4) is the High Nybble and the second Nybble (bit_3 to bit_0) is the Low Nybble.

Arguments
  • high_nybble - The High Nybble of the Byte.
  • low_nybble - The Low Nybble of the Byte.
Examples
use brainfoamkit_lib::{
    Byte,
    Nybble,
};

let high_nybble = Nybble::from(0b1011); // Dec: 11; Hex: 0x0B; Oct: 0o13
let low_nybble = Nybble::from(0b0101); // Dec: 5; Hex: 0x05; Oct: 0o5
let byte = Byte::from_nybbles(high_nybble, low_nybble);
assert_eq!(u8::from(&byte), 0b10110101); // Dec: 181; Hex: 0xB5; Oct: 0o265
assert_eq!(byte.to_string(), "0xB5");
Returns

A Byte containing the value of the two Nybbles.

See Also
source

pub fn get_high_nybble(&self) -> Nybble

Gets the High or First Nybble from the Byte. This method returns a Nybble. The High Nybble is the first nybble (bit_7 to bit_4).

Examples
use brainfoamkit_lib::Byte;

let byte = Byte::from(0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
let high_nybble = byte.get_high_nybble(); // Nybble: 0b0101; Dec: 5; Hex: 0x05; Oct: 0o5
assert_eq!(u8::from(&high_nybble), 0b0101); // Dec: 5; Hex: 0x05; Oct: 0o5
assert_eq!(high_nybble.to_string(), "0x5");
Returns

A Nybble representing the High Nybble of the Byte.

See Also
source

pub fn get_low_nybble(&self) -> Nybble

Gets the Low or Second Nybble from the Byte. This method returns a Nybble. The Low Nybble is the second nybble (bit_3 to bit_0).

Examples
use brainfoamkit_lib::Byte;

let byte = Byte::from(0b01010101); // Byte: 0b01010101; Dec: 85; Hex: 0x55; Oct: 0o125
let low_nybble = byte.get_low_nybble(); // Nybble: 0b0101; Dec: 5; Hex: 0x05; Oct: 0o5
assert_eq!(u8::from(&low_nybble), 0b0101); // Dec: 5; Hex: 0x05; Oct: 0o5
assert_eq!(low_nybble.to_string(), "0x5");
Returns

A Nybble containing the least significant nybble of the Byte.

See Also
source

pub fn set_bit(&mut self, index: usize)

Sets the Bit value at the specified index.

This method is used “Set” the bit value at a given index. This means that that bit value is set to 1.

The index is zero-based, so the least significant bit is at index 0 and the most significant bit is at index 7.

Arguments
  • index - The index of the Bit to set.
Examples
use brainfoamkit_lib::Byte;

let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
byte.set_bit(0); // Byte: 0b00000001; Dec: 1; Hex: 0x01; Oct: 0o1
byte.set_bit(2); // Byte: 0b00000101; Dec: 5; Hex: 0x05; Oct: 0o5
assert_eq!(u8::from(&byte), 0b00000101); // Dec: 5; Hex: 0x05; Oct: 0o5
assert_eq!(byte.to_string(), "0x05");
byte.set_bit(4); // Byte: 0b00010101; Dec: 21; Hex: 0x15; Oct: 0o25
byte.set_bit(6); // Byte: 0b01010101; Dec: 85; Hex: 0x55; Oct: 0o125
assert_eq!(u8::from(&byte), 0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
assert_eq!(byte.to_string(), "0x55");
Panics

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

Side Effects

This method will flip the Bit value at the specified index.

See Also
  • unset_bit(): Unset the Bit value at the specified index.
  • flip_bit(): Flip the Bit value at the specified index.
  • get_bit(): Get the Bit value at the specified index.
source

pub fn unset_bit(&mut self, index: usize)

Unsets the Bit value at the specified index.

This method is used “Unset” the bit value at a given index. This means that that bit value is set to 0.

The index is zero-based, so the least significant bit is at index 0 and the most significant bit is at index 7.

Arguments
  • index - The index of the Bit to unset.
Examples
use brainfoamkit_lib::Byte;
use brainfoamkit_lib::Bit;

let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
byte.set_bit(0); // Byte: 0b00000001; Dec: 1; Hex: 0x01; Oct: 0o1
byte.set_bit(2); // Byte: 0b00000101; Dec: 5; Hex: 0x05; Oct: 0o5
assert_eq!(u8::from(&byte), 0b00000101); // Dec: 5; Hex: 0x05; Oct: 0o5
assert_eq!(byte.to_string(), "0x05");
byte.unset_bit(0); // Byte: 0b00000100; Dec: 4; Hex: 0x04; Oct: 0o4
assert_eq!(u8::from(&byte), 0b00000100); // Dec: 4; Hex: 0x04; Oct: 0o4
assert_eq!(byte.to_string(), "0x04");
Panics

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

Side Effects

This method will unset the Bit value at the specified index.

See Also
  • set_bit(): Set the Bit value at the specified index.
  • flip_bit(): Flip the Bit value at the specified index.
  • get_bit(): Get the Bit value at the specified index.
source

pub fn get_bit(&self, index: u8) -> Bit

Get the Bit value at the specified index.

The index is zero-based, so the least significant bit is at index 0 and the most significant bit is at index 7.

Arguments
  • index - The index of the Bit to get.
Examples
use brainfoamkit_lib::{
    Bit,
    Byte,
};

let byte = Byte::new(
    Bit::one(),
    Bit::zero(),
    Bit::one(),
    Bit::zero(),
    Bit::one(),
    Bit::zero(),
    Bit::one(),
    Bit::zero(),
);
assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(byte.get_bit(0), Bit::Zero); // Least significant bit
assert_eq!(byte.get_bit(1), Bit::One);
assert_eq!(byte.get_bit(2), Bit::Zero);
assert_eq!(byte.get_bit(3), Bit::One);
assert_eq!(byte.get_bit(4), Bit::Zero);
assert_eq!(byte.get_bit(5), Bit::One);
assert_eq!(byte.get_bit(6), Bit::Zero);
assert_eq!(byte.get_bit(7), Bit::One); // Most significant bit
Panics

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

Returns

A Bit containing the value of the Bit at the specified index.

See Also
  • set_bit(): Set the Bit value at the specified index.
  • unset_bit(): Unset the Bit value at the specified index.
  • flip_bit(): Flip the Bit value at the specified index.
source

pub fn flip_bit(&mut self, index: u8)

Flips the Bit value at the specified index.

This method is used to flip the bit value at a given index. This means that that bit value is set to the opposite of its current value.

The index is zero-based, so the least significant bit is at index 0 and the most significant bit is at index 7.

Arguments
  • index - The index of the Bit to flip.
Examples
use brainfoamkit_lib::Byte;

let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
byte.set_bit(0); // Byte: 0b00000001; Dec: 1; Hex: 0x01; Oct: 0o1
byte.set_bit(2); // Byte: 0b00000101; Dec: 5; Hex: 0x05; Oct: 0o5
byte.set_bit(4); // Byte: 0b00010101; Dec: 21; Hex: 0x15; Oct: 0o25
byte.set_bit(6); // Byte: 0b01010101; Dec: 85; Hex: 0x55; Oct: 0o125

assert_eq!(u8::from(&byte), 0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
assert_eq!(byte.to_string(), "0x55");

byte.flip_bit(0); // Byte: 0b01010100; Dec: 84; Hex: 0x54; Oct: 0o124
byte.flip_bit(1); // Byte: 0b01010110; Dec: 86; Hex: 0x56; Oct: 0o126
byte.flip_bit(2); // Byte: 0b01010010; Dec: 82; Hex: 0x52; Oct: 0o122
byte.flip_bit(3); // Byte: 0b01011010; Dec: 90; Hex: 0x5A; Oct: 0o132
byte.flip_bit(4); // Byte: 0b01111010; Dec: 122; Hex: 0x7A; Oct: 0o172
byte.flip_bit(5); // Byte: 0b00111010; Dec: 58; Hex: 0x3A; Oct: 0o72
byte.flip_bit(6); // Byte: 0b10111010; Dec: 186; Hex: 0xBA; Oct: 0o272
byte.flip_bit(7); // Byte: 0b00111010; Dec: 122; Hex: 0x7A; Oct: 0o172

assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(byte.to_string(), "0xAA");
Panics

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

Side Effects

This method will flip the Bit value at the specified index.

See Also
  • set_bit(): Set the Bit value at the specified index.
  • unset_bit(): Unset the Bit value at the specified index.
  • get_bit(): Get the Bit value at the specified index.
source

pub fn flip(&mut self)

Flips all of the Bit values in the Byte.

This method is used to flip all of the bit values in the Byte. This means that all of the bit values are set to the opposite of their current values. This can be used to find the two’s complement of a Byte.

Examples
use brainfoamkit_lib::Byte;

let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0

byte.set_bit(0); // Byte: 0b00000001; Dec: 1; Hex: 0x01; Oct: 0o1
byte.set_bit(2); // Byte: 0b00000101; Dec: 5; Hex: 0x05; Oct: 0o5
byte.set_bit(4); // Byte: 0b00010101; Dec: 21; Hex: 0x15; Oct: 0o25
byte.set_bit(6); // Byte: 0b01010101; Dec: 85; Hex: 0x55; Oct: 0o125

assert_eq!(u8::from(&byte), 0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
assert_eq!(byte.to_string(), "0x55");

byte.flip();

assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(byte.to_string(), "0xAA");
Side Effects

This method will flip all of the Bit values in the Byte.

See Also
  • flip_bit(): Flip the Bit value at the specified index.
source

pub fn increment(&mut self)

Increments the Byte by one.

This method is used to increment the Byte by one. This means that the value of the byte will increase by 1.

This is a wrapping operator with no overflow. What this translates into in practice is that if you increment the byte when its value is 255, instead of getting an overflow error or the operation having no effect, the value wraps around to 0.

Examples
Simple Increment
use brainfoamkit_lib::Byte;

let mut byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0

byte.increment();

assert_eq!(u8::from(&byte), 0b00000001); // Dec: 1; Hex: 0x01; Oct: 0o1
assert_eq!(byte.to_string(), "0x01");

byte.increment();

assert_eq!(u8::from(&byte), 0b00000010); // Dec: 2; Hex: 0x02; Oct: 0o2
assert_eq!(byte.to_string(), "0x02");
Increment at Boundary
use brainfoamkit_lib::Byte;

let mut byte = Byte::from(255); // Byte: 0b11111111; Dec: 255; Hex: 0xFF; Oct: 0o377

byte.increment();

assert_eq!(u8::from(&byte), 0b00000000); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
assert_eq!(byte.to_string(), "0x00");
Side Effects

This method will increment the Byte by one.

See Also
source

pub fn decrement(&mut self)

Decrements the Byte by one.

This method is used to decrement the Byte by one. This means that the value of the byte will decrease by 1.

This is a wrapping operator with no overflow. What this translates into in practice is that if you decrement the byte when its value is 0, instead of getting an overflow error or the operation having no effect, the value wraps around to 255.

Examples
Simple Decrement
use brainfoamkit_lib::Byte;

let mut byte = Byte::from(0b00000010); // Byte: 0b00000010; Dec: 2; Hex: 0x02; Oct: 0o2

byte.decrement();

assert_eq!(u8::from(&byte), 0b00000001); // Dec: 1; Hex: 0x01; Oct: 0o1
assert_eq!(byte.to_string(), "0x01");

byte.decrement();

assert_eq!(u8::from(&byte), 0b00000000); // Dec: 0; Hex: 0x00; Oct: 0o0

assert_eq!(byte.to_string(), "0x00");
Decrement at Boundary
use brainfoamkit_lib::Byte;

let mut byte = Byte::from(0);  // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0

byte.decrement();

assert_eq!(u8::from(&byte), 0b11111111); // Byte: 0b11111111; Dec: 255; Hex: 0xFF; Oct: 0o377
assert_eq!(byte.to_string(), "0xFF");
Side Effects

This method will decrement the Byte by one.

See Also
  • increment(): Increment the Byte by one.
  • flip(): Flip all of the Bit values in the Byte.
source

pub const fn iter(&self) -> IterableByte<'_>

Create an iterator over the Byte. This allows the use of the for loop on the Byte.

Examples
use brainfoamkit_lib::Byte;

let byte = Byte::from(0b11001010); // Dec: 10; Hex: 0xA; Oct: 0o12

for bit in byte.iter() {
    println!("{}", bit);
}

Trait Implementations§

source§

impl BitAnd for Byte

source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the Bitwise And operation on the Byte.

This method is used to perform the Bitwise And operation on the Byte. This also allows the use of the & operator on the Byte.

Arguments
  • rhs - The right hand side of the BitAnd operation.
Examples
use brainfoamkit_lib::Byte;

let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252

assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(byte.to_string(), "0xAA");

byte = byte & Byte::from(0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125

assert_eq!(u8::from(&byte), 0b00000000); // Dec: 0; Hex: 0x00; Oct: 0o0
assert_eq!(byte.to_string(), "0x00");
Returns

A Byte containing the result of a bitwise AND operation on the two Bytes.

See Also
  • bitor(): Perform a Bitwise Or operation on the Byte.
  • bitxor(): Perform a Bitwise Xor operation on the Byte.
  • bitand_assign(): Perform a Bitwise And Assignment operation on the Byte.
  • bitor_assign(): Perform a Bitwise Or Assignment operation on the Byte.
  • bitxor_assign(): Perform a Bitwise Xor Assignment operation on the Byte.
§

type Output = Byte

The resulting type after applying the & operator.
source§

impl BitAndAssign for Byte

source§

fn bitand_assign(&mut self, rhs: Self)

Performs the Bitwise And Assignment operation on the Byte.

This method is used to perform the Bitwise And Assignment operation on the Byte. This also allows the use of the &= operator on the Byte.

Arguments
  • rhs - The right hand side of the BitAnd Assignment operation.
Examples
use brainfoamkit_lib::Byte;

let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252

assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252

byte &= Byte::from(0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125

assert_eq!(u8::from(&byte), 0); // Dec: 0; Hex: 0x00; Oct: 0o0
Side Effects

This method performs a bitwise AND operation on the two Bytes, storing the result in the first Byte.

See Also
  • bitand(): Perform a Bitwise And operation on the Byte.
  • bitor(): Perform a Bitwise Or operation on the Byte.
  • bitxor(): Perform a Bitwise Xor operation on the Byte.
  • bitor_assign(): Perform a Bitwise Or Assignment operation on the Byte.
  • bitxor_assign(): Perform a Bitwise Xor Assignment operation on the Byte.
source§

impl BitOr for Byte

source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the Bitwise Or operation on the Byte.

This method is used to perform the Bitwise Or operation on the Byte. This also allows the use of the | operator on the Byte.

Arguments
  • rhs - The right hand side of the Bitwise Or operation.
Examples
use brainfoamkit_lib::Byte;

let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252

assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(byte.to_string(), "0xAA");

byte = byte | Byte::from(0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125

assert_eq!(u8::from(&byte), 0b11111111); // Dec: 255; Hex: 0xFF; Oct: 0o377
assert_eq!(byte.to_string(), "0xFF");
Returns

A Byte containing the result of a Bitwise Or operation on the two Bytes.

See Also
  • bitand(): Perform a Bitwise And operation on the Byte.
  • bitxor(): Perform a Bitwise Xor operation on the Byte.
  • bitand_assign(): Perform a Bitwise And Assignment operation on the Byte.
  • bitor_assign(): Perform a Bitwise Or Assignment operation on the Byte.
  • bitxor_assign(): Perform a Bitwise Xor Assignment operation on the Byte.
§

type Output = Byte

The resulting type after applying the | operator.
source§

impl BitOrAssign for Byte

source§

fn bitor_assign(&mut self, rhs: Self)

Performs the Bitwise Or Assignment operation on the Byte.

This method is used to perform the Bitwise Or Assignment operation on the Byte. This also allows the use of the |= operator on the Byte.

Arguments
  • rhs - The right hand side of the Bitwise Or Assignment operation.
Examples
use brainfoamkit_lib::Byte;

let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252

assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252

byte |= Byte::from(0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125

assert_eq!(u8::from(&byte), 0b11111111); // Dec: 255; Hex: 0xFF; Oct: 0o377
Side Effects

This method performs a Bitwise Or operation on the two Bytes, storing the result in the first Byte.

See Also
  • bitand(): Perform a Bitwise And operation on the Byte.
  • bitor(): Perform a Bitwise Or operation on the Byte.
  • bitxor(): Perform a Bitwise Xor operation on the Byte.
  • bitand_assign(): Perform a Bitwise And Assignment operation on the Byte.
  • bitxor_assign(): Perform a Bitwise Xor Assignment operation on the Byte.
source§

impl BitXor for Byte

source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the Bitwise Xor operation on the Byte.

This method is used to perform the Bitwise Xor operation on the Byte. This also allows the use of the ^ operator on the Byte.

Arguments
  • rhs - The right hand side of the Bitwise Xor operation.
Examples
use brainfoamkit_lib::Byte;

let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252

assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252

byte = byte ^ Byte::from(0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125

assert_eq!(u8::from(&byte), 0b11111111); // Dec: 255; Hex: 0xFF; Oct: 0o377
Returns

A Byte containing the result of a Bitwise Xor operation on the two Bytes.

See Also
  • bitand(): Perform a Bitwise And operation on the Byte.
  • bitor(): Perform a Bitwise Or operation on the Byte.
  • bitand_assign(): Perform a Bitwise And Assignment operation on the Byte.
  • bitor_assign(): Perform a Bitwise Or Assignment operation on the Byte.
  • bitxor_assign(): Perform a Bitwise Xor Assignment operation on the Byte.
§

type Output = Byte

The resulting type after applying the ^ operator.
source§

impl BitXorAssign for Byte

source§

fn bitxor_assign(&mut self, rhs: Self)

Performs the Bitwise Xor Assignment operation on the Byte.

This method is used to perform the Bitwise Xor Assignment operation on the Byte. This also allows the use of the ^= operator on the Byte.

Arguments
  • rhs - The right hand side of the Bitwise Xor Assignment operation.
Examples
use brainfoamkit_lib::Byte;

let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252

assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252

byte ^= Byte::from(0b01010101);

assert_eq!(u8::from(&byte), 0b11111111); // Dec: 255; Hex: 0xFF; Oct: 0o377
Side Effects

This method performs a Bitwise Xor operation on the two Bytes, storing the result in the first Byte.

See Also
  • bitand(): Perform a Bitwise And operation on the Byte.
  • bitor(): Perform a Bitwise Or operation on the Byte.
  • bitxor(): Perform a Bitwise Xor operation on the Byte.
  • bitand_assign(): Perform a Bitwise And Assignment operation on the Byte.
  • bitor_assign(): Perform a Bitwise Or Assignment operation on the Byte.
source§

impl Clone for Byte

source§

fn clone(&self) -> Byte

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Byte

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Byte

source§

fn default() -> Self

Creates a new Byte with all bits set to zero.

Examples
use brainfoamkit_lib::Byte;

let byte = Byte::default(); // Byte: 0b00000000; Dec: 0; Hex: 0x00; Oct: 0o0
assert_eq!(u8::from(&byte), 0b00000000); // Dec: 0; Hex: 0x00; Oct: 0o0
assert_eq!(byte.to_string(), "0x00");
Returns

A Byte with all bits set to zero.

See Also
  • new(): Create a new Byte from individual Bit values.
  • from_nybbles(): Create a new Byte from two Nybbles.
  • from_u8(): Create a new Byte from a u8.
  • to_u8(): Convert the Byte to a u8.
source§

impl Display for Byte

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Converts the Byte to a String.

Examples
use brainfoamkit_lib::Byte;

let byte = Byte::from(0xAA); // Byte: 0b10101010; Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(byte.to_string(), "0xAA");
Returns

A String containing the value of the Byte.

See Also
source§

impl From<&Byte> for u8

source§

fn from(byte: &Byte) -> Self

Converts the Byte to an 8-bit unsigned integer (u8).

This method returns the value of the Byte as an 8-bit unsigned integer (u8).

Examples
use brainfoamkit_lib::{
    Bit,
    Byte,
};

let byte = Byte::new(
    Bit::one(),
    Bit::zero(),
    Bit::one(),
    Bit::zero(),
    Bit::one(),
    Bit::zero(),
    Bit::one(),
    Bit::zero(),
);
assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
Returns

An 8-bit unsigned integer (u8) containing the value of the Byte.

See Also
source§

impl From<u8> for Byte

source§

fn from(n: u8) -> Self

Creates a new Byte from a u8.

This method creates a new Byte from a u8.

Arguments
  • n - The u8 to create the Byte from.
Examples
use brainfoamkit_lib::Byte;

let byte = Byte::from(0xAA); // Byte: 0b10101010; Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(byte.to_string(), "0xAA");
Returns

A Byte containing the value of the u8.

See Also
  • to_u8(): Convert the Byte to a u8.
  • from_nybbles(): Create a new Byte from two Nybbles.
  • new(): Create a new Byte from individual Bit values.
source§

impl Hash for Byte

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a> IntoIterator for &'a Byte

IntoIterator implementation for a reference to a Byte.

This implementation allows a Byte reference to be converted into an iterator. The iterator will yield Bit items.

§

type IntoIter = IterableByte<'a>

The type of the iterator that will be returned. It’s an IterableByte with the same lifetime as the Byte reference.

§

type Item = Bit

The type of the items that will be returned when iterating over the Byte reference. In this case, it’s a Bit.

source§

fn into_iter(self) -> Self::IntoIter

Converts the Byte reference into an IterableByte iterator.

Returns

An IterableByte iterator with the same lifetime as the Byte reference.

source§

impl Not for Byte

source§

fn not(self) -> Self::Output

Performs the Not operation on the Byte.

This method is used to perform the Not operation on the Byte. This also allows the use of the ! operator on the Byte.

Examples
use brainfoamkit_lib::Byte;

let mut byte = Byte::from(0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252

assert_eq!(u8::from(&byte), 0b10101010); // Dec: 170; Hex: 0xAA; Oct: 0o252
assert_eq!(byte.to_string(), "0xAA");

byte = !byte;

assert_eq!(u8::from(&byte), 0b01010101); // Dec: 85; Hex: 0x55; Oct: 0o125
assert_eq!(byte.to_string(), "0x55");
Returns

This method inverts the Bit values in the Byte.

See Also
  • flip_bit(): Flip the Bit value at the specified index.
  • flip(): Flip all of the Bit values in the Byte.
§

type Output = Byte

The resulting type after applying the ! operator.
source§

impl PartialEq for Byte

source§

fn eq(&self, other: &Byte) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Byte

source§

impl Eq for Byte

source§

impl StructuralEq for Byte

source§

impl StructuralPartialEq for Byte

Auto Trait Implementations§

§

impl RefUnwindSafe for Byte

§

impl Send for Byte

§

impl Sync for Byte

§

impl Unpin for Byte

§

impl UnwindSafe for Byte

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.