Arithmetic's for Computers (Number Systems and Operations)

Views

THE NUMBER SYSTEM CONVERSION


There are many method can be used to solved the conversion between binary-decimal-hexadecimal number. The most systematic method  to convert the numbers is by using the repeated-division by base 2 (for binary), 10 (for decimal) and 16 (for hexadecimal). But there are another method that are more easy to be used for larger number. Here, I will introduce the other method instead of the repeated-dision.



DECIMAL-BINARY

Example 1

99 in decimal to binary number.

99-64=35-32=3-2=1-1=0
128         64           32           16               8              4              2              1
0              1              1              0              0              0               1               1              base2

Therefore, the answer for 99 base10 is 1100011 base2


Example 2

44.625 in decimal to binary number.

44-32=12-8=4-4=0
64           32           16              8              4            2          1
0              1              0              1               1            0         0

For the decimal point, we calculate like this

0.625 x 2 = 1.25                   ;1
0.25 x 2 = 0.5                      ;0
0.5 x 2 = 1.0                         ;1

Therefore we will get 101100.101 base2


BINARY-DECIMAL

Example 1

100010101010 in binary to decimal number.
1            0          0          0         1           0            1             0            1        0         1          0
2048   1024      512      256     128       64          32           16           8        4          2         1

= 2048 + 128 + 32 + 8 + 2

Then, the answer was 2218 base10


Example 2

1010101.11 in binary to decimal number.

1              0              1             0              1              0              1              .               1              1
64           32            16            8              4              2              1              .             0.50        0.25

= 64 + 16 + 4 + 1 + 0.5 + 0.25

Answer : 85.75 base10


DECIMAL-HEXADECIMAL

Example 1

235 in decimal to hexadecimal number.

235 - (16 x 14) = 11 – (11 x 1) =0

256         16           1
0              14=E      11=B
= EB base16


HEXADECIMAL-DECIMAL

Example 1

5FA in hexadecimal to decimal number.

5 x 256  F x 16     A x 1

(5 x 256) + (15 x 16) + (10 x 1) = 1280 + 240 + 10 = 1530

Answers: 1530 base10



BINARY-HEXADECIMAL

Example 1

100101010 in binary to hexadecimal number.

0001       0010       1010
1              2              10
1              2              A

Answers: 12A base16


HEXADECIMAL-BINARY

Example 1

ADA93 in hexadecimal to binary number.

A             D             A            9              3
10           13           10           9              3
1010       1101       1010       1001       0011       base2

Answers: 10101101101010010011 base2


ADDITION OF BINARY, HEXADECIMAL AND DECIMAL NUMBER


Binary addition

Rules for binary addition


Example 1

10010101 + 101011 in binary

Cout:    0 1 1 1 1 1 1
1 0 0 1 0 1 0 1
+          0 0 1 0 1 0 1 1
1 1 0 0 0 0 0 0

Example 2

10101010111 + 11110001 in binary

Cout:       0 1 1 1 1 1 0 1 1 1
               1 0 1 0 1 0 1 0 1 1 1
+             0 0 0 1 1 1 1 0 0 0 1
               1 1 0 0 1 0 0 1 0 0 0


Hexadecimal addition

In the hexadecimal addition, if the sum of number is greater  than 15, the amount of the sum that exceeds 16base10 will carry a 1 to the next column.

Example 1

45 + 28 in hexadecimal

cout:     0
4              5
+          2              8
6              13                    Answer :   6D base16

Example 2 

23D + 72 in hexadecimal

cout:     0              0
2              3              13       ; since D represent 13 in decimal
+                          7               2
2              10           15                   Answer :    2AF base16



Example 3

37F + A7C in hexadecimal

cout:     0              1
3              7               15       ; A = 10, B = 11, C = 12, D = 13, E = 14, F = 15
+         10             7               12
           13            14              27 

 -          0              0              16
           13            15              11




Two's Complement Representation and Overflow


2's complement is used occasionally to represent numbers in computer system. Normally we would assume that the numbers that we use often is a positive number but microprocessor will process the positive and negative numbers. So 2's compliment. Therefore, this is is used to represent the sign and magnitude of numbers that can be diagnosed.

For example we have an 8-bit number, and when the MSB (most significant bits) = 0, then the number is positive (). But if MSB = 1, then the number is negative (-). Surplus 7 numbers are its magnitude. The first bit from the right is the LSB (least significant bit).


magnitude

MSB






LSB


        
Sign bit  0 = (+)
                   1 = (-)


Binary subtraction
example 1

32 - 15 in binary

32 base10 = 100000 base2
since the number is positive, the 8 th bit called MSB is assigned with 0,
thus, 32 base10 = 00100000 base2

15 base10 = 1111 base2
1s complement; - change all the 0 bits to 1 bits and vice versa for all the 7 significant bits
      1110000 base2
2s complement; - add 1
      1110001 base2
since the number is negative, the MSB is assigned with 1,
thus, 15 base10= 11110001 base2

now do 32 + (-15);

cout:           1 1 0 0 0 0 0
                  0 0 1 0 0 0 0 0
         +       1 1 1 1 0 0 0 1
               1 0 0 0 1 0 0 0 1

the answer is 100010001 base2; - the answer contains 9 bits, so we have to discard the first bit on the left which is the carry out and the MSB is 0 which meant it is a positive answer.
Therefore the answer is 0 001 0001 base2 = 17 base10.

check and see, 32 - 15 = 17.

Hexadecimal subtraction
example 1

A5 base16 -87 base16

A5 base16 = 1010 0101 base2

87 base16 = 1000 0111 base2
1s complement; -> 0111 1000
2s complement; -> 0111 1001
MSB is assigned with 1, -> 1 0111 1001

A5 base16 +(- 87 base16);

cout:           1 1100 001
                  0 1010 0101
            +    1 0111 1001
                10 0001 1110

the answer is 1 1101 1110 base2; - the answer contains 10 bits, so we have to discard the first bit on the left which is the carry out and MSB is 0 which meant it is a positive answer.
therefore the answer is 0 0001 1110 base2 = 1E base16.

example 2

we have a shortcut to find the solution for the above question, where we no need to convert the hexadecimal into binary, meantime solving it using base16 by itself.
here we go:

A5 base16 -87 base16

87 base16
1s complement; -> 78 base16 (same as for binary where a+b=1, here in hexadecimal a+b=15)
2s complement; -> 79 base16 (add 1)

A5 base16 +(- 87 base16);

cout:            10
                     A5
               +    79
                   11E  (5+9=14 -> E in hexadecimal; A+7=17, 17>16 so 17-16=1, quotient is the carry out)


the answer is 11E base16; - the answer contains a carry out which can be excluded.
therefore the answer is 1E base16.


Binary multiplication

similarly same as binary addition
example 1

111 base2 x 11 base2

                             111
                       x        11
cout:                    1110
                               111
                      +     111  
                           10101

therefore 111x11 = 10101 base2 (7x3 = 21)

for hexadecimal multiplication, it is advisable to convert it into binary and do the solution.


Binary division
example1

10101 base2 / 111 base2

                           11
              111/10101
                 -     111  
                     00111
                 -       111
                             0 

therefore 10101/111 = 11 base2 (21/7 = 3)

for hexadecimal division, it is advisable to convert it into binary before doing calculation.

1 comment: