Chapter 3
Page 42
-
Four bits are required to store a single decimal digit. Many codes could be used. This one uses the binary number system.
digit code digit code 0 0000
5 0101
1 0001
6 0110
2 0010
7 0111
3 0011
8 1000
4 0100
9 1001
-
Binary addition
Let carry = 0 Repeat for each i = 0,...,(n - 1) // starting in ones place sumi = (xi + yi) % 2 // remainder carry = (xi + yi) / 2 // integer division
- Hexadecimal addition
Let carry = 0 Repeat for each i = 0,...,(n - 1) // starting in ones place sumi = (xi) + yi) % 16 // remainder carry = (xi + yi) / 16 // integer division
- Binary subtraction
Let borrow = 0 Repeat for i = 0,··· ,(N − 1) If yi ≤ xi Let differencei = xi − yi Else Let j = i + 1 While (xi = 0) and (j < N) Add 1 to j If j = N Let borrow = 1 Subtract 1 from j Add 2 to xi While j > i Subtract 1 from xi Subtract 1 from j Add 2 to xi Let differencei = xi − yi
- Hexadecimal subtracton
Let borrow = 0 Repeat for i = 0,··· ,(N − 1) If yi ≤ xi Let differencei = xi − yi Else Let j = i + 1 While (xi = 0) and (j < N) Add 1 to j If j = N Let borrow = 1 Subtract 1 from j Add 16 to xi While j > i Subtract 1 from xi Subtract 1 from j Add 16 to xi Let differencei = xi − yi
Page 49
- Signed decimal to two’s complement binary.
If x >= 0 Convert x to binary Else Negate x Convert the result to binary Compute the 2s complement of the result in the binary domain
- Two’s complement in binary to signed decimal.
If high-order bit of x is 0 Convert x to decimal Else Compute the 2s complement of x Compute the decimal equivalent of the result Place a minus sign in front of the decimal equivalent
- Two’s complement binary to signed decimal
0x1234
= +46600xffff
= -10x8000
= -327680x7fff
= +32767
- Signed decimal to 2s complement binary
- +1024 =
0x0400
- -1024 =
0xfc00
- -256 =
0xff00
- -32767
0x8001
- +1024 =
Page 54
- Three-bit arithmetic using Decoder Ring
- Start at the tic mark for 1, move 3 tic marks CW, giving
100
= 4. We did not pass the tic mark at the top, soCF
=0
, and the result is right. - Start at the tic mark for 3, move 4 tic marks CW, giving
111
= 7. We did not pass the tic mark at the top, soCF
=0
, and the result is right. - Start at the tic mark for 5, move 6 tic marks CW, giving
011
= 3. We did pass the tic mark at the top, soCF
=1
, and the result is wrong. - Start at the tic mark for +1, move 3 tic marks CW, giving
101
= -3. We did pass the tic mark at the bottom, soOF
=1
, and the result is wrong. - Start at the tic mark for -3, move 3 tic marks CCW, giving
010
= +2. We did pass the tic mark at the bottom, soOF
=1
, and the result is wrong. - Start at the tic mark for +3, move 4 tic marks CCW, giving
111
= -1. We did not pass the tic mark at the bottom, soOF
=0
, and the result is right.
- Start at the tic mark for 1, move 3 tic marks CW, giving
- Eight-bit addition, unsigned and signed
0x55
+0xaa
=0xff
, unsigned right, signed right0x55
+0xf0
=0x45
, unsigned wrong (CF
), signed right0x80
+0x7b
=0xfb
, unsigned right, signed right0x63
+0x7b
=0xde
, unsigned right, signed wrong (OF
)0x0f
+0xff
=0x0e
, unsigned wrong (CF
), signed right0x80
+0x80
=0x00
, unsigned wrong (CF
), signed wrong (OF
)
- Sixteen-bit addition, unsigned and signed
0x1234
+0xedcc
=0x0000
, unsigned wrong (CF
), signed right0x1234
+0xfedc
=0x1110
, unsigned wrong (CF
), signed right0x8000
+0x8000
=0x0000
, unsigned wrong (CF
), signed wrong (OF
)0x0400
+0xffff
=0x03ff
, unsigned wrong (CF
), signed right0x07d0
+0x782f
=0x7fff
, unsigned right, signed right0x8000
+0xffff
=0x7fff
, unsigned wrong (CF
), signed wrong (OF
)