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
Implementations§
source§impl Byte
impl Byte
sourcepub const fn new(
zeroth: Bit,
first: Bit,
second: Bit,
third: Bit,
fourth: Bit,
fifth: Bit,
sixth: Bit,
seventh: Bit
) -> Self
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
from_u8()
: Create a new Byte from a u8.from_nybbles()
: Create a new Byte from two Nybbles.default()
: Create a new Byte with all bits set to zero.
sourcepub fn from_nybbles(high_nybble: Nybble, low_nybble: Nybble) -> Self
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
get_high_nybble()
: Get the High Nybble of the Byte.get_low_nybble()
: Get the Low Nybble of the Byte.from_u8()
: Create a new Byte from a u8.default()
: Create a new Byte with all bits set to zero.to_u8()
: Convert the Byte to a u8.new()
: Create a new Byte from individual Bit values.
sourcepub fn get_high_nybble(&self) -> Nybble
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
get_low_nybble()
: Get the Low Nybble of the Byte.from_nybbles()
: Create a new Byte from two Nybbles.
sourcepub fn get_low_nybble(&self) -> Nybble
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
get_high_nybble()
: Get the High Nybble of the Byte.from_nybbles()
: Create a new Byte from two Nybbles.
sourcepub fn set_bit(&mut self, index: usize)
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.
sourcepub fn unset_bit(&mut self, index: usize)
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.
sourcepub fn get_bit(&self, index: u8) -> Bit
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.
sourcepub fn flip_bit(&mut self, index: u8)
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.
sourcepub fn flip(&mut self)
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.
sourcepub fn increment(&mut self)
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
decrement()
: Decrement the Byte by one.flip()
: Flip all of the Bit values in the Byte.- Integer Overflow: An overview of the mathematics behind integer overflow
sourcepub fn decrement(&mut self)
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.
sourcepub const fn iter(&self) -> IterableByte<'_> ⓘ
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
impl BitAnd for Byte
source§fn bitand(self, rhs: Self) -> Self::Output
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 theBitAnd
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.
source§impl BitAndAssign for Byte
impl BitAndAssign for Byte
source§fn bitand_assign(&mut self, rhs: Self)
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 theBitAnd
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
impl BitOr for Byte
source§fn bitor(self, rhs: Self) -> Self::Output
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.
source§impl BitOrAssign for Byte
impl BitOrAssign for Byte
source§fn bitor_assign(&mut self, rhs: Self)
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
impl BitXor for Byte
source§fn bitxor(self, rhs: Self) -> Self::Output
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.
source§impl BitXorAssign for Byte
impl BitXorAssign for Byte
source§fn bitxor_assign(&mut self, rhs: Self)
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 Default for Byte
impl Default for Byte
source§fn default() -> Self
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 From<&Byte> for u8
impl From<&Byte> for u8
source§fn from(byte: &Byte) -> Self
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
to_string()
: Convert the Byte to a String.from_u8()
: Create a new Byte from a u8.
source§impl From<u8> for Byte
impl From<u8> for Byte
source§fn from(n: u8) -> Self
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<'a> IntoIterator for &'a Byte
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.
source§impl Not for Byte
impl Not for Byte
source§fn not(self) -> Self::Output
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.