Programming and Graphics

(Kiana) #1

10 Introduction to C++ Programming and Graphics


1.3 Binarysystemarithmetic


The English word “arithmetic” derives from the Greek wordαριθμoς,which
means “number.” We can add, subtract, multiply, and divide two numbers in
their binary number representation using rules that are similar to those for the
decimal representation.


Addition


To compute the sum of two binary numbers, we add the corresponding
digits according to four basic rules:


(0) 2 + (0) 2 = (0) 2
(0) 2 + (1) 2 = (1) 2
(1) 2 + (0) 2 = (1) 2 (23)
(1) 2 + (1) 2 = (0) 2 , and carry (1) 2 to the left.

For example, following these rules, we find


(111) 2 + (111) 2 = (1110) 2 , (24)

which is equivalent to 7 + 7 = 14. The sequence of incremental operations that
led us to this result is:


+ 111 110 110 100 0100 0000 0000
+ 111 110 100 100 1000 1000 0000
carry: + 1 1 1
---- --- --- --- ---- ---- ----
0 0 10 10 110 1110

The bits in the third row are the “carry.”


To compute the difference between two binary numbers, we subtract the
corresponding digits according to four basic rules:


(0) 2 −(0) 2 = (0) 2
(1) 2 −(1) 2 = (0) 2
(1) 2 −(0) 2 = (1) 2
(0) 2 −(1) 2 = (1) 2 , with (1) 2 borrowed from the left. (25)

For example, following these rules, we find


(1000) 2 −(11) 2 = (101) 2 , (26)

which is equivalent to 8−3 = 5. The sequence of incremental operations that
led us to this result is:

Free download pdf