Enum brainfoamkit_lib::Bit
source · 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
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
impl Bit
sourcepub const fn zero() -> Self
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.
sourcepub const fn one() -> Self
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
Bit::zero()
: Constructs a new Bit with the value 0.
sourcepub fn flip(&mut self)
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
Bit::set()
: Sets the value of the Bit to 1.Bit::unset()
: Sets the value of the Bit to 0.Bit::is_set()
: Checks if the value of the Bit is 1.Bit::is_unset()
: Checks if the value of the Bit is 0.
sourcepub fn set(&mut self)
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
Bit::flip()
: Flips the value of the Bit.Bit::unset()
: Sets the value of the Bit to 0.Bit::is_set()
: Checks if the value of the Bit is 1.Bit::is_unset()
: Checks if the value of the Bit is 0.
sourcepub fn unset(&mut self)
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
Bit::flip()
: Flips the value of the Bit.Bit::set()
: Sets the value of the Bit to 1.Bit::is_set()
: Checks if the value of the Bit is 1.Bit::is_unset()
: Checks if the value of the Bit is 0.
sourcepub fn is_set(&self) -> bool
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
Bit::is_unset()
: Checks if the bit is unset.Bit::set()
: Sets the value of the Bit to 1.Bit::unset()
: Sets the value of the Bit to 0.
sourcepub fn is_unset(&self) -> bool
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
Bit::is_set()
: Checks if the bit is set.Bit::set()
: Sets the value of the Bit to 1.Bit::unset()
: Sets the value of the Bit to 0.
Trait Implementations§
source§impl BitAnd for Bit
impl BitAnd for Bit
source§fn bitand(self, rhs: Self) -> Self::Output
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
Bit::not()
: Performs a logical NOT on the Bit.Bit::bitor()
: Performs a logical OR on the Bit.Bit::bitxor()
: Performs a logical XOR on the Bit.Bit::bitandassign()
: Performs a logical AND on the Bit and assigns the result to the Bit.Bit::bitorassign()
: Performs a logical OR on the Bit and assigns the result to the Bit.Bit::bitxorassign()
: Performs a logical XOR on the Bit and assigns the result to the Bit.
source§impl BitAndAssign for Bit
impl BitAndAssign for Bit
source§fn bitand_assign(&mut self, rhs: Self)
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
Bit::not()
: Performs a logical NOT on the Bit.Bit::bitor()
: Performs a logical OR on the Bit.Bit::bitxor()
: Performs a logical XOR on the Bit.Bit::bitandassign()
: Performs a logical AND on the Bit and assigns the result to the Bit.Bit::bitorassign()
: Performs a logical OR on the Bit and assigns the result to the Bit.Bit::bitxorassign()
: Performs a logical XOR on the Bit and assigns the result to the Bit.
source§impl BitOr for Bit
impl BitOr for Bit
source§fn bitor(self, rhs: Self) -> Self::Output
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
Bit::not()
: Performs a logical NOT on the Bit.Bit::bitand()
: Performs a logical AND on the Bit.Bit::bitxor()
: Performs a logical XOR on the Bit.Bit::bitandassign()
: Performs a logical AND on the Bit and assigns the result to the Bit.Bit::bitorassign()
: Performs a logical OR on the Bit and assigns the result to the Bit.Bit::bitxorassign()
: Performs a logical XOR on the Bit and assigns the result to the Bit.
source§impl BitOrAssign for Bit
impl BitOrAssign for Bit
source§fn bitor_assign(&mut self, rhs: Self)
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
Bit::not()
: Performs a logical NOT on the Bit.Bit::bitand()
: Performs a logical AND on the Bit.Bit::bitxor()
: Performs a logical XOR on the Bit.Bit::bitandassign()
: Performs a logical AND on the Bit and assigns the result to the Bit.Bit::bitorassign()
: Performs a logical OR on the Bit and assigns the result to the Bit.Bit::bitxorassign()
: Performs a logical XOR on the Bit and assigns the result to the Bit.
source§impl BitXor for Bit
impl BitXor for Bit
source§fn bitxor(self, rhs: Self) -> Self::Output
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
Bit::not()
: Performs a logical NOT on the Bit.Bit::bitor()
: Performs a logical OR on the Bit.Bit::bitand()
: Performs a logical AND on the Bit.Bit::bitandassign()
: Performs a logical AND on the Bit and assigns the result to the Bit.Bit::bitorassign()
: Performs a logical OR on the Bit and assigns the result to the Bit.Bit::bitxorassign()
: Performs a logical XOR on the Bit and assigns the result to the Bit.
source§impl BitXorAssign for Bit
impl BitXorAssign for Bit
source§fn bitxor_assign(&mut self, rhs: Self)
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
Bit::not()
: Performs a logical NOT on the Bit.Bit::bitor()
: Performs a logical OR on the Bit.Bit::bitand()
: Performs a logical AND on the Bit.Bit::bitxor()
: Performs a logical XOR on the Bit.Bit::bitandassign()
: Performs a logical AND on the Bit and assigns the result to the Bit.Bit::bitorassign()
: Performs a logical OR on the Bit and assigns the result to the Bit.
source§impl Default for Bit
impl Default for Bit
source§fn default() -> Self
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
Bit::zero()
: Constructs a new Bit with the value 0.Bit::one()
: Constructs a new Bit with the value 1.
source§impl Display for Bit
impl Display for Bit
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
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
Bit::to_string()
: Converts the Bit to a string.
source§impl From<Bit> for u8
impl From<Bit> for u8
source§fn from(bit: Bit) -> Self
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
Bit::to_u8()
: Converts the Bit to a u8.
source§impl From<u8> for Bit
impl From<u8> for Bit
source§fn from(value: u8) -> Self
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
Bit::zero()
: Constructs a new Bit with the value 0.Bit::one()
: Constructs a new Bit with the value 1.
source§impl Not for Bit
impl Not for Bit
source§fn not(self) -> Self::Output
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
Bit::flip()
: Flips the value of the Bit.Bit::set()
: Sets the value of the Bit to 1.Bit::unset()
: Sets the value of the Bit to 0.Bit::bitand()
: Performs a logical AND on the Bit.Bit::bitor()
: Performs a logical OR on the Bit.Bit::bitxor()
: Performs a logical XOR on the Bit.Bit::bitandassign()
: Performs a logical AND on the Bit and assigns the result to the Bit.Bit::bitorassign()
: Performs a logical OR on the Bit and assigns the result to the Bit.Bit::bitxorassign()
: Performs a logical XOR on the Bit and assigns the result to the Bit.
impl Copy for Bit
impl Eq for Bit
impl StructuralPartialEq for Bit
Auto Trait Implementations§
impl Freeze for Bit
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)