Class Binary
- java.lang.Object
-
- Binary
-
public class Binary 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.Bit
getRightMostBit()
Returns the right-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
setBit(int index, Bit bit)
Sets a bit at the given index by copying the value ofbit
into the bit at the given index.int
toDecimal()
Returns the decimal (base 10) value of this binary number as anint
value.(package private) static int
toDecimalHelper(List<Bit> bits)
Recursively computes the decimal value of a binary number represented as a list of bits.String
toString()
Returns a string representation of this binary number.
-
-
-
Constructor Detail
-
Binary
public Binary(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
-
Binary
public Binary(Binary 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
-
getRightMostBit
public Bit getRightMostBit()
Returns the right-most bit of this binary number. The returnedBit
cannot be used to modify this binary number.- Returns:
- the right-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
-
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.
-
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
-
toDecimalHelper
static int toDecimalHelper(List<Bit> bits)
Recursively computes the decimal value of a binary number represented as a list of bits. For example, the list:[1, 0, 1, 1]
representing the binary number 1011 has a decimal value of
(1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0) = 11
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.
- Parameters:
bits
- a list of bits representing a binary number- Returns:
- the decimal value of the 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
-
-