Struct brainfoamkit_lib::Nybble
source · pub struct Nybble { /* private fields */ }
Expand description
A Nybble is a 4-bit unsigned integer (u4).
This is a wrapper around four Bit instances. The least significant bit is
bit_0
and the most significant bit is bit_3
. This struct is used to
conveniently manipulate 4-bit values.
Note that bit instances are stored in reverse (LSB is first, MSB is last)
order, so the least significant bit is bit_0
and the most significant bit
is bit_3
. However, the new
method takes the bits in the
correct (MSB is first, LSB is last) order.
Examples
Create a Nybble from primitive Bit values
use brainfoamkit_lib::{
Bit,
Nybble,
};
let nybble = Nybble::new(Bit::One, Bit::Zero, Bit::One, Bit::Zero);
assert_eq!(u8::from(&nybble), 0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(nybble.to_string(), "0xA");
Create a Nybble from a u8 value
use brainfoamkit_lib::Nybble;
let nybble = Nybble::from(0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(u8::from(&nybble), 0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(nybble.to_string(), "0xA");
Set and Unset bits to generate the desired Nybble
use brainfoamkit_lib::Nybble;
let mut nybble = Nybble::default();
nybble.set_bit(0); // Nybble: 0b0001; Dec: 1; Hex: 0x1; Oct: 0o1
nybble.set_bit(2); // Nybble: 0b0101; Dec: 5; Hex: 0x5; Oct: 0o5
assert_eq!(u8::from(&nybble), 0b0101); // Dec: 5; Hex: 0x5; Oct: 0o5
assert_eq!(nybble.to_string(), "0x5");
Flip the bits at a given index
use brainfoamkit_lib::Nybble;
let mut nybble = Nybble::from(0b0101); // Dec: 5; Hex: 0x5; Oct: 0o5
nybble.flip_bit(0); // Nybble: 0b0100; Dec: 4; Hex: 0x4; Oct: 0o4
nybble.flip_bit(1); // Nybble: 0b0110; Dec: 6; Hex: 0x6; Oct: 0o6
assert_eq!(u8::from(&nybble), 0b0110); // Dec: 6; Hex: 0x6; Oct: 0o6
assert_eq!(nybble.to_string(), "0x6");
Panics
The methods set_bit()
,
unset_bit()
and get_bit()
will
panic if the index is out of bounds.
See Also
Implementations§
source§impl Nybble
impl Nybble
sourcepub const fn new(first: Bit, second: Bit, third: Bit, fourth: Bit) -> Self
pub const fn new(first: Bit, second: Bit, third: Bit, fourth: Bit) -> Self
Creates a new Nybble instance with the specified Bit values.
This method takes four Bit instances as arguments.
The least significant bit is bit_0
and the most significant bit is
bit_3
.
Arguments
first
- The most significant bit.second
- The second most significant bit.third
- The second least significant bit.fourth
- The least significant bit.
Examples
use brainfoamkit_lib::{
Bit,
Nybble,
};
let nybble = Nybble::new(Bit::One, Bit::Zero, Bit::One, Bit::Zero);
assert_eq!(u8::from(&nybble), 10);
assert_eq!(nybble.to_string(), "0xA");
Returns
A new Nybble with the specified Bit values.
See Also
sourcepub fn set_bit(&mut self, index: u8)
pub fn set_bit(&mut self, index: u8)
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 3.
Arguments
index
- The index of the Bit value to set.
Examples
use brainfoamkit_lib::{
Bit,
Nybble,
};
let mut nybble = Nybble::default();
nybble.set_bit(0);
nybble.set_bit(2);
assert_eq!(u8::from(&nybble), 0b0101); // Dec: 5; Hex: 0x5; Oct: 0o5
assert_eq!(nybble.to_string(), "0x5");
Side Effects
This method will set the Bit value at the specified index.
Panics
This method will panic if the index is out of bounds.
See Also
unset_bit()
: Unsets the Bit value at the specified index.get_bit()
: Gets the Bit value at the specified index.flip_bit()
: Flips the Bit value at the specified index.
sourcepub fn unset_bit(&mut self, index: u8)
pub fn unset_bit(&mut self, index: u8)
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 3.
Arguments
index
- The index of the Bit value to unset.
Examples
use brainfoamkit_lib::Nybble;
use brainfoamkit_lib::Bit;
let mut nybble = Nybble::default(); // Nybble: 0b0000; Dec: 0; Hex: 0x0; Oct: 0o0
nybble.set_bit(0); // Nybble: 0b0001; Dec: 1; Hex: 0x1; Oct: 0o1
nybble.set_bit(2); // Nybble: 0b0101; Dec: 5; Hex: 0x5; Oct: 0o5
assert_eq!(u8::from(&nybble), 0b0101); // Dec: 5; Hex: 0x5; Oct: 0o5
assert_eq!(nybble.to_string(), "0x5");
nybble.unset_bit(0); // Nybble: 0b0100; Dec: 4; Hex: 0x4; Oct: 0o4
assert_eq!(u8::from(&nybble), 4); // Dec: 4; Hex: 0x4; Oct: 0o4
assert_eq!(nybble.to_string(), "0x4");
Side Effects
This method will unset the Bit value at the specified index.
Panics
This method will panic if the index is out of bounds.
See Also
set_bit()
: Sets the Bit value at the specified index.get_bit()
: Gets the Bit value at the specified index.flip_bit()
: Flips 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.
This method is used to get the bit value at a given index.
The index is zero-based, so the least significant bit is at index 0 and the most significant bit is at index 3.
Arguments
index
- The index of the Bit value to get.
Examples
use brainfoamkit_lib::Nybble;
use brainfoamkit_lib::Bit;
let nybble = Nybble::new(Bit::One, Bit::Zero, Bit::One, Bit::Zero); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(nybble.get_bit(0), Bit::Zero);
assert_eq!(nybble.get_bit(1), Bit::One);
assert_eq!(nybble.get_bit(2), Bit::Zero);
assert_eq!(nybble.get_bit(3), Bit::One);
Panics
This method will panic if the index is out of bounds.
Returns
The Bit value at the specified index.
See Also
set_bit()
: Sets the Bit value at the specified index.unset_bit()
: Unsets the Bit value at the specified index.flip_bit()
: Flips the Bit value at the specified index.
sourcepub fn get_bit_ref(&self, index: u8) -> &Bit
pub fn get_bit_ref(&self, index: u8) -> &Bit
Get a reference to the Bit value at the specified index.
This method is used to get a reference to the bit value at a given index.
The index is zero-based, so the least significant bit is at index 0 and the most significant bit is at index 3.
Arguments
index
- The index of the Bit value to get.
Examples
use brainfoamkit_lib::Nybble;
use brainfoamkit_lib::Bit;
let one = Bit::one();
let zero = Bit::zero();
let nybble = Nybble::new(Bit::One, Bit::Zero, Bit::One, Bit::Zero); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(nybble.get_bit_ref(0), &zero);
assert_eq!(nybble.get_bit_ref(1), &one);
assert_eq!(nybble.get_bit_ref(2), &zero);
assert_eq!(nybble.get_bit_ref(3), &one);
Panics
This method will panic if the index is out of bounds.
Returns
A reference to the Bit value at the specified index.
See Also
set_bit()
: Sets the Bit value at the specified index.unset_bit()
: Unsets the Bit value at the specified index.flip_bit()
: Flips 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 3.
Arguments
index
- The index of the Bit value to flip.
Examples
use brainfoamkit_lib::Nybble;
use brainfoamkit_lib::Bit;
let mut nybble = Nybble::default(); // Nybble: 0b0000; Dec: 0; Hex: 0x0; Oct: 0o0
nybble.set_bit(0); // Nybble: 0b0001; Dec: 1; Hex: 0x1; Oct: 0o1
nybble.set_bit(2); // Nybble: 0b0101; Dec: 5; Hex: 0x5; Oct: 0o5
assert_eq!(u8::from(&nybble), 0b0101); // Dec: 5; Hex: 0x5; Oct: 0o5
assert_eq!(nybble.to_string(), "0x5");
nybble.flip_bit(0); // Nybble: 0b0100; Dec: 4; Hex: 0x4; Oct: 0o4
nybble.flip_bit(1); // Nybble: 0b0110; Dec: 6; Hex: 0x6; Oct: 0o6
nybble.flip_bit(2); // Nybble: 0b0010; Dec: 2; Hex: 0x2; Oct: 0o2
nybble.flip_bit(3); // Nybble: 0b1010; Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(u8::from(&nybble), 10);
assert_eq!(nybble.to_string(), "0xA");
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()
: Sets the Bit value at the specified index.unset_bit()
: Unsets the Bit value at the specified index.get_bit()
: Gets 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 Nybble.
This method is used to flip all of the bit values in the Nybble. 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 Nybble.
Examples
use brainfoamkit_lib::Nybble;
use brainfoamkit_lib::Bit;
let mut nybble = Nybble::default(); // Nybble: 0b0000; Dec: 0; Hex: 0x0; Oct: 0o0
nybble.set_bit(0); // Nybble: 0b0001; Dec: 1; Hex: 0x1; Oct: 0o1
nybble.set_bit(2); // Nybble: 0b0101; Dec: 5; Hex: 0x5; Oct: 0o5
assert_eq!(u8::from(&nybble), 0b0101); // Dec: 5; Hex: 0x5; Oct: 0o5
assert_eq!(nybble.to_string(), "0x5");
nybble.flip(); // Nybble: 0b1010; Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(u8::from(&nybble), 0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(nybble.to_string(), "0xA");
Side Effects
This method will flip all of the Bit values in the Nybble.
See Also
flip_bit()
: Flips the Bit value at the specified index.
sourcepub fn increment(&mut self)
pub fn increment(&mut self)
Increment the Nybble with rollover overflow
This method increments the value stored in the Nybble. This has a rollover for overflow. This means that if we increment past the maximum value (15), we will go back to 0.
Examples
Regular Use
use brainfoamkit_lib::Nybble;
let mut nybble = Nybble::default(); // Nybble: 0b0000; Dec: 0; Hex: 0x0; Oct: 0o0
nybble.increment();
assert_eq!(u8::from(&nybble), 1);
assert_eq!(nybble.to_string(), "0x1");
nybble.increment();
assert_eq!(u8::from(&nybble), 2);
assert_eq!(nybble.to_string(), "0x2");
Overflow Use
use brainfoamkit_lib::Nybble;
let mut nybble = Nybble::from(15); // Nybble: 0b1111; Dec: 15; Hex: 0xF; Oct: 0o17
nybble.increment();
assert_eq!(u8::from(&nybble), 0);
assert_eq!(nybble.to_string(), "0x0");
Side Effects
This method increments the value stored in the Nybble.
See Also
decrement()
: Decrements the value stored in the Nybble.flip()
: Flips all of the Bit values in the Nybble.
sourcepub fn decrement(&mut self)
pub fn decrement(&mut self)
Decrement the Nybble with no rollover
This method decrements the value stored in the Nybble. This has no rollover for underflow. This means that if we decrement past the minimum value (0), we will stay at 0.
Examples
Regular Use
use brainfoamkit_lib::Nybble;
let mut nybble = Nybble::from(2); // Nybble: 0b0010; Dec: 2; Hex: 0x2; Oct: 0o2
nybble.decrement();
assert_eq!(u8::from(&nybble), 1);
assert_eq!(nybble.to_string(), "0x1");
nybble.decrement();
assert_eq!(u8::from(&nybble), 0);
assert_eq!(nybble.to_string(), "0x0");
Underflow Use
use brainfoamkit_lib::Nybble;
let mut nybble = Nybble::default(); // Nybble: 0b0000; Dec: 0; Hex: 0x0; Oct: 0o0
nybble.decrement();
assert_eq!(u8::from(&nybble), 0);
assert_eq!(nybble.to_string(), "0x0");
Side Effects
This method decrements the value stored in the Nybble.
See Also
increment()
: Increments the value stored in the Nybble.flip()
: Flips all of the Bit values in the Nybble.
sourcepub const fn iter(&self) -> IterableNybble<'_> ⓘ
pub const fn iter(&self) -> IterableNybble<'_> ⓘ
Create an iterator over the Nybble.
This allows the use of the for
loop on the Nybble.
Examples
use brainfoamkit_lib::Nybble;
let nybble = Nybble::from(0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
for bit in nybble.iter() {
println!("{}", bit);
}
Trait Implementations§
source§impl BitAnd for Nybble
impl BitAnd for Nybble
source§fn bitand(self, rhs: Self) -> Self::Output
fn bitand(self, rhs: Self) -> Self::Output
Perform the Bitwise And operation on two Nybbles.
This method performs the Bitwise And operation on two Nybbles.
This also allows the use of the &
operator on two Nybbles.
Arguments
rhs
- The right hand side of the Bitwise And operation.
Examples
use brainfoamkit_lib::Nybble;
let nybble_1 = Nybble::from(0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
let nybble_2 = Nybble::from(0b1100); // Dec: 12; Hex: 0xC; Oct: 0o14
assert_eq!(u8::from(&nybble_1), 0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(nybble_1.to_string(), "0xA");
assert_eq!(u8::from(&nybble_2), 0b1100); // Dec: 12; Hex: 0xC; Oct: 0o14
assert_eq!(nybble_2.to_string(), "0xC");
let nybble = nybble_1 & nybble_2;
assert_eq!(u8::from(&nybble), 0b1000); // Dec: 8; Hex: 0x8; Oct: 0o10
assert_eq!(nybble.to_string(), "0x8");
Returns
A new Nybble with the Bitwise And operation performed on the two Nybbles.
See Also
bitor()
: Performs the Bitwise Or operation on two Nybbles.bitxor()
: Performs the Bitwise Xor operation on two Nybbles.bitand_assign()
: Performs the Bitwise And operation on two Nybbles and assigns the result to the first Nybble.bitor_assign()
: Performs the Bitwise Or operation on two Nybbles and assigns the result to the first Nybble.bitxor_assign()
: Performs the Bitwise Xor operation on two Nybbles and assigns the result to the first Nybble.
source§impl BitAndAssign for Nybble
impl BitAndAssign for Nybble
source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
Perform the Bitwise And operation on two Nybbles and assigns the result to the first Nybble.
This method performs the Bitwise And operation on two Nybbles and assigns the result to the first Nybble.
Arguments
rhs
- The right hand side of the Bitwise And operation.
Examples
use brainfoamkit_lib::Nybble;
let mut nybble_1 = Nybble::from(0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
let nybble_2 = Nybble::from(0b1100); // Dec: 12; Hex: 0xC; Oct: 0o14
assert_eq!(u8::from(&nybble_1), 0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(nybble_1.to_string(), "0xA");
assert_eq!(u8::from(&nybble_2), 0b1100); // Dec: 12; Hex: 0xC; Oct: 0o14
assert_eq!(nybble_2.to_string(), "0xC");
nybble_1 &= nybble_2;
assert_eq!(u8::from(&nybble_1), 0b1000); // Dec: 8; Hex: 0x8; Oct: 0o10
assert_eq!(nybble_1.to_string(), "0x8");
Side Effects
This method will perform the Bitwise And operation on two Nybbles and assign the result to the first Nybble.
See Also
bitand()
: Performs the Bitwise And operation on two Nybbles.bitor()
: Performs the Bitwise Or operation on two Nybbles.bitxor()
: Performs the Bitwise Xor operation on two Nybbles.bitor_assign()
: Performs the Bitwise Or operation on two Nybbles and assigns the result to the first Nybble.bitxor_assign()
: Performs the Bitwise Xor operation on two Nybbles and assigns the result to the first Nybble.
source§impl BitOr for Nybble
impl BitOr for Nybble
source§fn bitor(self, rhs: Self) -> Self::Output
fn bitor(self, rhs: Self) -> Self::Output
Perform the Bitwise Or operation on two Nybbles.
This method performs the Bitwise Or operation on two Nybbles.
This also allows the use of the |
operator on two Nybbles.
Arguments
rhs
- The right hand side of the Bitwise Or operation.
Examples
use brainfoamkit_lib::Nybble;
let nybble_1 = Nybble::from(0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
let nybble_2 = Nybble::from(0b1100); // Dec: 12; Hex: 0xC; Oct: 0o14
assert_eq!(u8::from(&nybble_1), 0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(nybble_1.to_string(), "0xA");
assert_eq!(u8::from(&nybble_2), 0b1100); // Dec: 12; Hex: 0xC; Oct: 0o14
let nybble = nybble_1 | nybble_2;
assert_eq!(u8::from(&nybble), 0b1110); // Dec: 14; Hex: 0xE; Oct: 0o16
assert_eq!(nybble.to_string(), "0xE");
Returns
A new Nybble with the Bitwise Or operation performed on the two Nybbles.
See Also
bitand()
: Performs the Bitwise And operation on two Nybbles.bitxor()
: Performs the Bitwise Xor operation on two Nybbles.bitand_assign()
: Performs the Bitwise And operation on two Nybbles and assigns the result to the first Nybble.bitor_assign()
: Performs the Bitwise Or operation on two Nybbles and assigns the result to the first Nybble.bitxor_assign()
: Performs the Bitwise Xor operation on two Nybbles and assigns the result to the first Nybble.
source§impl BitOrAssign for Nybble
impl BitOrAssign for Nybble
source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
Perform the Bitwise Or operation on two Nybbles and assigns the result to the first Nybble.
This method performs the Bitwise Or operation on two Nybbles and assigns
the result to the first Nybble. This also allows the use of the |=
operator on two Nybbles.
Arguments
rhs
- The right hand side of the Bitwise Or operation.
Examples
use brainfoamkit_lib::Nybble;
let mut nybble_1 = Nybble::from(0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
let nybble_2 = Nybble::from(0b1100); // Dec: 12; Hex: 0xC; Oct: 0o14
assert_eq!(u8::from(&nybble_1), 0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(nybble_1.to_string(), "0xA");
assert_eq!(u8::from(&nybble_2), 0b1100); // Dec: 12; Hex: 0xC; Oct: 0o14
assert_eq!(nybble_2.to_string(), "0xC");
nybble_1 |= nybble_2;
assert_eq!(u8::from(&nybble_1), 0b1110); // Dec: 14; Hex: 0xE; Oct: 0o16
Side Effects
This method will perform the Bitwise Or operation on two Nybbles and assign the result to the first Nybble.
See Also
bitand()
: Performs the Bitwise And operation on two Nybbles.bitor()
: Performs the Bitwise Or operation on two Nybbles.bitxor()
: Performs the Bitwise Xor operation on two Nybbles.bitand_assign()
: Performs the Bitwise And operation on two Nybbles and assigns the result to the first Nybble.bitxor_assign()
: Performs the Bitwise Xor operation on two Nybbles and assigns the result to the first Nybble.
source§impl BitXor for Nybble
impl BitXor for Nybble
source§fn bitxor(self, rhs: Self) -> Self::Output
fn bitxor(self, rhs: Self) -> Self::Output
Perform the Bitwise Xor operation on two Nybbles.
This method performs the Bitwise Xor operation on two Nybbles.
This also allows the use of the ^
operator on two Nybbles.
Arguments
rhs
- The right hand side of the Bitwise Xor operation.
Examples
use brainfoamkit_lib::Nybble;
let nybble_1 = Nybble::from(0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
let nybble_2 = Nybble::from(0b1100); // Dec: 12; Hex: 0xC; Oct: 0o14
assert_eq!(u8::from(&nybble_1), 0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(nybble_1.to_string(), "0xA");
assert_eq!(u8::from(&nybble_2), 0b1100); // Dec: 12; Hex: 0xC; Oct: 0o14
let nybble = nybble_1 ^ nybble_2;
assert_eq!(u8::from(&nybble), 0b0110); // Dec: 6; Hex: 0x6; Oct: 0o6
Returns
A new Nybble with the Bitwise Xor operation performed on the two Nybbles.
See Also
bitand()
: Performs the Bitwise And operation on two Nybbles.bitor()
: Performs the Bitwise Or operation on two Nybbles.bitand_assign()
: Performs the Bitwise And operation on two Nybbles and assigns the result to the first Nybble.bitor_assign()
: Performs the Bitwise Or operation on two Nybbles and assigns the result to the first Nybble.bitxor_assign()
: Performs the Bitwise Xor operation on two Nybbles and assigns the result to the first Nybble.
source§impl BitXorAssign for Nybble
impl BitXorAssign for Nybble
source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
Perform the Bitwise Xor operation on two Nybbles and assigns the result to the first Nybble.
This method performs the Bitwise Xor operation on two Nybbles and
assigns the result to the first Nybble. This also allows the use of
the ^=
operator on two Nybbles.
Arguments
rhs
- The right hand side of the Bitwise Xor operation.
Examples
use brainfoamkit_lib::Nybble;
let mut nybble_1 = Nybble::from(0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
let nybble_2 = Nybble::from(0b1100); // Dec: 12; Hex: 0xC; Oct: 0o14
assert_eq!(u8::from(&nybble_1), 0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(nybble_1.to_string(), "0xA");
assert_eq!(u8::from(&nybble_2), 0b1100); // Dec: 12; Hex: 0xC; Oct: 0o14
assert_eq!(nybble_2.to_string(), "0xC");
nybble_1 ^= nybble_2;
assert_eq!(u8::from(&nybble_1), 0b0110); // Dec: 6; Hex: 0x6; Oct: 0o6
Side Effects
This method will perform the Bitwise Xor operation on two Nybbles and assign the result to the first Nybble.
See Also
bitand()
: Performs the Bitwise And operation on two Nybbles.bitor()
: Performs the Bitwise Or operation on two Nybbles.bitxor()
: Performs the Bitwise Xor operation on two Nybbles.bitand_assign()
: Performs the Bitwise And operation on two Nybbles and assigns the result to the first Nybble.bitor_assign()
: Performs the Bitwise Or operation on two Nybbles and assigns the result to the first Nybble.
source§impl Default for Nybble
impl Default for Nybble
source§fn default() -> Self
fn default() -> Self
Create a default (empty) Nybble.
Creates a new Nybble with default (all Bit::Zero
)
Bit values.
Examples
use brainfoamkit_lib::Nybble;
let nybble = Nybble::default();
assert_eq!(u8::from(&nybble), 0b0000); // Nybble: 0b0000; Dec: 0; Hex: 0x0; Oct: 0o0
assert_eq!(nybble.to_string(), "0x0");
Returns
A new Nybble with default (all Bit::Zero
) Bit
values.
See Also
source§impl Display for Nybble
impl Display for Nybble
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
source§impl From<&Nybble> for u8
impl From<&Nybble> for u8
source§fn from(nybble: &Nybble) -> Self
fn from(nybble: &Nybble) -> Self
Converts the Nybble to an 8-bit unsigned integer (u8).
This method converts the Nybble to an 8-bit unsigned integer (u8).
Examples
use brainfoamkit_lib::Nybble;
use brainfoamkit_lib::Bit;
let nybble = Nybble::new(Bit::One, Bit::Zero, Bit::One, Bit::Zero); // Dec: 10; Hex: 0xA; Oct: 0o12
let result = u8::from(&nybble); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(result, 0b1010);
Returns
The Nybble as an 8-bit unsigned integer (u8).
See Also
from_u8()
: Creates a new Nybble from a u8 value.to_string()
: Converts the Nybble to a string.
source§impl From<u8> for Nybble
impl From<u8> for Nybble
source§fn from(n: u8) -> Self
fn from(n: u8) -> Self
Creates a new Nybble from a u8 value.
This method takes a u8 value as an argument and creates a new Nybble truncating to only the least significant four bits.
Arguments
n
- The u8 value to create the Nybble from.
Examples
A valid value for a Nybble
use brainfoamkit_lib::Nybble;
let nybble = Nybble::from(5);
assert_eq!(u8::from(&nybble), 5);
assert_eq!(nybble.to_string(), "0x5");
A value too large for a Nybble
use brainfoamkit_lib::Nybble;
let nybble = Nybble::from(16);
assert_eq!(u8::from(&nybble), 0);
assert_eq!(nybble.to_string(), "0x0");
Returns
A new Nybble from the specified u8 value, or an all
Bit::One
Nybble if the value is larger than 15.
See Also
source§impl<'a> IntoIterator for &'a Nybble
impl<'a> IntoIterator for &'a Nybble
IntoIterator
implementation for a reference to a Nybble
.
This implementation allows a Nybble
reference to be converted into an
iterator. The iterator will yield Bit
items.
source§impl Not for Nybble
impl Not for Nybble
source§fn not(self) -> Self::Output
fn not(self) -> Self::Output
Perform the Not operation on the Nybble.
This method performs the Not operation on the Nybble.
This also allows the use of the !
operator on the Nybble.
Examples
use brainfoamkit_lib::Nybble;
let nybble = Nybble::from(0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(u8::from(&nybble), 0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12
assert_eq!(nybble.to_string(), "0xA");
let nybble = !nybble;
assert_eq!(u8::from(&nybble), 0b0101); // Dec: 5; Hex: 0x5; Oct: 0o5
assert_eq!(nybble.to_string(), "0x5");
Returns
A new Nybble with the Bit values flipped.
See Also
flip()
: Flips all of the Bit values in the Nybble.flip_bit()
: Flips the Bit value at the specified index.bitand()
: Performs the Bitwise And operation on two Nybbles.bitor()
: Performs the Bitwise Or operation on two Nybbles.bitxor()
: Performs the Bitwise Xor operation on two Nybbles.bitand_assign()
: Performs the Bitwise And operation on two Nybbles and assigns the result to the first Nybble.bitor_assign()
: Performs the Bitwise Or operation on two Nybbles and assigns the result to the first Nybble.bitxor_assign()
: Performs the Bitwise Xor operation on two Nybbles and assigns the result to the first Nybble.