Number Systems
Number Systems
Positional decimal systems include a zero and use symbols (called digits) for the ten
values (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) to represent any number, no matter how large
or how small. These digits are often used with a decimal separator which indicates
the start of a fractional part, and with a symbol such as the plus sign + (for positive)
or minus sign − (for negative) adjacent to the numeral to indicate its polarity.
The binary numeral system, or base-2 number system represents numeric values
using two symbols, 0 and 1. More specifically, the usual base-2 system is a positional
notation with a radix of 2. Owing to its straightforward implementation in digital
electronic circuitry using logic gates, the binary system is used internally by all
modern computers.
Representation
A binary number can be represented by any sequence of bits (binary digits), which in
turn may be represented by any mechanism capable of being in two mutually
exclusive states.
When spoken, binary numerals are usually read digit-by-digit, in order to distinguish
them from decimal numbers. For example, the binary numeral 100 is pronounced
one zero zero, rather than one hundred, to make its binary nature explicit, and for
purposes of correctness. Since the binary numeral 100 is equal to the decimal value
four, it would be confusing to refer to the numeral as one hundred.
Counting in binary is similar to counting in any other number system. Beginning with
a single digit, counting proceeds through each symbol, in increasing order. Decimal
counting uses the symbols 0 through 9, while binary only uses the symbols 0 and 1.
When the symbols for the first digit are exhausted, the next-higher digit (to the left)
is incremented, and counting starts over at 0. In decimal, counting proceeds like so:
000, 001, 002, ... 007, 008, 009, (rightmost digit starts over, and next digit is
incremented)
...
090, 091, 092, ... 097, 098, 099, (rightmost two digits start over, and next
digit is incremented)
0000,
0010, 0011, (rightmost two digits start over, and next digit is incremented)
0100, 0101, 0110, 0111, (rightmost three digits start over, and the next digit
is incremented)
Since binary is a base-2 system, each digit represents an increasing power of 2, with
the rightmost digit representing 20, the next representing 21, then 22, and so on. To
determine the decimal representation of a binary number simply take the sum of the
products of the binary digits and the powers of 2 which they represent. For example,
the binary number:
100101
[(1) × 25] + [(0) × 24] + [(0) × 23] + [(1) × 22] + [(0) × 21] + [(1) × 20] =
[1 × 32] + [0 × 16] + [0 × 8] + [1 × 4] + [0 × 2] + [1 × 1] = 37
To create higher numbers, additional digits are simply added to the left side
of the binary representation.
Binary arithmetic
Addition
0+0→0
0+1→1
1+0→1
Adding two "1" digits produces a digit "0", while 1 will have to be added to
the next column. This is similar to what happens in decimal when certain
single-digit numbers are added together; if the result equals or exceeds the
value of the radix (10), the digit to the left is incremented:
This is known as carrying. When the result of an addition exceeds the value of
a digit, the procedure is to "carry" the excess amount divided by the radix
(that is, 10/10) to the left, adding it to the next positional value. This is
correct since the next position has a weight that is higher by a factor equal to
the radix. Carrying works the same way in binary:
1 1 1 1 1 (carried digits)
01101
+ 10111
-------------
=100100
In this example, two numerals are being added together: 01101 2 (1310) and
101112 (2310). The top row shows the carry bits used. Starting in the
rightmost column, 1 + 1 = 102. The 1 is carried to the left, and the 0 is written
at the bottom of the rightmost column. The second column from the right is
added: 1 + 0 + 1 = 102 again; the 1 is carried, and 0 is written at the bottom.
The third column: 1 + 1 + 1 = 112. This time, a 1 is carried, and a 1 is written in
the bottom row. Proceeding like this gives the final answer 100100 2 (36
decimal).
When computers must add two numbers, the rule that: x xor y = (x + y) mod
2 for any two bits x and y allows for very fast calculation, as well.
Subtraction
0−0→0
0 − 1 → 1, borrow 1
1−0→1
1−1→0
Subtracting a "1" digit from a "0" digit produces the digit "1", while 1 will
have to be subtracted from the next column. This is known as borrowing. The
principle is the same as for carrying. When the result of a subtraction is less
than 0, the least possible value of a digit, the procedure is to "borrow" the
deficit divided by the radix (that is, 10/10) from the left, subtracting it from
the next positional value.
The octal numeral system, or oct for short, is the base-8 number system, and uses
the digits 0 to 7. Numerals can be made from binary numerals by grouping
consecutive binary digits into groups of three (starting from the right). For example,
the binary representation for decimal 74 is 1001010, which can be grouped into
(00)1 001 010 — so the octal representation is 112.
By performing the calculation above in the familiar decimal system we see why 112
in octal is equal to 64+8+2 = 74 in decimal.
Representation
To binary
To convert from a base-10 integer numeral to its base-2 (binary) equivalent, the number is
divided by two, and the remainder is the least-significant bit. The (integer) result is again
divided by two, its remainder is the next most significant bit. This process repeats until the
result of further division becomes zero.
To octal
To convert integer decimals to octal, divide the original number by the largest possible
power of 8 and successively divide the remainders by successively smaller powers of 8 until
the power is 1. The octal representation is formed by the quotients, written in the order
generated by the algorithm.
125 / 8^2 = 1
125 − ((8^2)*1) = 61
61 / 8^1 = 7
61 − ((8^1)*7) = 5
900 / 8^3 = 1
388 / 8^2 = 6
388 − ((8^2)*6) = 4
4 / 8^1 = 0
4 − ((8^1)*0) = 4
4 / 8^0 = 4
To decimal:
Binary 1 0 0 1 0 1 0 1 1 0 1
Decimal 1×210 + 0×29 + 0×28 + 1×27 + 0×26 + 1×25 + 0×24 + 1×23 + 1×22 + 0×21 + 1×20 = 1197
This example converts 11112 to base ten. Since each position in a binary numeral can
contain either a 1 or 0, its value may be easily determined by its position from the right:
00012 = 110
00102 = 210
01002 = 410
10002 = 810
Therefore:
This example shows the conversion of a binary number to decimal, mapping each digit to
the decimal value, and adding the results.
To octal
The binary digits are grouped by threes, starting from the decimal point and proceeding to
the left and to the right. Add leading 0s (or trailing zeros to the right of decimal point) to fill
out the last group of three if necessary. Then replace each trio with the equivalent octal
digit.
1 2 7 4
To hexadecimal
The advantage of using hexadecimal rather than decimal increases rapidly with the size of
the number. When the number becomes large, conversion to decimal is very tedious.
However, when mapping to hexadecimal, it is trivial to regard the binary string as 4-digit
groups and map each to a single hexadecimal digit.
Compare this to the conversion to hexadecimal, where each group of four digits can be
considered independently, and converted directly:
To decimal
To convert a number k to decimal, use the formula that defines its base-8 representation:
To binary
To convert octal to binary, replace each octal digit by its binary representation. The
correspondence between octal and binary numerals is the same as for the first eight digits
of hexadecimal in the table above. Binary 000 is equivalent to the octal digit 0, binary 111 is
equivalent to octal 7, and so forth.
Octal Binary
0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111
To hexadecimal
The conversion is made in two steps using binary as an intermediate base. Octal is
converted to binary and then binary to hexadecimal, grouping digits by fours, which
correspond each to a hexadecimal digit.
To binary:
1 0 5 7
then to hexadecimal:
2 2 F
The following table shows each hexadecimal digit along with the equivalent decimal value
and four-digit binary sequence:
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
To convert a hexadecimal number into its binary equivalent, simply substitute the
corresponding binary digits:
To convert a binary number into its hexadecimal equivalent, divide it into groups of four
bits. If the number of bits isn't a multiple of four, simply insert extra 0 bits at the left (called
padding). For example:
To convert a hexadecimal number into its decimal equivalent, multiply the decimal
equivalent of each hexadecimal digit by the corresponding power of 16 and add the
resulting values:
C0E716 = (12 × 163) + (0 × 162) + (14 × 161) + (7 × 160) = (12 × 4096) + (0 × 256) + (14 ×
16) + (7 × 1) = 49,38310
Exercise done in the class.