Class Binary4
- java.lang.Object
-
- Binary4
-
public class Binary4 extends Object
A class that represents a binary (base-2) number. A binary number is made up of one or more binary digits called bits. A bit has a value that is equal to either 0 or 1. An 8-bit binary number is shown below.A
Binary
number owns the individual bits.To convert a binary number to its decimal (base-10) equivalent, each bit is multiplied by an increasing power of 2 starting from the right-most bit. For example, the decimal equivalent of the 8-bit binary number can be calculated as (1 * 128) + (0 * 64) + (0 * 32) + (1 * 16) + (0 * 8) + (1 * 4) + (1 * 2) + (1 * 1) = 151:
The decimal equivalent of the 3-bit binary number can be calculated as (1 * 4) + (1 * 2) + (1 * 1) = 7:
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Bit
getBit(int index)
Gets the bit at the given index.List<Bit>
getBits()
Returns a reference to the list of bits; for testing purposes only.static void
main(String[] args)
A simple test method.int
numberOfBits()
Returns the number of bits in this binary number.List<Bit>
safeGetBits()
Returns a list of the bits of this binary number.void
setLeftmostBit(Bit bit)
Sets the leftmost bit by copying the value ofbit
into the leftmost bit.void
shiftLeft(int n)
Shifts all of the bits of this binary numbern
positions to the left.(package private) static void
shiftLeft(List<Bit> x)
Using recursion, shifts the bits of the specified list one position to the left.int
toDecimal()
Returns the decimal (base 10) value of this binary number as anint
value.String
toString()
Returns a string representation of this binary number.
-
-
-
Constructor Detail
-
Binary4
public Binary4(Bit bit)
Initializes this binary number to a single digit binary number by copying the specified bit.- Parameters:
bit
- the bit to copy
-
Binary4
public Binary4(List<Bit> bits)
Initializes this binary number to have the specified bits. The constructor copies the individual bits into this binary number.- Parameters:
bits
- the bits of the binary number- Throws:
IllegalArgumentException
- if bits.size() is less than 1
-
-
Method Detail
-
numberOfBits
public int numberOfBits()
Returns the number of bits in this binary number.- Returns:
- the number of bits in this binary number
-
getBit
public Bit getBit(int index)
Gets the bit at the given index. The returned bit cannot be used to modify this binary number.Index 0 is the index of the left-most bit.
- Parameters:
index
- the index of the bit to get- Throws:
IllegalArgumentException
- if index is out of bounds for this binary number
-
setLeftmostBit
public void setLeftmostBit(Bit bit)
Sets the leftmost bit by copying the value ofbit
into the leftmost bit.- Parameters:
bit
- the value of the bit to set
-
toDecimal
public int toDecimal()
Returns the decimal (base 10) value of this binary number as anint
value. If this binary number has 32 bits or more then the user of this method should expect that the result will be incorrect because of overflow.- Returns:
- the decimal (base 10) value of this binary number
-
safeGetBits
public List<Bit> safeGetBits()
Returns a list of the bits of this binary number. The returned list cannot be used to modify the bits of this number.- Returns:
- a list of bits equal to the list of bits of this number
-
shiftLeft
public void shiftLeft(int n)
Shifts all of the bits of this binary numbern
positions to the left. The originaln
left-most bits are discarded (that is, those bits do not wrap around to the left). Then
right-most bits are set to 0. For example, if this binary number is:1110101
then shifting the bits of the number by 2 positions changes this binary number to:
1010100
- Parameters:
n
- the number of positions to shift- Throws:
IllegalArgumentException
- if n is less than zeroIllegalArgumentException
- if n is greater than the number of bits in this number
-
shiftLeft
static void shiftLeft(List<Bit> x)
Using recursion, shifts the bits of the specified list one position to the left. The last bit of the specified list becomes equal to the binary digit 0. For example, ifx
is the list[1, 1, 0, 1]
then
shiftLeft(x)
changesx
to equal the list[1, 0, 1, 0]
If
x
is empty then this method does nothing (and does not throw an exception).- Parameters:
x
- a list of bits
-
toString
public String toString()
Returns a string representation of this binary number. The returned string is made up of the bit values (0 or 1) of the number from the left-most bit to the right-most bit.
-
getBits
public List<Bit> getBits()
Returns a reference to the list of bits; for testing purposes only.- Returns:
- a reference to the list of bits of this binary number
-
main
public static void main(String[] args)
A simple test method.- Parameters:
args
- not used
-
-