Binary numbers Author: Stefan Höst In computer arithmetic binary vectors are used to represent numbers. This text is a short version of how binary vectors can represent numbers (integers). Also the Hexa-decimal notation will be introduces. Our normal number representation is based on the decimal numbers, which has the base 10. That is, all numbers are expressed in powers of 10. For example the number 213 can be written 21310 = 2 · 102 + 1 · 101 + 3 · 100 When representing the same number in a computer, we should not use the base 10 since we have only 0s and 1s. Instead we use the base 2. The same number as above can then be represented by 11010101, since 110101012 = 1 · 27 + 1 · 26 + 0 · 25 + 1 · 24 + 0 · 23 + 1 · 22 + 0 · 21 + 1 · 20 = 21310 In general a binary vector of length n can have 2n different combinations and, hence, we can represent 2n different numbers. The natural binary coded decimal (NBCD) procedure above will then map the vector and integer by the following sum (xn−1 , . . . , x1 , x0 )2 = n−1 X xi 2i , xi ∈ {0, 1} i=0 It will be able to represent the integers from 0 to 2n − 1 (i.e. Z2n ). Of course, as in our normal decimal notation, the position of the digit is important of the value it represents. The left most bit in the vector has the highest value, actually one more than all the others together. This bit is the most significant bit (msb). Similarly, the right most bit has the lowest value, i.e. 1, and is the least significant bit (lsb). Even though the inverse function of the mapping above is not used in this course, for completeness the next example shows the procedure. Example 1 The inverse mapping can be derived by using Euclid’s division theorem, n = 2q + r, iteratively. To write n = 245 in binary form we use the following derivations. 245 = 2 · 122 + 1 (lsb) 122 = 2 · 61 + 0 61 = 2 · 30 + 1 30 = 2 · 15 + 0 15 = 2 · 7 + 1 7=2·3+1 3=2·1+1 1=2·0+1 ⇒ 24510 = 111101012 1 (msb) To see where this comes from we give a complementary derivation that can be done in parallel 245 = 2 · 122 + 1 = 2 · (2 · 61) + 1 = 22 · 61 + 1 = 22 · (2 · 30 + 1) + 1 = 23 · 30 + 22 + 1 = 23 · (2 · 15) + 22 + 1 = 24 · 15 + 22 + 1 = 24 · (2 · 7 + 1) + 22 + 1 = 25 · 7 + 24 + 22 + 1 = 25 · (2 · 3 + 1) + 24 + 22 + 1 = 26 · 3 + 25 + 24 + 22 + 1 = 26 · (2 + 1) + 25 + 24 + 22 + 1 = 27 + 26 + 25 + 24 + 22 + 1 = |1 · 27 + 1 · 26 + 1 · 25 + 1 · 24 {z + 0 · 23 + 1 · 22 + 0 · 21 + 1 · 2}0 (11110101)2 Hexa-decimal numbers Even though binary numbers and binary vectors are well suited for computers, humans have a bit of a problem to view them. Instead we try to find another representations of the vectors, but it should have an easier mapping than between binary and decimal. The answer to this is often to use the base 16 instead, which forms the Hexa-decimal number representation. Then we need to use 16 digits. This is solved by using the normal decimal digits complemented by the letters A–F representing 10–15, i.e. the set of digits are H = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F } The value of a vector is written as before (xn−1 , . . . , x1 , x0 )16 = n−1 X xi 16i , xi ∈ H i=0 Going back to number we started with, 213, we can then have a look at the value 110101012 = 1 · 27 + 1 · 26 + 0 · 25 + 1 · 24 + 0 · 23 + 1 · 22 + 0 · 21 + 1 · 20 = (1 · 23 + 1 · 22 + 0 · 21 + 1 · 20 )24 + (0 · 23 + 1 · 22 + 0 · 21 + 1 · 20 )20 = 13 · 161 + 5 · 160 = D516 Generalizing the above example we see that there is a direct and easy translation between binary and Hexa-decimal representations. Given a binary vector we start by taking the four least significant bits and form the least significant Hexa-decimal digit. Then take the next four bits to form the next Hexa-decimal digit and so on. Conversion from Hexadecimal to binary is preformed by translating each of the Hexa-decimal digits to a four tipple of bits. Example 2 The 32 bit binary vector below can be represented as (0001 1010 0110 0000 0101 1111 0100 1001)2 = 1A605F 4916 It is more tedious to use the decimal representation 442523465. 2 Conversions It is also convenient to use MATLAB for conversion: > > > > dec2bin(integer) bin2dec(’bin vector’) dec2hex(integer) hex2dec(’hex vector’) 3