pub enum Bit {
    Zero,
    One,
}
Expand description

Representation of a single bit.

This Enum is the most basic building block of the BrainfoamKit library. This encodes a single bit, which can be either a 0 or a 1. I have implemented this as an Enum to ensure that the only possible values are 0 and 1. Additionally, the variants are not public and can only be accessed through the Bit::zero() and Bit::one() constructor functions.

Examples

Create with helper functions

use brainfoamkit_lib::Bit;

let bit = Bit::zero();
assert_eq!(bit, Bit::Zero);
let bit = Bit::one();
assert_eq!(bit, Bit::One);

Perform basic operations

use brainfoamkit_lib::Bit;

let mut bit = Bit::zero();
bit.flip();
assert_eq!(bit, Bit::One);
let mut bit = Bit::one();
bit.flip();
assert_eq!(bit, Bit::Zero);

Display the value

use brainfoamkit_lib::Bit;

let bit = Bit::zero();
assert_eq!(format!("{}", bit), "0");
let bit = Bit::one();
assert_eq!(format!("{}", bit), "1");

Convert to a u8

use brainfoamkit_lib::Bit;

let bit = Bit::zero();
assert_eq!(u8::from(bit), 0);
let bit = Bit::one();
assert_eq!(u8::from(bit), 1);

Perform logical operations

use brainfoamkit_lib::Bit;

let bit = Bit::zero() & Bit::zero();
assert_eq!(bit, Bit::Zero);
let bit = Bit::zero() | Bit::one();
assert_eq!(bit, Bit::One);
let bit = Bit::one() ^ Bit::one();
assert_eq!(bit, Bit::Zero);

See Also

  • Nybble: A 4-bit value composed of 4 Bits.
  • Byte: An 8-bit value composed of 8 Bits.

Variants§

§

Zero

The zero variant of the Bit Enum. Represents the value 0 or the Off state.

§

One

The one variant of the Bit Enum. Represents the value 1 or the On state.

Implementations§

source§

impl Bit

source

pub const fn zero() -> Self

Constructs a new Bit with the value 0.

This function returns a value of the Bit Enum with the Bit::Zero variant.

Examples
use brainfoamkit_lib::Bit;

let bit = Bit::zero();
assert_eq!(bit, Bit::Zero);
assert_eq!(bit.to_string(), "0");
Returns

A new Bit with the Bit::Zero variant.

See Also
  • Bit::one(): Constructs a new Bit with the value 1.
source

pub const fn one() -> Self

Constructs a new Bit with the value 1.

This function returns a value of the Bit Enum with the Bit::One variant.

Examples
use brainfoamkit_lib::Bit;

let bit = Bit::one();
assert_eq!(bit, Bit::One);
assert_eq!(bit.to_string(), "1");
Returns

A new Bit with the Bit::One variant.

See Also
source

pub fn flip(&mut self)

Flips the value of the Bit.

This function flips the value of the Bit. This means that if the Bit is Bit::Zero it will become Bit::One and vice versa.

Examples
use brainfoamkit_lib::Bit;

let mut bit = Bit::zero();
bit.flip();
assert_eq!(bit, Bit::One);
let mut bit = Bit::one();
bit.flip();
assert_eq!(bit, Bit::Zero);
Side Effects

The value of the Bit is flipped.

See Also
source

pub fn set(&mut self)

Set the bit

This function sets the bit. This means that the value of the bit is set to 1. This function ignores the current value of the bit.

Examples
use brainfoamkit_lib::Bit;

let mut bit = Bit::zero();
bit.set();
assert_eq!(bit, Bit::One);
Side Effects

The value of the Bit is set to 1.

Notes

This is equivalent to calling bit.flip() on a Bit::Zero.

See Also
source

pub fn unset(&mut self)

Unset the bit

This function unsets the bit. This means that the value of the bit is set to 0. This function ignores the current value of the bit.

Examples
use brainfoamkit_lib::Bit;

let mut bit = Bit::one();
bit.unset();
assert_eq!(bit, Bit::Zero);
Side Effects

The value of the Bit is set to 0.

Notes

This is equivalent to calling bit.flip() on a Bit::One.

See Also
source

pub fn is_set(&self) -> bool

Check if the bit is set

This function checks if the bit is set (i.e. has the value of Bit::One).

Examples
use brainfoamkit_lib::Bit;

let bit = Bit::one();
assert!(bit.is_set());
Returns

A boolean indicating if the bit is set.

See Also
source

pub fn is_unset(&self) -> bool

Check if the bit is unset

This function checks if the bit is unset (i.e. has the value of Bit::Zero).

Examples
use brainfoamkit_lib::Bit;

let bit = Bit::zero();
assert!(bit.is_unset());
Returns

A boolean indicating if the bit is unset.

See Also

Trait Implementations§

source§

impl BitAnd for Bit

source§

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

Perform a logical AND on the Bit.

This function performs a logical AND on the Bit. This means that if both of the Bits are Bit::One the result will be Bit::One, otherwise the result will be Bit::Zero. This function also allows you to use the & operator on the Bit.

Arguments
  • rhs - The other Bit to perform the logical AND with.
Examples
use brainfoamkit_lib::Bit;

let bit = Bit::zero() & Bit::zero();

assert_eq!(bit, Bit::Zero);

let bit = Bit::zero() & Bit::one();

assert_eq!(bit, Bit::Zero);

let bit = Bit::one() & Bit::zero();

assert_eq!(bit, Bit::Zero);

let bit = Bit::one() & Bit::one();

assert_eq!(bit, Bit::One);
Returns

A new Bit with the value of the logical AND of the two Bits.

See Also
§

type Output = Bit

The resulting type after applying the & operator.
source§

impl BitAndAssign for Bit

source§

fn bitand_assign(&mut self, rhs: Self)

Perform a logical AND on the Bit and assign the result to the Bit.

This function performs a logical AND on the Bit and assigns the result to the Bit. This also allows you to use the &= operator on the Bit.

Arguments
  • rhs - The other Bit to perform the logical AND with.
Examples
use brainfoamkit_lib::Bit;

let mut bit = Bit::zero();

bit &= Bit::zero();
assert_eq!(bit, Bit::Zero);
Side Effects

The value of the Bit is updated to the result of the logical AND.

See Also
source§

impl BitOr for Bit

source§

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

Perform a logical OR on the Bit.

This function performs a logical OR on the Bit. This means that if either of the Bits is Bit::One the result will be Bit::One, otherwise the result will be Bit::Zero. This function also allows you to use the | operator on the Bit.

Arguments
  • rhs - The other Bit to perform the logical OR with.
Examples
use brainfoamkit_lib::Bit;

let bit = Bit::zero() | Bit::zero();
assert_eq!(bit, Bit::Zero);

let bit = Bit::zero() | Bit::one();
assert_eq!(bit, Bit::One);

let bit = Bit::one() | Bit::zero();
assert_eq!(bit, Bit::One);

let bit = Bit::one() | Bit::one();
assert_eq!(bit, Bit::One);
Returns

A new Bit with the value of the logical OR of the two Bits.

See Also
§

type Output = Bit

The resulting type after applying the | operator.
source§

impl BitOrAssign for Bit

source§

fn bitor_assign(&mut self, rhs: Self)

Perform a logical OR on the Bit and assign the result to the Bit.

This function performs a logical OR on the Bit and assigns the result to the Bit. This also allows you to use the |= operator on the Bit.

Arguments
  • rhs - The other Bit to perform the logical OR with.
Examples
use brainfoamkit_lib::Bit;

let mut bit = Bit::zero();

bit |= Bit::zero();
assert_eq!(bit, Bit::Zero);

bit |= Bit::one();
assert_eq!(bit, Bit::One);

bit |= Bit::zero();

assert_eq!(bit, Bit::One);
Side Effects

The value of the Bit is updated to the result of the logical OR.

See Also
source§

impl BitXor for Bit

source§

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

Perform a logical XOR on the Bit.

This function performs a logical XOR on the Bit. This means that if the Bits are different the result will be Bit::One, otherwise the result will be Bit::Zero. This function also allows you to use the ^ operator on the Bit.

Arguments
  • rhs - The other Bit to perform the logical XOR with.
Examples
use brainfoamkit_lib::Bit;

let bit = Bit::zero() ^ Bit::zero();

assert_eq!(bit, Bit::Zero);

let bit = Bit::zero() ^ Bit::one();

assert_eq!(bit, Bit::One);
Returns

A new Bit with the value of the logical XOR of the two Bits.

See Also
§

type Output = Bit

The resulting type after applying the ^ operator.
source§

impl BitXorAssign for Bit

source§

fn bitxor_assign(&mut self, rhs: Self)

Perform a logical XOR on the Bit and assign the result to the Bit.

This function performs a logical XOR on the Bit and assigns the result to the Bit. This also allows you to use the ^= operator on the Bit.

Arguments
  • rhs - The other Bit to perform the logical XOR with.
Examples
use brainfoamkit_lib::Bit;

let mut bit = Bit::zero();

bit ^= Bit::zero();

assert_eq!(bit, Bit::Zero);

bit ^= Bit::one();

assert_eq!(bit, Bit::One);
Side Effects

The value of the Bit is updated to the result of the logical XOR.

See Also
source§

impl Clone for Bit

source§

fn clone(&self) -> Bit

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 Bit

source§

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

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

impl Default for Bit

source§

fn default() -> Self

Create a new Bit with the value 0.

This function returns a new Bit with the value 0.

Examples
use brainfoamkit_lib::Bit;

let bit = Bit::default();
assert_eq!(bit, Bit::Zero);
Returns

A new Bit with the value 0.

See Also
source§

impl Display for Bit

source§

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

Display the value of the Bit.

This function displays the value of the Bit.

Examples
use brainfoamkit_lib::Bit;

let bit = Bit::zero();
assert_eq!(format!("{}", bit), "0");

let bit = Bit::one();
assert_eq!(format!("{}", bit), "1");
Returns

A string containing the value of the Bit.

See Also
source§

impl From<Bit> for u8

source§

fn from(bit: Bit) -> Self

Convert a Bit to a u8.

This function returns the value of the Bit as a u8.

Arguments
  • bit - The Bit to convert to a u8.
Examples
use brainfoamkit_lib::Bit;

let bit = Bit::zero();
assert_eq!(u8::from(bit), 0);

let bit = Bit::one();
assert_eq!(u8::from(bit), 1);
Returns

The value of the Bit as a u8.

See Also
source§

impl From<u8> for Bit

source§

fn from(value: u8) -> Self

Create a new Bit from a u8.

This function returns a new Bit with the value of the u8.

Arguments
  • value - The value to create the Bit from.
Examples
use brainfoamkit_lib::Bit;

let bit = Bit::from(0);
assert_eq!(bit, Bit::Zero);

let bit = Bit::from(1);
assert_eq!(bit, Bit::One);
Returns

A new Bit with the value of the u8.

See Also
source§

impl Hash for Bit

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 Not for Bit

source§

fn not(self) -> Self::Output

Perform a logical NOT on the Bit.

This function performs a logical NOT on the Bit. This means that if the Bit is Bit::Zero it will become Bit::One and vice versa. This function also allows you to use the ! operator on the Bit.

Examples
use brainfoamkit_lib::Bit;

let bit = !Bit::zero();
assert_eq!(bit, Bit::One);

let bit = !Bit::one();
assert_eq!(bit, Bit::Zero);
Returns

A new Bit with the value of the logical NOT of the Bit.

See Also
§

type Output = Bit

The resulting type after applying the ! operator.
source§

impl PartialEq for Bit

source§

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

source§

impl Eq for Bit

source§

impl StructuralEq for Bit

source§

impl StructuralPartialEq for Bit

Auto Trait Implementations§

§

impl RefUnwindSafe for Bit

§

impl Send for Bit

§

impl Sync for Bit

§

impl Unpin for Bit

§

impl UnwindSafe for Bit

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.