Programs can be expressed as binary

Chapter 2: Number systems

General basics of programming



This book is licensed under a Creative Commons license.


2.1 General

Peculiarities of number systems

The non-programmer is often not even aware that there are different number systems and that the decimal system used in everyday life is only one of an infinite number. Number systems are defined via the character set and place value. The character set represents the characters allowed in the number system. In the decimal system, these are the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. The place value determines which base number is raised to the power of a digit. The base number in the decimal system is 10.

The value of the three digits 456 is calculated in the decimal system as follows: The position on the far right is 0. The position to the left of it, on which the number 5 is located, is 1. The position on the far left is the 2. Further positions would be can be described with linearly increasing numbers. As you already know, the basic number is raised to the power of the job description. So the digit 0 becomes 100, position 1 becomes 101 and position 2 becomes 102. The respective result is multiplied by the number in the respective place. In the example, the number 6 is in the 0 position, the number 5 is in the 1 position and the number 4 is in the position 2. The entire invoice is therefore 6 * 100+5*101+4*102. That gives 6 * 1 + 5 * 10 + 4 * 100, which in turn gives 6 + 50 + 400 and leads to solution 456.

Of course, if you see a number like 456 in the decimal system, you don't need to do the calculation every time. You know immediately what the number means because you are used to working in the decimal system. However, you need to keep the rules of calculating the value of a number in mind when you are looking at numbers from other number systems. Or can you immediately tell what the hexadecimal number FA or the binary number 10011 represent?


2.2 Number systems and the computer

Computers don't know letters

Why do you actually have to deal with number systems? You're a programmer, not a mathematician. Unfortunately, as a programmer, you are dealing with a machine that works exclusively with numbers. Computers are real number acrobats. If you want to say anything directly to the computer, you have to express yourself in numbers. The next problem is that while the computer basically understands numbers, it prefers to work in different number systems than you do. While you can get along well with the decimal system in everyday life, your computer prefers to use the binary system. And so that it doesn't get too easy, numbers in the binary system are unfortunately not that easy to convert into numbers in the decimal system. Since the binary system is extremely uncomfortable to use for human purposes, programmers prefer to use the hexadecimal system. Because it is only a few steps from the binary system to the hexadecimal system, and handling hexadecimal numbers is much easier than handling binary numbers.

Note that modern programming languages ​​usually allow you to specify decimal numbers without problems if you need to specify numbers anywhere in the program. As a programmer, you are basically not forced to deal with binary or hexadecimal numbers. However, in many cases it can be helpful to know how numbers look and are formed in the binary and hexadecimal systems.


2.3 The binary system

Zeros and ones

You may be familiar with the saying that computers only know zeros and ones. Zeros and ones are the only two valid characters in the binary system. The character set of the binary system consists of 0 and 1. If you now find out that the place value in the binary system is 2, you have all the information you need to calculate the value of binary numbers.

For example, look at the binary number 10011. At the 0 position there is a 1, at the 1 position also a 1, at the 2 position a 0, at the 3 position also a 0 and again a 1. Positions are always counted up from right to left starting at zero . Since the place value in the binary system is 2, the calculation looks like this: 1 * 20+1*21+0*22+0*23+1*24. That results in 1 * 1 + 1 * 2 + 0 * 4 + 0 * 8 + 1 * 16, which in turn results in 1 + 2 + 0 + 0 + 16. So the solution is 19. The binary number 10011 represents the value 19 in the decimal system.

How do you get from the decimal number 19 to the corresponding binary representation? Divide the decimal number by the place value of the binary system, which is 2, and note the remainder. Again divide the result by 2 and write down the residual value again. At some point you will get to zero. You will now get the binary number if you put the remaining values ​​in a row. Would you like an example?

19/2 = 9 remainder 1. 9/2 = 4 remainder 1. 4/2 = 2 remainder 0. 2/2 = 1 remainder 0.1 / 2 = 0 remainder 1. Now write the remainder from right to left in a number and you get 10011.


2.4 The hexadecimal system

Numbers and a few letters

The following characters are available in the hexadecimal system: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. While the binary system, in contrast to the decimal system, displays fewer information states can store one digit - namely only two -, more information states can be stored in the hexadecimal system - namely 16. The first ten characters are identical to those in the decimal system. For the decimal value 10 you need two digits in the decimal system. In the hexadecimal system, you can express this value in one place with the character A. A second digit is only required for numbers above the decimal value 15, which corresponds to the character F in the hexadecimal system.

How does the conversion of a hexadecimal number, for example FA, into the decimal system look like? In addition to the character set, you also need the place value. It's in the hexadecimal system 16. And you're good to go. The character A is at the 0 position and the F is at the 1 position. The calculation therefore looks like this: A * 160+ F * 161. Since the decimal system does not use letters, the characters from the hexadecimal system are simply replaced with the values ​​from the decimal system. The new calculation is called: 10 * 160+15*161. This implies 10 * 1 + 15 * 16, which gives 250 as a result.

A decimal number is converted into a hexadecimal number using the same scheme as for binary numbers. You divide the decimal number by the place value, i.e. 16, and note the remainder. Then repeat the step by dividing the result of the calculation you just made by 16 again. This repeats itself until you get to zero. In the last step, if necessary, convert two-digit residual values ​​into the letters from the character set in the hexadecimal system.

250/16 = 15 remainder 10. 15/16 = 0 remainder 15. 10 corresponds to A, 15 corresponds to F. In the correct order - namely from right to left - this results in the hexadecimal number FA.


2.5 From the binary system to the hexadecimal system and back

Number magic

You now know the binary system and the hexadecimal system. If you remember, I sold you the hexadecimal system as the system into which binary numbers can be transferred particularly easily. But to what extent is the binary number 11101010 particularly easy to convert into the hexadecimal number EA?

The trick is that every four digits in the binary system represent one digit in the hexadecimal system. This means that the binary number 0001 corresponds to 1 in the hexadecimal system, 0010 corresponds to 2, 0011 corresponds to 3 up to 1110 corresponds to E and 1111 corresponds to F. For the next higher number you need a new digit in both the binary system and the hexadecimal system. If you now look at the binary number 11101010, it consists of eight digits. Since every four digits in the binary system represent one digit in the hexadecimal system, you already know that you need exactly two digits. Let's look at the first four digits: 1110. By converting to the decimal system, you get the value 14 (you don't need to do these conversions with a little experience, as you can see the value immediately). 14 is in the hexadecimal system E. This means that the first character is already fixed. The next four binary digits are 1010. By converting to the decimal system, you get the value 10. 10 corresponds to the character A in the hexadecimal system. This completes the conversion: The binary number 11101010 corresponds to EA in the hexadecimal system.

Now that you've figured out the trick, converting a hexadecimal number into the binary system is no longer difficult. You simply convert each digit of the hexadecimal number into four digits of a binary number. Since the hexadecimal number EA consists of two digits, the corresponding binary number must consist of eight digits. At the leftmost position there is the character E, which corresponds to the decimal value 14. Conversion takes you to representation 1110. The second character is A, which corresponds to the decimal value 10. The decimal value 10 looks like this in binary form: 1010. This makes it clear that the hexadecimal number EA is 11101010 in binary form.


2.6 Number systems in practice

At the paying doctor in the practice

By default, numbers that you specify in source code for developing computer programs are written as decimal numbers. However, you can also specify hexadecimal numbers using special identifiers. Binary numbers themselves are seldom supported, since the notation made up of zeros and ones is very error-prone and a better solution is available with hexadecimal numbers.

int i = 0x14;

The code example above is written in the C programming language. The hexadecimal number 14 is assigned to a variable of the type. So that the compiler recognizes that it is a hexadecimal number, the abbreviation is prefixed to the value in C.

Programming languages ​​like C provide another notation.

int i = 014;

By prefixing the number, the number is noted in the octal system. The octal system has the character set 0, 1, 2, 3, 4, 5, 6 and 7 and the place value 8. It has similar advantages as the hexadecimal system: Three binary digits can be converted into one octal digit (with the hexadecimal system four binary digits into one hexadecimal digit ). In practice, however, it is of very little importance.

As you saw in Chapter 1, Programming languages know, programming languages ​​provide bit-by-bit operators, among other things. You can use these operators to edit individual bits. Bits are information that can only assume two states: True or false, set or not set, 1 or 0. Binary numbers are ideal for representing bits.

<html> <head> <title>Allgemeine Grundlagen der Programmierung</title> <script type="text/javascript"> var i = 0xC, j = 0x5; alert(i & j); </script> </head> <body> </body> </html>

In the Javascript code above, two variables named and are defined. The hexadecimal numbers C and 5 are assigned to the variables. The binary representation of the two numbers is 1100 and 0101. The two variables are linked with the operator, which is called the AND operator. This bit-wise operator works as follows: Bits in the same places are compared with one another. If both bits are set - i.e. if the 1 is present in both places - the corresponding bit is also set to 1 in the result.

All you have to do now is to compare bits at the same positions in the two variables: There is a 0 at position 0 of the variable, and a 1 in the variable. Position 0 in the result is therefore set to 0. At the position 1 in the variable there is again a 0, also in the variable. Position 1 in the result is therefore set to 0. At position 2 in the variable there is a 1, also in the variable. Therefore, digit 2 is set to 1 in the result. Position 3 is only set in one of the two variables, so in the result there will be a 0 in position 3. The result of the bitwise operator looks like this: 0100. This corresponds to 4 in the hexadecimal and also in the decimal system.

<html> <head> <title>Allgemeine Grundlagen der Programmierung</title> <script type="text/javascript"> var i = 0xC, j = 0x5; alert(i | j); </script> </head> <body> </body> </html>

Another bitwise operator is called an OR operator. The following rule applies here: As a result, a bit is set when at least one bit is set in an operand. In contrast to the operator, both bits do not have to be set. In order to get the result, individual bits must be compared with each other again: At the 0 position in the variable there is a 0, in the variable a 1. Therefore, a 1 will be at the 0 position in the result. At position 1 there is a 0 in the variable, also in the variable. The digit 1 in the result therefore also has a 0. At the digits 2 and 3 of the variables and there is always at least a 1, so that the digits 2 and 3 are also set to 1 in the result. The result is the binary number 1101, which corresponds to D in the hexadecimal system and 13 in the decimal system.

<html> <head> <title>Allgemeine Grundlagen der Programmierung</title> <script type="text/javascript"> var i = 0xC, j = 0x5; alert(i ^ j); </script> </head> <body> </body> </html>

The operator is called EXCLUSIVE OR. As a result, a bit is only set to 1 if only exactly one bit is set to 1 in the two operands. If the bits in both operands are set to 1, the result will be 0 in the same place.

<html> <head> <title>Allgemeine Grundlagen der Programmierung</title> <script type="text/javascript"> var i = 0xC; alert(~i); </script> </head> <body> </body> </html>

A fairly simple bitwise operator is. In contrast to the other operators, it only requires a single operand, which is specified after. This operator simply reverses the bits: bits set to 1 become bits set to 0, bits set to 0 become bits set to 1. This means that 1100 becomes 0011, which corresponds to the hexadecimal number 3 (which is also a decimal number). The bitwise operator is called the NOT operator.

If your browser shows a negative number as a result instead of 3 when executing the example, this is completely correct. Different results depend on the range of variables. Do not be surprised, but proceed from the description above and ignore a negative result if necessary.


2.7 Exercises

Practice creates masters

You can purchase the solutions to all of the exercises in this book as a ZIP file.

  1. Use a pen and paper to convert the decimal number 40019 into the appropriate hexadecimal number and into the appropriate binary number.

  2. Convert the octal number 143215 with pen and paper into the appropriate hexadecimal number and into the appropriate binary number.

  3. Write a program in Javascript that combines the two values ​​from the first two exercises with the bitwise operators, and. Predict the results of the bitwise operations as decimal values ​​before running the program.

  4. For experts: Write a program in Javascript that receives a decimal number from the user and converts it into a hexadecimal number. You need the operator to implement the program. This operator divides two values ​​and returns the integer remainder as the result.

    To implement the program you need a loop that repeatedly performs a division and combines the remainder values ​​to form a hexadecimal number. Don't forget to convert two-digit remainders to letters.


Copyright © 2001-2010 Boris Schäling