Number Systems Number Systems

Information

Computers process information.

Almost all computers nowadays are digital, which means that information is represented by numbers.

The alternative to a digital computer is an analog computer, in which information is represented by a continuously variable physical quantity. For example, a car speedometer is usually an analog device. A traditional record player is an analog device. A compact disc is digital, and a CD player contains a digital to analog converter (DAC) in order to convert the numbers on the disc into sounds.

It is convenient to use binary numbers, so that the only digits are 0 and 1. In an electronic computer, 0 and 1 can be represented by different voltages - often 0 volts for 0 and +5 volts for 1 - or by the absence or presence of electric charge.

Example: the letter Q is conventionally represented by the number 81, which is 1010001 in binary.

Number Systems: Binary

In everyday life we use decimal, or base 10, which means that each digit is a number from 0 to 9 and the column values are powers of 10. For example, the number 237 means


2 ×100 + 3×10 + 7 ×1.
The column values are as follows:


100
10
1
2
3
7
In binary, or base 2, each digit is either 0 or 1 and the column values are powers of 2. For example, the column values for the binary number 110101 are


32
16
8
4
2
1
1
1
0
1
0
1
and this means


1 ×32 + 1 ×16 + 0 ×8 + 1 ×4 + 0 ×2 + 1×1
which works out to be 53. A binary digit is usually called a bit.

To convert from binary to decimal, simply add up the powers of 2 indicated by the positions of the 1s in the binary number.

An 8-bit binary number is called a byte, and can represent a decimal number from 0 to 255.

A word is a longer binary number. Different microprocessors use words of different lengths. 32 bits is a common word length with current technology, but 64-bit and 128-bit designs are emerging.

Binary numbers are often written with leading zeros to fill up a byte or word.

Converting from Decimal to Binary (1)

The first method of converting from decimal to binary calculates the bits from left to right. Assume we are starting with a number n.

  1. Work out how many bits are needed to represent n in binary. If n \geqslant 2m then at least m+1 bits are needed, but more can be used. Let b be the number of bits to be used.
  2. The leftmost bit is 1 if n \geqslant 2b-1, otherwise it is 0.
  3. If the leftmost bit is 1 then let n¢ = n - 2b-1, otherwise let n¢ = n. If n¢ = 0 then all the remaining bits are 0, otherwise repeat the previous step to calculate the binary representation of n¢ in b-1 bits.

Converting from Decimal to Binary (2)

The second method calculates the bits from right to left. Again assume we are starting with a number n.

  1. The rightmost bit is 1 if n is odd and 0 if n is even.
  2. Let n = n / 2, ignoring any remainder. Repeat from step 1, to calculate the bits one by one from the right, until n reaches 0.
Leading zeros can always be added if necessary to produce a binary representation in a larger number of bits.



Binary Conversion Exercise

For practice with either method, use the Binary Converter exercise.
This exercise can be found at the following page : Binary Conversion Exercise


Fractions in Binary

Fractions can be represented in binary by adding digits to the right of a ``binary point'' (instead of a decimal point). The column values to the right of the binary point are [1/2], [1/4], [1/8] etc.

For example, the binary number 101.011 is 4 + 1 + [1/4] +[1/8], i.e. 5[3/8], or 5 + 0.25 + 0.125 = 5.375 as a decimal.

To convert a decimal fraction to binary:

First convert the integer part (i.e. the whole number on the left of the decimal point) to binary as usual.

To convert the fractional part: multiply by 2. If the result is greater than or equal to 1, put a 1 in the [1/2] column, otherwise put a 0 in the [1/2] column. Take the fractional part of the result and multiply by 2 again to work out the [1/4] column. Repeat, always working with just the fractional part, until either the result is exactly 1, or enough digits have been calculated.

Note that a fraction might be recurring in binary even if it is non-recurring in decimal. Generally we would calculate a binary fraction to a specified number of digits of accuracy.

Example Convert 7.2 to binary.

The integer part is 111. 0.2 ×2 = 0.4 so the [1/2] column is 0. 0.4 ×2 = 0.8 so the [1/4] column is also 0. 0.8 ×2 = 1.6 so the [1/8] column is 1. We now discard the 1 and start multiplying 0.6 by 2. 0.6 ×2 = 1.2 so the [1/16] column is 1. Discarding the 1 leaves 0.2 and obviously the calculation repeats from this point; therefore we have a recurring binary fraction. So 7.2 in decimal is 111.0011 recurring in binary.

Number Systems: Hexadecimal

Hexadecimal, also known simply as hex, is base 16. Each digit of a hexadecimal number therefore represents a number from 0 to 15. The letters A - F (or a - f) are used as extra digits to represent the numbers 10 - 15.

Example The hex number 3f corresponds to 3 ×16 + 15 in decimal, i.e. 63.

There is a simple correspondence between the binary and hex representations of a number. Each hex digit corresponds to 4 binary digits, as follows.

Hex Binary Hex Binary
0 0000 8 1000
1 0001 9 1001
2 0010 a 1010
3 0011 b 1011
4 0100 c 1100
5 0101 d 1101
6 0110 e 1110
7 0111 f 1111

Examples

  1. 3d in hex is 3×16 + 13 = 61 in decimal.
  2. 11010110 in binary is d6 in hex. Note that hex is much more compact than binary.
It is immediately obvious how many bits are needed to represent a given hex number - 4 bits per digit. It is much less obvious how many bits are needed to represent a given decimal number (until you memorise the facts that the largest 8 bit number is 255 and the largest 16 bit number is 65535, etc!)

The prefix ``0x'' is often used to indicate that hex is being used. So 0x32 means 32 in hex, i.e. 50 in decimal. This convention works in Java (and C and C++), but in Ada you write 16#32#.

Number Systems: Octal

Occasionally octal, or base 8, is used. An octal digit is a number from 0 to 7, corresponding to 3 bits, and the column values are powers of 8 (1, 8, 64, 512, ...).


File translated from TEX by TTH, version 2.78.
On 27 Jul 2001, 10:19.