Class Binary3
- java.lang.Object
-
- Binary3
-
public class Binary3 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 boolean
equals(Object obj)
Compares this binary number to an object for equality.List<Bit>
getBits()
Returns a reference to the list of bits; for testing purposes only.Bit
getLeftMostBit()
Returns the left-most bit of this binary number.int
hashCode()
Returns a hash code for this binary number.static void
main(String[] args)
A simple test method.int
numberOfBits()
Returns the number of bits in this binary number.void
reverse()
Reverses the order of the bits of this binary number.(package private) static void
reverse(List<Bit> x)
Using recursion, reverses the order of the elements of the specified list.void
setBit(int index, Bit bit)
Sets a bit at the given index by copying the value ofbit
into the bit at the given index.void
shiftRight()
Shifts all of the bits of this binary number one position to the right.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
-
Binary3
public Binary3(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
-
Binary3
public Binary3(Binary3 other)
Initializes this binary number by copying the bits from another binary number.- Parameters:
other
- the binary number to copy
-
-
Method Detail
-
numberOfBits
public int numberOfBits()
Returns the number of bits in this binary number.- Returns:
- the number of bits in this binary number
-
getLeftMostBit
public Bit getLeftMostBit()
Returns the left-most bit of this binary number. The returnedBit
cannot be used to modify this binary number.- Returns:
- the left-most bit of this binary number
-
setBit
public void setBit(int index, Bit bit)
Sets a bit at the given index by copying the value ofbit
into the bit at the given index.Index 0 is the index of the left-most bit.
- Parameters:
index
- the index of the bit to setbit
- the value of the bit to set- Throws:
IllegalArgumentException
- if index is out of bounds for this binary number
-
shiftRight
public void shiftRight()
Shifts all of the bits of this binary number one position to the right. The original right-most bit is discarded (that is, it does not wrap around to the left-most bit). The left-most bit is set to 0. For example, if this binary number is:110011
then after shifting the bits right the binary number becomes:
011001
This is equivalent to dividing the number by 2 and discarding the remainder.
-
equals
public boolean equals(Object obj)
Compares this binary number to an object for equality. Returns true if and only ifobj
is aBinary
number that represents the same decimal value as this number.This method does not use
toDecimal
to compare the binary numbers. Instead, it makes a copy of the binary number with the fewest number of bits and adds enough zeros to the front of the copied number so that the copied number has the same number of bits as this number; then the binary representation of this number and the copied number are compared for equality. For example if this binary number has the 6-digit representation:000011
and the other binary number has the 3-digit representation:
011
then a copy of the shorter number is padded with 3 zeros to become:
000011
which has the same binary representation as this number.
-
reverse
public void reverse()
Reverses the order of the bits of this binary number. For example if this binary is:010111
then reversing this binary number changes this binary number to:
111010
-
reverse
static void reverse(List<Bit> x)
Using recursion, reverses the order of the elements of the specified list. Reversing an empty list causes the method to do nothing (and does not cause an exception to be thrown).- Parameters:
x
- a list to reverse
-
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
-
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.
-
hashCode
public int hashCode()
Returns a hash code for this binary number.
-
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
-
-