Binary Numbers
A computer is just a machine, but it is a machine that keeps track of information, such as letters and numbers. Unfortunately, the computer does not have anything as fancy as a human brain, so keeping track of letters and numbers is a bit difficult. The computer records information in its memory using rows of bits. A bit is something like a tiny light bulb which is either on or off. In realitiy, a computer bit is a capacitor and a transistor working together -- the capacitor is either storing a small electric charge (i.e., is on) or is not storing a charge (off ). The transistor helps the process of turning the capacitor on and off, and also is used to read the current state of the capacitor (i.e., it can check if the capacitor is on or off).
A set of bits can have different patterns of on and off to represent to different values. A similar system that we see everyday is a traffic light. A traffic is a row of three light bulbs, and different on/off patterns have different values:
on
off means STOP
off
off
on means YIELD
off
off
off means GO
on
On a computer the different on/off patterns indicate different numeric values. The computer puts the bits into small groups called bytes (a group of eight bits). Each group is given a different on/off pattern to represent a specific number. For example, if we look at a byte that has all of the bits turned off:
off off off off off off off off means 0
and if only the last transistor turned on
off off off off off off off on means 1
If the last two bits are turned on
off off off off off off on on means 3
When referring to the value of bits people typically
write a 0 for off and a 1 for on. So the values shown above could
be written as follows:
0 0 0 0 0 0 0 0 means 0
and only the last bit on
0 0 0 0 0 0 0 1 means 1
The last two bits on:
0 0 0 0 0 0 1 1 means 3
If you use a certain set of on/off patterns for the bits then the bit patterns can represent numbers in base 2 (binary). Humans typically write numbers using base 10 (decimal). Each place, or digit, in a base 10 number has a given value. For example, the number 3692 can be interpreted as follows:
1000 100 10 1
---------------------
3 6 9 2
The total value of 3692 is determined by mulitiplying each digit by its column value (i.e., 3 x 1000, and 6 x 100) then adding these results together (i.e., 3000 + 600 + 90 + 2 = 3692). The same system is used for base 2 numbers, except the column values are based on the powers of (1, 2, 4, 8, 16, etc.) instead of powers of 10 (1, 10, 100, 1000, etc.).
The example below shows how to determine the value of the binary number 00010010. The numbers above line indicate how much each column is worth -- that column value is multiplied into its corresponding binary digit.
-128 64 32 16 8 4 2 1
--------------------------------------
0 0 0 1 0 0 1 0
A '1' picks up, or is multiplied into, the value above it. A '0' is just zero. So in the example
above there is a '1' at the 16 place and a '1' at the 2 place'. Add these together and you get the value 18, which is the value intended to be stored in the
computer in this case (i.e., 1 x 16 + 1 x 2 = 18).. Therefore
0 0 0 1 0 0 1 0 (in base 2) = 18 (in base 10)
Determine the decimal (base 10) values of the following binary (base 2) numbers. Write your answers on a scrap piece of paper.
-
What is the value of binary number 0 0 0 0 0 0 1 1 in base 10?
-
What is the value of binary number 0 0 0 1 1 0 0 1 in base 10?
-
What is the value of binary number 0 0 0 0 0 0 0 0 in base 10?
-
What is the value of binary number 0 0 1 0 0 0 0 0 in base 10?
-
What is the value of binary number 0 0 0 0 1 1 0 0 in base 10?
You might have noticed that the left-most column is a negative 128. This allows us to represent negative numbers. For example, the binary number
1 0 0 0 0 0 0 0
represents the decimal value -128, and the binary number
1 0 0 0 0 0 1 1
represents -125 (-128 + 2 + 1).
Determine the decimal (base 10) values of the following binary (base 2) numbers. Write your answers on a scrap piece of paper.
- What is the value of binary number 1 0 0 0 0 1 0 0 in base 10?
- What is the value of binary number 1 0 0 0 0 0 1 0 in base 10?
Using the scheme described above:
- What is the smallest (most negative) value that you can store in 8 bits?
- What is the largest (biggest positive) value that you can store in 8 bits?
Here are the answers to the above questions.
The examples above used a one byte number. This corresponds to Java's byte built-in data type which can represent the integer values from -128 to +127. Java also has the short built-in data type which uses two bytes of memory and can represent the integer values from -32,768 to +32,767:
-32768 16384 8192 4096 2048 1024 256 128 64 32 16 8 4 2 1
-------------------------------------------------------------------------------
0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
Java's most commonly used integer is probably the four byte int which can represent the integer values from -2,147,483,648 to +2,147,483,647. For very large values programmers use the eight byte long which can represent the values from -9223372036854775808 to +9223372036854775807. |