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

  • Bit: The building block of a Nybble.
  • Byte: A Byte is a collection of eight Bits.

Implementations§

source§

impl Nybble

source

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
source

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.
source

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.
source

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.
source

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.
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 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.
source

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.
source

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.
source

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.
source

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

source§

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.
§

type Output = Nybble

The resulting type after applying the & operator.
source§

impl BitAndAssign for Nybble

source§

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

source§

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.
§

type Output = Nybble

The resulting type after applying the | operator.
source§

impl BitOrAssign for Nybble

source§

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

source§

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.
§

type Output = Nybble

The resulting type after applying the ^ operator.
source§

impl BitXorAssign for Nybble

source§

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 Clone for Nybble

source§

fn clone(&self) -> Nybble

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 Nybble

source§

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

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

impl Default for Nybble

source§

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
  • new(): Creates a new Nybble with the specified Bit values.
  • from_u8(): Creates a new Nybble from a u8 value.
  • to_u8(): Converts the Nybble to an 8-bit unsigned integer (u8).
source§

impl Display for Nybble

source§

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

Converts the Nybble to a string.

This method converts the Nybble to a string.

Examples
use brainfoamkit_lib::Nybble;

let nybble = Nybble::from(0b1010); // Dec: 10; Hex: 0xA; Oct: 0o12

assert_eq!(nybble.to_string(), "0xA");
Returns

The Nybble as a string.

See Also
  • to_u8(): Converts the Nybble to an 8-bit unsigned integer (u8).
  • from_u8(): Creates a new Nybble from a u8 value.
source§

impl From<&Nybble> for u8

source§

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
source§

impl From<u8> for Nybble

source§

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
  • new(): Creates a new Nybble with the specified Bit values.
  • default(): Creates a new Nybble with default (all Bit::Zero) Bit values.
source§

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.

§

type IntoIter = IterableNybble<'a>

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

§

type Item = Bit

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

source§

fn into_iter(self) -> Self::IntoIter

Converts the Nybble reference into an IterableNybble iterator.

Returns

An IterableNybble iterator with the same lifetime as the Nybble reference.

source§

impl Not for Nybble

source§

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.
§

type Output = Nybble

The resulting type after applying the ! operator.
source§

impl PartialEq for Nybble

source§

fn eq(&self, other: &Nybble) -> 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 Nybble

source§

impl Eq for Nybble

source§

impl StructuralEq for Nybble

source§

impl StructuralPartialEq for Nybble

Auto Trait Implementations§

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.