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.
|
|
|
|
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.
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.
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 |
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#.