Number Systems

This page was translated by a robot.

In everyday dealings, we humans calculate in the so-called decimal system , i.e. with the digits 0 to 9. However, since a computer can only distinguish between the two states of electricity and non-electricity, it only knows the digits 0 and 1. The system with two states becomes a binary system called.

However, the limited number of digits is not an obstacle for mathematical calculations. These can be executed in exactly the same way, regardless of the so-called number system . In programming with C and C++, the decimal system and in certain cases the hexadecimal system (16 digits) are mainly used. The binary system is built into only a few compilers, but can always be reproduced using bit operations. On the sidelines, the okal system (8 digits) plays a small role in programming.

The decimal system

The decimal system is the system that we humans calculate with our 10 fingers. By stringing the Arabic digits 0 through 9 together, we can write any positive integer. However, the arrangement of the digits is not arbitrary. Even in elementary school we learn to write down numbers in such a way that the digits on the left are more significant than the digits on the right. Here is the number 456 as an example:

  4 * 100
+ 5 *  10
+ 6 *   1
---------
=     456

The hundreds, tens and ones were represented as the sum of so-called powers of ten . In the following, these powers of ten are written down mathematically:

  Decimal
----------------
        1 = 10^0
       10 = 10^1
      100 = 10^2
    1'000 = 10^3
   10'000 = 10^4
  100'000 = 10^5
1'000'000 = 10^6
            etc.

The number 10 is called the base of this system.

Since people normally calculate with the decimal system, when programming in C and C++, a compiler always assumes the decimal system when a number occurs in the source code. To write a number in another system, it must be specially marked.

Even if a compiler always accepts decimal numbers, it converts the numbers to binary numbers during translation. How the conversion of number systems works is further down on this page.

The Binary System

Setting the base to 2 gives rise to powers of two :

   Binary         Decimal
-------------------------
        1 = 2^0 =       1
       10 = 2^1 =       2
      100 = 2^2 =       4
     1000 = 2^3 =       8
    10000 = 2^4 =      16
   100000 = 2^5 =      32
  1000000 = 2^6 =      64
 10000000 = 2^7 =     128
100000000 = 2^8 =     256
            etc.

Important numbers are:

2^8: These are the number of states that can be represented with one byte and thus with the int8_ttype.

2^10: This number is often used for size indications. Since it is very close to 1000, it is used for the kilo . Calculated exactly, however, is 2^10 = 1024. Correctly, this size is not called kilo, but kibi (kilobinary), although the distinction between 1000 and 1024 is rarely made.

2^16: These are the number of states that can be represented with two bytes and thus with the int16_ttype .

2^20: This number is around one million and is called a mega because of its proximity to one million . However, since 2^20 does not exactly number one million, there is also the correct size specification Mebi (Megabinary).

2^30: This number corresponds to around 1 billion and is denoted by Giga . Correctly, this size specification is called Gibi (Gigabinary)

2^32: These are the number of states that can be represented with four bytes and thus with the int32_ttype .

2^64: These are the number of states that can be represented with eight bytes and thus with the int64_ttype .

In C and C++, binary numbers can usually no longer be written with modern compilers. However, if a compiler allows them, they must be marked as binary numbers by appending a 0bleading or trailing number to the number b. Further information on the notation can be found under the fixed values ​​(literals) .

The Hexadecimal System

The hexadecimal system has base 16, which results in the following powers of 16:

Hexadecimal                Decimal
----------------------------------
          1 = 16^0 =             1
         10 = 16^1 =            16
        100 = 16^2 =           256
       1000 = 16^3 =         4'096
      10000 = 16^4 =        65'536
     100000 = 16^5 =     1'048'576
    1000000 = 16^6 =    16'777'216
   10000000 = 16^7 =   268'435'456
  100000000 = 16^8 = 4'294'967'296
              etc.

The hexadecimal system requires 16 different digits. Since the Arabic numerals are not sufficient for this, the letters to are simply used for the values 10​​to :15af

+-----+-----+------+
| Dec | Hex |  Bin |
+-----+-----+------+
|  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 |
|  10 |  a  | 1010 |
|  11 |  b  | 1011 |
|  12 |  c  | 1100 |
|  13 |  d  | 1101 |
|  14 |  e  | 1110 |
|  15 |  f  | 1111 |
+-----+-----+------+

It doesn't matter in C and C++ whether the letters are uppercase or lowercase. To identify a hexadecimal number, the number is 0xappended to the front. Further information on the notation can be found under the fixed values ​​(literals) .

As can be easily seen in the previous table, hexadecimal numbers and binary numbers are closely related. Every four bits of a binary number can be represented as a hexadecimal number. Last but not least, due to the more compact notation, hexadecimal numbers replaced binary numbers in programming at some point.

The Octal System

The octal system has the base 8. This system is only used very rarely. For example, when specifying the rights flags of files in a Unix file system or when specifying individual characters using an appropriate escape code. The latter can be looked up in the fixed values ​​(literals).

Conversion of the Systems

From a mathematical point of view, a written or otherwise stored number has a value on the one hand and a form of representation on the other. Choosing the number system only changes the form of representation, but not the underlying value. All systems with two or more digits are therefore to be regarded as equivalent and can always be converted into one another.

A conversion between binary numbers and hexadecimal numbers is very simple. According to the table above, four binary digits correspond to exactly one hexadecimal digit. When converting from one system to the other, the digits can simply be replaced by their counterparts. The same applies to conversions between the octal system and the binary system, but the corresponding bit sequences are not listed on this page. For a conversion between the octal system and the hexadecimal system, the number is simply first converted into the binary system as an intermediate step.

More interesting is the conversion between the decimal system and one of the others. Since the number 10 itself is not a power of two, there are no unambiguous sequences of digits that would represent the decimal digits. For example, in order to convert a hexadecimal number into a decimal number, the powers of sixteen used must be summed up:

Hexadecimal: 4f36ea17

4  * 268'435'456 =   1'073'741'824
15 *  16'777'216 =     251'658'240
3  *   1'048'576 =       3'145'728
6  *      65'536 =         393'216
14 *       4'096 =          57'344
10 *         256 =           2'560
1  *          16 =              16
7  *           1 =               7
                   + -------------
Decimal:             1'328'998'935

The reverse conversion must therefore also take place in reverse:

Decimal:             1'328'998'935

1'328'998'935 / 268'435'456 =  4 remaining 255'257'111
  255'257'111 /  16'777'216 = 15 remaining   3'598'871
    3'598'871 /   1'048'576 =  3 remaining     453'143
      453'143 /      65'536 =  6 remaining      59'927
       59'927 /       4'096 = 14 remaining       2'583
        2'583 /         256 = 10 remaining          23
           23 /          16 =  1 remaining           7
            7 /           1 =  7 remaining           0

Hexadecimal: 4f36ea17

The decimal conversions take a lot of time. For this reason, numerical values ​​in computers are usually stored in binary and only switched to the decimal system for input or output.

An example program that can convert values ​​of any size into one another can be found in the number system converter.

Next Chapter: Bits, Bytes, msb, Endianness