Sunteți pe pagina 1din 4

Number Bases

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.):

2B3 = two two-hundred fifty-sixes, eleven sixteens, and three ones


= two two-hundred fifty-sixes + eleven sixteens + three ones
= 2 two-hundred fifty-sixes + 11 sixteens + 3 ones
= (2 × 256) + (11 × 16) + (3 × 1)
= (2 × 162) + (11 × 161) + (3 × 160) [remember: 160 = 1]
= 512 + 176 + 3
= 691 (in Base 10)
2

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:

1310 = 11012 69110 = 2B316

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?):

016 = 00002 416 = 01002 816 = 10002 12 = C16 = 11002


116 = 00012 516 = 01012 916 = 10012 13 = D16 = 11012
216 = 00102 616 = 01102 10 = A16 = 10102 14 = E16 = 11102
316 = 00112 716 = 01112 11 = B16 = 10112 15 = F16 = 11112

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:

100010111012 = 100 0101 11012


= 0100 0101 11012
= 4 5 D16 = 45D16

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

Write each of the following decimal numbers in binary and hexadecimal:


5. 225 6. 1000

Write each of the following hexadecimal numbers in binary and decimal:


7. 19516 8. A0116
9. ABC16 10. 101016
3

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:

10 10001 10011 11111


+1 +1010 +1010 + 1010
11 11011 11101 101001

Subtraction Using Two’s Complement

With two’s complement, a computer can subtract two numbers using the same circuitry it uses to perform
addition.

To find the two’s complement of a number ‘x’ with respect to 2n:


1. Make sure x has n digits (add leading zeroes as necessary).
2. Change each digit of x to its complement (0 → 1, 1 → 0). This is called the one’s complement of x.
3. Add one to the one’s complement of x. This is the two’s complement of x with respect to 2n.

To find the difference (y - x) using two’s complement:


1. Choose n to be one more than the number of digits in the larger of x and y.
2. Find the two’s complement of x with respect to 2n.
3. Add y and the two’s complement of x.
4. Examine the “sign bit” of the sum — that is, the digit in the 2n-1 position (it is also the nth digit
counting from the right).
a) If the sign bit is zero, the difference is positive. Drop the leading one from the sum, and the
number remaining is the answer.
b) If the sign bit is one, the difference is negative. Take the two’s complement of the sum as the
answer and place a negative sign in front.

Here’s an example whose difference is positive:


11001 - 1010

• 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

Here’s an example whose difference is negative:


1010 - 11001

• Again n = 6, so we take the two’s complement of 11001 with respect to 26:


11001 → 011001 [leading zeroes] → 100110 [1’s comp.] → 100111 [2’s comp.]
• Add:
1010
+100111
110001
• Since the sign bit — again the sixth digit from the right — is one, the answer is negative. Find
the two’s complement of the sum and precede it with a negative sign:
110001 [sum] → 001110 [1’s comp.] → 001111 [2’s comp.] → -1111 [answer]

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

5. 10111 - 11101 6. 11101 - 10111


7. 11110 - 111 8. 111 - 11110

S-ar putea să vă placă și