Documente Academic
Documente Profesional
Documente Cultură
Although we commonly use the decimal system (Base 10) to perform mathematical calculations, it is
quite useful to study other number bases. Computers use binary (Base 2), but because numbers written in
binary can be long and easily misread, hexadecimal (Base 16) is commonly used in output messages.
Any number can be represented in the decimal system using a combination of the ten decimal digits 0, 1,
2, 3, 4, 5, 6, 7, 8 and 9. The position of each digit is important, and corresponds to a successive power of
ten (one, ten, hundred, thousand, etc.):
3472 = three thousands, four hundreds, seven tens, and two ones
= three thousands + four hundreds + seven tens + two ones
= 3 thousands + 4 hundreds + 7 tens + 2 ones
= (3 × 1000) + (4 × 100) + (7 × 10) + (2 × 1)
= (3 × 103) + (4 × 102) + (7 × 101) + (2 × 100) [remember: 100 = 1]
Any number can be represented in the binary system using a combination of the two binary digits 0 and 1.
As in the decimal system, the position of each digit is important — but instead corresponds to a successive
power of two (one, two, four, eight, sixteen, etc.):
1101 = one eight, one four, zero twos, and one one
= one eight + one four + zero twos + one one
= 1 eight + 1 four + 0 twos + 1 one
= (1 × 8) + (1 × 4) + (0 × 2) + (1 × 1)
= (1 × 23) + (1 × 22) + (0 × 21) + (1 × 20) [remember: 20 = 1]
By expanding a number in binary to show the powers of two involved, we can also obtain its decimal
representation. This allows us to convert from decimal to binary. Continuing from the last line above:
=8+4+0+1
= 13 (in Base 10)
Since 1101 could represent either “one-thousand one-hundred and one” in Base 10 or “thirteen” in Base
2, a small subscript is commonly appended to indicate the base when it might not otherwise be clear
from the context:
11012 = 1310
Any number can be represented in the hexadecimal system using a combination of the sixteen hex digits
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A (for 10), B (11), C (12), D (13), E (14) and F (15). Once again, the position of
each digit is important and corresponds to a successive power of sixteen (one, sixteen, two-hundred fifty-
six, etc.):
By expressing numbers in binary using powers of two and numbers in hex using powers of sixteen, they
can be converted to their decimal representation. To “go the other way” — i.e., to convert a decimal
number to binary or hexadecimal — requires division.
If a decimal number is successively divided by two, its binary representation is obtained; if successively
divided by sixteen, its hexadecimal representation is obtained (the ‘R’ below means “remainder”):
13 ÷ 2 = 6 R 1 691 ÷ 16 = 43 R 3
6 ÷ 2 = 3 R 0 43 ÷ 16 = 2 R 11
3 ÷ 2 = 1 R 1 2 ÷ 16 = 0 R 2
1 ÷ 2 = 0 R 1
Keep dividing until a quotient of zero is obtained, then write the remainders in the reverse of the order
they were obtained (when converting to hexadecimal, remember to change all two-digit remainders to the
corresponding hex digit). Thus:
The procedures above demonstrate how to convert numbers between decimal and binary, and decimal and
hexadecimal. Converting between binary and hexadecimal is surprisingly easy. By taking advantage of
the fact that 24 = 16, we can replace every four binary digits with a single hex digit and vice versa. Simply
keep the following chart in mind (do you see the pattern?):
When converting from binary to hexadecimal, group digits into blocks of four from right to left. Append
zeros in front of the binary number — “leading zeros” — as necessary to obtain even blocks of four:
When converting from hexadecimal to binary, expand each hex digit to the corresponding binary value.
Drop any leading zeros from the converted binary number:
74BE16 = 7 4 B E16
= 0111 0100 1011 11102 = 1110100101111102
Exercises:
Write each of the following binary numbers in decimal and hexadecimal:
1. 1101012 2. 10101012
3. 111112 4. 100000012
Binary Arithmetic
Addition
Adding numbers written in Base 2 is just like adding numbers written in Base 10. The basic rules for
binary addition are as follows:
0 + 0 = 0 1 + 0 = 1
0 + 1 = 1 1 + 1 = 10
As in Base 10, remember to “carry the one” as appropriate. Here are a few examples:
With two’s complement, a computer can subtract two numbers using the same circuitry it uses to perform
addition.
• 11001 has five digits, so n = 6 and we take the two’s complement of 1010 with respect to 26:
1010 → 001010 [leading zeroes] → 110101 [1’s complement] → 110110 [2’s complement]
• Add:
11001
+110110
1001111
• Since the sign bit — the sixth digit from the right — is zero, the answer is positive. Drop the
leading one (and leading zeroes) from 10001111, leaving 1111 as the answer.
4
Exercises:
Perform the operation indicated on the following binary numbers. Check your work by converting each
problem (and your answer) from binary to decimal:
1. 10111 + 11101 2. 11111 + 111
3. 101011 + 111011 4. 11111111 + 10000001