Binary numerical system is quite different from the decimal that we got used to in common life. Its basis is 2 and each digit can have one of two values, “1” or “0”. Binary numerical system is used for computers and microcontrollers, because it is much easier for processing than decimal. Usually, binary number consists of 8, 16 or 32 binary digits. Origins of this division are irrelevant for this course, so we will just take it for granted. Example: 10011011 - binary number with 8 digitsTo understand the logic of binary numbers, let us have an example. Let’s assume that we have a cabinet with four drawers and that we should tell someone to bring us something from one of these. Nothing simpler, we could say “in the lower row on the left” and it would be quite sufficient. However, if it must be done without this kind of orientation, left, right, up, down and the likes, then we have a problem. There is a plenty of solutions for the situation, but we should look for the best and the most efficient! Let us mark the columns with A and rows with B. If A=1, we assume the upper row of drawers, and if A=0 we assume the lower. Similar with columns, B=1 is the left column and B=0 is the right column (following picture). Now, it is easier to explain which drawer we think of, just use one of the four combinations 00, 01, 10 or 11. This “naming” of each drawer is nothing more than binary nomenclature of numbers, that is, converting decimal numbers into binary system. In short, labels “first, second, third and fourth” are substituted with “00, 01, 10 and 11”.
We still need to understand the logic of binary numerical system, i.e. how to get the decimal value of a number out of the sequence consisting of ones and zeros. This procedure is called conversion of binary number to decimal value.
As it can be seen, conversion of binary number to decimal value is done by totaling the sum on the right. Depending on the position in the binary number, digits carry different “weight” multiplied by themselves, and totaling them all gives us an understandable decimal number.
Let’s further assume that there are marbles in each of the drawers, 2 in the first, 4 in the second, 7 in the third and 3 in the fourth. Let the person opening the drawers also use the binary system. Under these conditions, question would be “How many marbles are there in 01?”, and the answer would be “There are 100 marbles in 01!” Notice that both question and answer are clear, although we did not use common terminology. Further, notice that two digits are sufficient for decimal numbers from 0 to 3, and that all values greater than 3 require additional binary digits. Thus, for 0 ~ 7 range, three digits are sufficient, four digits cover the range 0 ~ 15, etc. Simply put, the greatest decimal number that can be represented with n binary digits is 2 raised to power n, decreased by one.
24-1 = 16 - 1 = 15
So, 4 binary digits cover decimal values from 0 to 15, including the values “0” and “15”, which is 16 different values.
Arithmetical operations that exist in decimal numerical system also apply in binary system. In this chapter, we will cover only addition and subtraction, for simplicity sake.
Basic rules that apply to binary addition are:
Addition works similar to decimal numerical system - we add the digits of the same weight. If both digits added are zero, the result remains zero, while “0” and “1” total “1”. Two ones give zero, but one is carried to the left position.
We can do the check by converting these numbers to decimal system and adding them. Value of the first number is 10, value of the second is 9 and 19 as result, which means that operation was done correctly. Problem occurs when the result is greater than can be represented with given number of binary digits. There are various solutions, one of them being expanding the number of binary digits like in the example below.Subtraction works on the same principles as addition does. Two zeros give zero in result, as do two ones, while subtraction of one from zero requires borrowing one from the higher position in binary number. Example: Conversion of numbers to decimal system gives as values 10 and 9, with the result of subtraction of 1, which is correct.