Monday 27 October 2014

PLC Numerical Systems

Numerical systems

 INTRODUCTION
 D.1 Decimal numerical system
 D.2 Binary numerical system
 D.3 Hexadecimal numerical system

People were always difficult to except the fact that something is different from themselves or their way of thinking. It is probably one of the reasons why numerical systems other than decimal are hard to understand. Still, whether we like it or not, reality is quite different. Decimal system used in everyday life is by far less used than binary code, which is the working basis for millions of computers across the world.

Each numerical system rests upon its basis. With decimal numerical system, this basis is 10, with binary it is 2, while with hexadecimal it equals 16. Value of each digit depends on it’s position in the number, represented in certain numerical system. Sum of values of each digit is the value of the number. Binary and hexadecimal systems are especially interesting for this course. Besides these two, decimal system will be detailed, too, for the sake of comparison. Although there is nothing new to tell about decimal system, we will give it a look for its relations with other numerical systems.
Decimal numerical system is defined with its basis 10 and decimal positioning from right to left, and it consists of digits 0,1,2,3,4,5,6,7,8,9. This means that the rightmost digit is multiplied by 1 in total sum, next digit to it is multiplied by 10, next one by 100, etc.

Example:
Operations of addition, subtraction, division and multiplication in decimal numerical system are well known, so we will not detail these.
 
D.2 Binary numerical system
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 digits

To 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.
Example:
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.

Hexadecimal numerical system has number 16 for basis. Therefore, there are 16 different digits used in this system. These are “0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F”. Letters A, B, C, D, E and F represent values 10, 11, 12, 13, 14, 15 and are used for the sake of easier notation. As with binary numerical system, we can apply the same formula here for determining the greatest decimal number that can be represented with a given number of hexadecimal digits.


Example:
162 - 1 = 256 - 1 = 255

Usually, hexadecimal numbers have prefix “$” or “0x” to emphasize the fact that hexadecimal system is used. Thus, number A37E should be represented with $A37E or 0xA37E. No calculations are needed for converting the hexadecimal number to binary system - it is simple substituting of hexadecimal digits with binary ones. Since maximum value of hexadecimal digit is 15, 4 binary digits are required per one hexadecimal.
 
Example:
Check, i.e. converting both numbers to decimal system, gives us value 228 which is correct.

In order to calculate decimal equivalent of hexadecimal number, each digit of number should be multiplied by 16 raised to power equal to the position in the number and then added altogether.
Addition works similar to two previous numerical systems.
Example:
It is required to add the appropriate digits of a number, and if their sum equals 16, that position takes value “0”. Values exceeding 16 should be added to the sum of digits on higher position. First number converted equals 14891, while other is 43457. Their sum is 58348, that is $E3EC converted to decimal numerical system. Subtraction works identically to previously mentioned systems.

Example:
Conversion gives us numbers 11590 and 5970, and the result of subtractions is 5620, that is $15F4 converted to decimal numerical system.
Conclusion
Binary numerical system remains the most commonly used, decimal system the most intelligible, while hexadecimal is somewhere in between. It’s simple conversion to binary system makes it, besides binary and decimal, the most important numerical system to us.

No comments:

Post a Comment