上QQ阅读APP看书,第一时间看更新
Binary arithmetic
Since a byte is the common unit used in computers, let's play with it. We can start with basic arithmetical functions: addition, subtraction, multiplication, and division. The pencil-and-paper method is still a strong method for doing binary math. Binary arithmetic is similar to doing arithmetic in decimal numbers. The difference is that there are only two numbers used, 1 and 0.
Addition is carried out as follows:
1b 10101b
+ 1b + 1111b
10b 100100b
An example of subtraction is as follows:
10b 1101b
- 1b - 111b
1b 110b
Multiplication is carried out as follows:
101b 1b x 1b = 1b
x 10b 1b x 0b = 0b
000
101
1010b
Division in binary works as follows:
1010b 1000b
10b | 10100b 11b | 11010b
-10 -11
010 0010
-10 -000
00 10b (remainder)
-0
0