Computer Number Systems
Objective #1: Count in binary, octal and hexadecimal.
- All computers do the amazing things that they do by simply manipulating ones and zeros. Computers can only do binary (base 2) arithmetic. You must be able to use
the binary number system in order to appreciate how the computer works. Examples of binary numbers are 1110, 10101, 111, & 000101.
- An individual digit in the binary numbering system is called a bit (as in binary digit). Furthermore, because it is convenient
to work with larger groups of bits at a time, computer scientists call a group of 8 bits a byte.
Examples of bytes are:
1010 1111
1100 0011
0000 0001
We place a gap between every 4 bits in
a byte to make them more readable.
- Counting in binary is similar to counting in the familiar decimal system. However, you can only use 0's and 1's. Follow the pattern below
0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
- Notice that all odd decimal numbers (such as 7) are equivalent to binary numbers that end with a 1 bit ( 0111) and all even decimal numbers (such as 4) are equivalent to binary numbers that end with a 0 bit ( 0100 ). You'll understand this pattern later in the lecture notes.
- At its lowest level every computer works in binary with bits. It's only for the convenience of humans (readability, shorter numbers, etc.) that we like to work in other number systems. Since our normal numbering system, decimal, is base 10 it is not easy to convert decimal numbers to binary numbers. So computer scientists prefer to work in base 8 which is known as octal or base 16 which is known as hexadecimal. The numbers 8 and 16 are perfect powers of 2 (8 = 2 cubed and 16 = 2 to the 4th power).
- Decimal System
- base 10
- valid digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- Binary System
- base 2
- valid digits - 0, 1
- Octal System
- base 8
- valid digits - 0, 1, 2, 3, 4, 5, 6, 7
- Hexadecimal System
- base 16
- valid digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
- A refers to decimal 10, B refers to decimal 11, etc
- Replace the System.out.println statement in the HelloWorld program that you learned earlier in this unit with this loop to use a program to count in binary
for (int i = 1; i <= 10; i++)
{
System.out.println(Integer.toBinaryString(i));
}
The for loop iterates 10 times using the loop variable i. It also makes use of the built-in, library method toBinaryString found in the Integer wrapper class. The Integer class also contains a method named toOctalString and toHexString
Objective #2: Add and subtract binary,
octal, and hexadecimal
numbers.
- When you add two decimal numbers, you carry groups of tens to the next column when necessary. For example, when adding 48 + 56, you initially add the 8 and 6 digits in the ones columns and get a total of 14. However, you pull a group of 10 out of the 14 and print a 1 above the tens column in a process that we refer to as "carrying" a group of ten. You then write the leftover amount 4 as the ones digit in the answer. Next you add the 1 that you carried to the 4 and the 5 in the tens column for a total of 10. From that 10 you carry a group of 10 and put a 1 at the top of the hundreds column and write a 0 since that is the leftover as part of the answer. Finally, the 1 that is all by itself in the hundreds column is written as part of the final answer.
- The same process is used when adding binary, octal, and hexadecimal numbers. Instead of carrying groups of 10 though you carry groups of 2 when doing binary addition and groups of 8 when doing octal addition and groups of 16 when doing hexadecimal addition.
- When subtracting decimal numbers, you must "borrow" a group of 10 sometimes from a digit in the next higher column. This gives you an extra 10 added to the value that is doing the borrowing. Likewise, you sometimes must borrow a group of 2 when doing binary subtraction, a group of 8 when doing octal subtraction, and a group of 16 when doing hexadecimal subtraction.
- Here's a great guide that Mr. Minich made for students to learn to add octal numbers: STEP-BY-STEP GUIDE ON ADDING OCTAL NUMBERS
- There are dozens or hundreds of online videos that you can find at YouTube, KhanAcademy.org, etc. to learn how to add and subtract numbers in different number systems. It is your responsibility to learn this topic ON YOUR OWN and get any additional help from the teacher outside of class.
Here is a link to get your started:
Objective #3: Convert decimal numbers into binary,
octal, & hexadecimal and vice versa.
- The names of the columns in a decimal number are ones, tens, hundreds, thousandths, and so on from right to left. For example, the digit 3 in 123 is in the ones column. In an octal number the columns could be referred to as the ones, eights, sixty-fours, one hundred twenty-eights and so on from right to left. The digit 2 in 425 is in the eights column. In a hexadecimal number, you could refer to the columns as the ones, sixteens, two hundred fifty sixes, and so on from right to left.
- To convert a number in a base other than 10 to base 10:
Easy way:
If you need to convert 345 (base 8) to decimal, you would label
the three columns as the 1's, 8's, and 64's columns from right to left.
Then, you would multiply the digits by these column labels and compute the
sum of the 3 products. Since a 3 is in the 64's column, you multiply to obtain
192. Since 4 is in the 8's column, you multiply 4 x 8 to get 32. Then add
32 to the 192 from the step before to get a running total of 224. Finally,
since a 5 is in the 1's column, multiply 1 x 5 to get the product of 5 and
add it to the running toal of 224 to get 229. That final value, 229, is the
decimal equivalent to the original number 345 (base 8).
View an example - binary to decimal : octal to decimal : hex
to decimal
Formal way:
(3 x 82) + ( 4 x 81)
+ (5 x 80) = 229
View an example: binary to decimal - octal to decimal -hex
to decimal
- To convert a decimal number into a base other than 10:
View an example: decimal to binary - decimal to octal - decimal
to hex
To convert a decimal number into binary:
Write out the powers in the other base starting at the power of 0 until
you reach a number higher than the given number.
Divide the highest power of the base that can divided at least once into
the given number.
Put the quotient of that division into a column that will eventually be
the leftmost digit of the final answer.
Continue steps 2 & 3 using the remainder of the previous division but
add each successive quotient to the right of previous quotients in the final
answer.
Flip the standard division sign upside down so the line goes under the dividend rather than over it. I'll call this "upside down division". Use 2 as a divisor each time to go into the dividend with the remainder written to the right. Continue until the dividend is 0. The binary value can be put together from the bottom up.
Example: To convert decimal 42 to binary:
2 |_ 42_
2 |_21_ R 0
2 |_10_ R 1
2 |_5_ R 0
2 |_2_ R 1
2 |_1_ R 0
2 |_0_ R 1
So decimal 42 equals binary 101010
- Here is a particularly good video of a professor explaining how to convert decimal, binary, octal, and hex numbers - https://www.youtube.com/watch?v=LG7O1DIwpX4
- Here is a fun decimal to binary game at http://forums.cisco.com/CertCom/game/binary_game_page.htm
- There are dozens or hundreds of online videos that you can find at YouTube, KhanAcademy.org, etc. to learn how to convert numbers from one number system to another. It is your responsibility to learn this topic ON YOUR OWN and get any additional help from the teacher outside of class.
Here are a few links to get your started:
Objective #4: Converting binary numbers directly into hexadecimal or octal numbers and vice versa.
- Watch this video found in the teacher's YouTube playlist to learn how to use a shortcut to convert binary values directly into either hexadecimal or octal. This can save time on AP exam multiple choice questions since you'll avoid having to first convert the binary number into a decimal number and then convert that decimal number into a hexadecimal number.