Class Bit
- java.lang.Object
-
- Bit
-
- All Implemented Interfaces:
Comparable<Bit>
public class Bit extends Object implements Comparable<Bit>
A binary bit whose value is 0 or 1.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Bit
and(Bit other)
Sets this bit to the logical AND of this bit and the other bit.int
compareTo(Bit other)
Compares this bit to another bit for order.boolean
equals(Object obj)
Compares this bit to an object for equality.int
hashCode()
Returns a hash code for this bit;Bit
not()
Sets the value of this bit to 0 if the current value of this bit is 1, or sets the value of this bit to 1 if the current value of this bit is 0.Bit
or(Bit other)
Sets this bit to the logical OR of this bit and the other bit.Bit
set(boolean bit)
Sets this bit to 1 ifbit
is true, 0 otherwise.Bit
set(int bit)
Sets this bit the specified value.String
toString()
Returns the string representation of this bit ("0" or "1").int
value()
Get the value of this bit (0 or 1)
-
-
-
Constructor Detail
-
Bit
public Bit(boolean bit)
Initialize this bit to 1 ifbit
is true, and 0 otherwise.- Parameters:
bit
- the value of the bit
-
Bit
public Bit(int bit)
Initialize this bit to the specified value.- Parameters:
bit
- the value of this bit- Throws:
IllegalArgumentException
- if bit is not equal to 0 or not equal to 1
-
Bit
public Bit(Bit other)
Initialize this bit by copying another bit.- Parameters:
other
- the bit to copy
-
-
Method Detail
-
value
public int value()
Get the value of this bit (0 or 1)- Returns:
- the value of this bit
-
set
public final Bit set(boolean bit)
Sets this bit to 1 ifbit
is true, 0 otherwise.- Parameters:
bit
- the value of this bit- Returns:
- a reference to this Bit
-
set
public final Bit set(int bit)
Sets this bit the specified value.- Parameters:
bit
- the value of this bit- Returns:
- a reference to this Bit
- Throws:
IllegalArgumentException
- if bit is not equal to 0 or not equal to 1
-
not
public Bit not()
Sets the value of this bit to 0 if the current value of this bit is 1, or sets the value of this bit to 1 if the current value of this bit is 0.- Returns:
- a reference to this bit
-
and
public Bit and(Bit other)
Sets this bit to the logical AND of this bit and the other bit. Sets this bit to 1 if this bit and the other bit values are both 1, otherwise sets this bit to 0.- Parameters:
other
- another bit- Returns:
- a reference to this bit
-
or
public Bit or(Bit other)
Sets this bit to the logical OR of this bit and the other bit. Sets this bit to 0 if this bit and the other bit values are both 0, otherwise sets this bit to 1.- Parameters:
other
- another bit- Returns:
- a reference to this bit
-
hashCode
public int hashCode()
Returns a hash code for this bit;
-
equals
public boolean equals(Object obj)
Compares this bit to an object for equality. Returns true if and only ifobj
is aBit
with the same value as this bit.
-
compareTo
public int compareTo(Bit other)
Compares this bit to another bit for order. Returns 1 if this bit is 1 and the other bit is 0, -1 if this bit is 0 and the other bit is 1, and 0 otherwise.- Specified by:
compareTo
in interfaceComparable<Bit>
- Returns:
- 1, -1, or 0 if this bit is greater than, less than, or equal to the other bit
-
-