Numerical Bases
  Author: Hobbie
  Date: 8 Sept 2004
  Category: Other
  Expertise Level: ++
  ISA Logo

A numerical base is a way to represent numbers. The most commonly-used base is decimal (base 10). Computers, because they use electrical signals, work in binary (base 2) - see below. Hexadecimal (base 16) and octal (base 8) are also quite often used for related reasons.

The number of the base stands for the number of digits used by the base. For example, 0,1,2,....9 in base 10. For bases higher than 10, we use letters to represent values above 9 (A=10, B=11,....). For example hexadecimal will use 0,1,....,9,A,B,C,D,E,F.

A number abcd in base x will have a value of ax3 + bx2 + cx1 + dx0. For example, in decimal, 34517 = 30000 + 4000 + 500 + 10 + 7 = 3*104 + 4*103 + 5*102+1*101 + 7*100. A binary number, such as 10011001, will have a value of 1*28 + 0*27 + 0*26 + 1*25 + 1*24 + 0*22 + 0*21 + 1*20 = 28 + 25 + 24 + 20 = 256 + 32 + 16 + 1 = 305. Or 1A3 in hexadecimal is equal to 162 + 10*16 + 3 = 419 in decimal.

To convert a number from base 10 to base x, you have to divide the number by x until you get a quotient of 0, the number in base x will be the inverted succession of the remainders. For example, to convert 14523 in base 5:

  • 14523 = 5*2904 + 3
  • 2904 = 5*580 + 4
  • 580 = 5*116 + 0
  • 116 = 5*23 + 1
  • 23 = 5*4 + 3
  • 4 = 5*0 + 4
  • 14523 equals 431043 in base 5.

In hexadecimal:

  • 14523 = 16*907 + 11
  • 907 = 16*56 + 11
  • 56 = 16*3 + 8
  • 3 = 16*0 +3
  • 14523 equals 38BB in hexadecimal (remember: B=11).

Binary

Binary, or base 2, uses only two digits (0 and 1). Informations stored or transfered in computers is made of binary numbers. It is made of bits, standing for binary digits, which is therefore a "0" or a "1". 8 bits put together make a byte (for example 10110110). 1024 bytes (210 bytes) is a kilobyte, 1024 kilobytes is a megabyte, and so on to gigabytes and terabytes.

The binary and decimal representations of the first fifteen numbers are below:

Diagram: bases-1

Adding or multiplying binary numbers is done the same way as with decimal, with the only difference being the number of digits used. 2 doesn't exist in binary, 1 + 1 therefore equals 10 (read as "one-zero", not "ten") in base two.

Diagram: bases-2

  • AND: This logical operation is quite simple. It returns 0 if one or more of its arguments is 0, and 1 if all of them are 1. It is used mainly when working with masks. When encountering a 0, the value of the bit at this position will be discarded. When encountering a 1, the value will remain unchanged. For example a 11110000 mask will only retain the four first bits of a number.

  • OR: This operation will return 1 if one or more arguments is 1, and 0 if all of them are 0. It is used to make sure certain bits have a value of 1.

  • XOR: XOR, or Exclusive OR, returns 1 if it receives two different numbers, and 0 if it receives two equal ones. It can be used to compare two numbers. If the returned number has many 0s, the two numbers are similar; if it has many 1s, they are close to "opposite".

    Diagram: bases-3

Converting a number between binary and decimal can get quite difficult when working with big values. And, while binary is the base computers work in, it is not easy for humans to read. For these reasons, binary is often connverted into hexadecimal, and sometimes octal, which is much simpler to convert to or from. If you try to convert an hexadecimal number to binary, you will notice that each hexadecimal digit is represented by four bits in binary. For example, to convert 75D9 in binary:

  • 7 = 0111
  • 5 = 0101
  • 13 = 1101
  • 9 = 1001


  • 75D9 = 0111.0101.1101.1001 = 0111010111011001

This is due to 16 being equal to 24. Likewise, since 8=23, an octal number will be represented by three bits in binary. And, in the same way, converting to binary from hexadecimal or octal can be done by breaking the binary number in parts of respectively four or three bits and converting each.

To solve this problem with conversions while using decimal, Binary Coded Decimal (or BCD) was created. As with hexadecimal, each digit is coded on four bits. The main advantage of this method is that it converts decimal in binary easily, but it is not very practical for operations. It is used in small devices such as calculators, cell phones or LCD screens.

Links