2.1
2.2
••••• unsigned char short int long
128 64 32 16 8 4 2 1
1 0 0 1 0 0 1 1 = +147
-128 64 32 16 8 4 2 1
1 0 0 1 0 0 1 1 = -109
9 3 = +1470x
2.3
•––––
–
2.4
• + - * /
•––
•
(10)
(5)
(5)
0 1 1 1
+ 0 0 1 1
1 0 1 0
(7)
(3)
(10)
1 1 1
8 4 2 1
1 0 1 0
- 0 1 0 1
0 1 0 1
0 0
8 4 2 1
11
2.5
2.6
•
––
•
••
•–
2.7
•
•–
–•••
CS:APP 2.3.3
2.8
•
––
•
010011
101100
+ 1
101101
-32 16 8 4 2 1
2.9
101010
010101
+ 1
010110
-32 16 8 4 2 1
101001
+ 1
101010
0000
1111
+ 1
0000
1000
0111
+ 1
1000
1 2
3
4 0001 => 1111
2.10
2.11
12 1239
1011
487 56
12 1239
1011
487 56
00 010203
9899
04...
.
00 010203
9899
04...
.
00000001
0010
0011
1110
1111
0100...
.
00000001
0010
0011
1110
1111
0100...
.
10’s complement04 - 02 =
04 + (100 - 02) =04 + 98
2’s complement0100 - 0010 =
0100 + (10000 - 0010) =0100 + 1110
Clock Analogy4 - 2 =
4 + (12 - 2) =4 + 10
When using modulo arithmetic, subtraction can always be converted to addition.
2.12
•–––
••
–
––
•
CS:APP 2.3.1CS:APP 2.3.2
2.13
••
0011
+ 0010
0101
(3)
(2)
(5)
1101
+ 0010
1111
(-3)
(2)
(-1)
0011
+ 1110
0001
(3)
(-2)
(1)
1101
+ 1110
1011
(-3)
(-2)
(-5) -8+2+1
0000 0000
1110 Drop final carry-out 1100
2.14
•
–•
1001+ 0011
1100
11(9)(3)
(12)
(-7)(3)
(-4)
If unsigned If signed
2.15
•
•
0011- 0010
(+3)(+2)
Drop final carry-out
1111_00111101
+ 10001
Bit flip of +2
Add 1
1101- 1110
(-3)(-2)
1_11010001
+ 11111
Bit flip of -2
Add 1
2.16
•
––
•
(12)(2)
(-4)(2)
If unsigned If signed
1100- 0010
11_1_11001101
+ 11010
~B
Add 1
A
If unsigned If signed
(10) (-6)
2.17
•…
–
–
2.18
2.19
•
•
––
CS:APP 2.3.1
CS:APP 2.3.2
2.20
00000001
0010
0011
0100
0101
01100111
1000
1111
1110
1101
1100
1011
1010
1001
0+1
+2
+3
+4
+5
+6
+7+8
+9
+10
+11
+12
+13
+14
+15
10
Plus 7
2.21
00000001
0010
0011
0100
0101
01100111
1000
1111
1110
1101
1100
1011
1010
1001
0+1
+2
+3
+4
+5
+6
+7-8
-7
-6
-5
-4
-3
-2
-1
2.22
•
•––
1101+ 0100
0001
11(13)(4)
(17)
(-3)(4)
(+1)
0110+ 0101
1011
10(6)(5)
(11)
(6)(5)
(-5)
2.23
•
•––
(7)(8)
(-1)
(7)(-8)(15)
0111- 1000
0111_01110111
+ 11111 (15) (-1)
2.24
2.25
•char v
–• v = 1;
–• v = 0xf0;
–• v = 0;
–•
––––
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? 1
1 1 1 1 ? ? ? ?
? ? ? ? ? ? 0 0
2.26
•••
X Y AND0 0 00 1 01 0 01 1 1 P
ass
Forc
e '0
'
X Y XOR0 0 00 1 11 0 11 1 0
Pas
sIn
vert
X Y OR0 0 00 1 11 0 11 1 1
Pas
sFo
rce
'1'
2.27
•
0xa5AND 0xf0
0xa0
1010 0101& 1111 0000
1010 0000
0xa5OR 0xf0
0xf5
1010 0101| 1111 0000
1111 0101
0xa5XOR 0xf0
0x55
1010 0101^ 1111 0000
0101 0101
#include <stdio.h> // C-Library // for printf()
int main(){ char a = 0xa5; char b = 0xf0;
printf("a & b = %x\n", a & b); printf("a | b = %x\n", a | b); printf("a ^ b = %x\n", a ^ b); printf("~a = %x\n", ~a); return 0;}
NOT 0xa50x5a
~ 1010 01010101 1010
C bitwise operators:& = AND| = OR^ = XOR~ = NOT
2.28
•–
•–
•–
0000 0001=T&& 0000 0010=T
0000 0001=T
0000 0001& 0000 0010
F = 0000 0000
! 0000 0001=T0000 0000=F
~ 0000 0001T = 1111 1110
#include <stdio.h> int main() { int x = 1, y = 2; int z1 = x && y; // 1 int z2 = x & y; // 0 printf("z1=%d, z2=%d\n",z1,z2); char z = 1; if(!z) { printf("L1\n"); } // F if(~z) { printf("L2\n"); } // T return 0;}
…
!! 0101 0111=T0000 0001=T
2.29
•
•
0101 1001=x#include <stdio.h> int main() { int x = 0x59; int y = 0xd3; x = x ^ y; // x = bitwise diff y = x ^ y; // flip y if different x = x ^ y; // flip y if flipped}
1101 0011=y
0101 1001=x^ 1101 0011=y
1000 1010=x 1000 1010=x^ 1101 0011=y
0101 1001=y1000 1010=x^ 0101 1001=y
1101 0011=x
#include <stdio.h>
int main() { int x = 0x59; int y = 0xd3; int temp = x; // save x x = y; // overwrite x y = temp; // overwrite y}
2.30
•
•
int isOdd(int x) { /* Isolate the lowest bit */ return x & 1;}
int isMultOf4(int x) { /* Check if 2 LSBs are both 0 */ return !(x & 3);}
2.31
2.32
•––
••
––
0 0 0 0 0 0 1 1
0 0 0 0 1 1 0 0
0 0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 1 0
0 0… …
2.33
•–
–
•–––
• …
• …
Right shift
Left shift
Right shift
Left shift
0
0
0
Copies of MSB are shifted in
2.34
••
––
0 0 ... 0 0 1 1 ... 0 1 1 0 0 0 0 0… …
0 ... 0 1 1 0 0 = +12
= +3 = +96
0x 0000 000C
0x00000003 0x00000060
2.35
••
–•
–
1 1 1 ... 1 1 1 1 ..1 1 0 0 0 0
… …
1 1 ... 1 1 0 0 = -4
= -1 = -16
…
0xFFFFFFFC
0xFFFFFFFF 0xFFFFFFF0
2.36
•
•
–
•
––
int mul17(int x) { return 17*x; }
17= 1 0 0 0 12
124816
sall $4, %edx addl %edx, %eax
int mul17(int x) { int x16 = x << 4; return x16 + x; }
CS:APP 2.3.6
2.37
▪▪
▪▪
5 = 0 1 0 1
124-8
5>>1 = 0 0 1 0
124-8
1
0.5
-5>>1 = 1 1 0 1
124-8
1
0.5
-5 = 1 0 1 1
124-8
▪▪
▪▪
2.38
05 >> 10 0 1 0
124-8
1
0.5
0-5 >> 1
-4-8 -3 -2.5
2.52
+5>>1
124-8
1
0.5
-5>>1
1 1 0 1
0 2.52-2-2.5
2.39
• x >> k– ≥
–
•–
–
-4>>1 = 1 1 1 0
-4 = 1 1 0 0
-5>>1 = 1 1 0 1
-5 = 1 0 1 1
-5 1 0 1 1 +1 + 1
-4 1 1 0 0-4>>1 = 1 1 1 0
2.40
•––
•––
•––
-8 = 1 0 0 0-8>>2 = 1 1 1 0
-7 = 1 0 0 1-7>>2 = 1 1 1 0
-7 1 0 0 1 +3 + 1 1
-4 1 1 0 0-4>>2 = 1 1 1 1
-8 1 0 0 0 +3 + 1 1
-5 1 0 1 1-5>>2 = 1 1 1 0
2.41
#define M /* mystery number 1 */#define N /* mystery number 2 */
int arith(int x, int y) { int result = x*M + y/N; return result;}
/* Translation of assembled code for a given value of M and N */
int optarith(int x, int y) { int t = x; x <<= 5; x -= t; if (y < 0) y += 3; y >>= 2; return x + y; }
P1.42
2.43
2.44
• + - * /
•–––
•
2.45
•••
0
+ 0
00
no need to carry
sum bit
0
+ 1
01
no need to carry
sum bit
1
+ 0
01
no need to carry
sum bit
1
+ 1
10
carry 1 into next column of bits
sum bit
1
2.46
(10)
(5)
(5)
0 1 1 1
+ 0 0 1 1
1 0 1 0
(7)
(3)
(10)
1 1 1
8 4 2 1
1 0 1 0
- 0 1 0 1
0 1 0 1
0 0
8 4 2 1
11
2.47
0110
+ 0111
(6)
(7)
( )
8 4 2 1
2.48
0110
+ 0111
1101
(6)
(7)
(13)
110
8 4 2 1
2.49
0110
+ 0111
1101
(6)
(7)
(13)
0
+ 1
01
0
carry bit sum bit
0110
+ 0111
1101
(6)
(7)
(13)
1
+ 1
10
10 0
carry bit sum bit
0110
+ 0111
1101
(6)
(7)
(13)
1
+ 1
11
110 1
carry bit sum bit
0110
+ 0111
1101
(6)
(7)
(13)
0
+ 0
01
110 1
carry bit sum bit
1 2
43
2.50
•–
4 D16+ B 5161 0 216
1 1
256 16 1
2.51
2.52
•
–•
0 1 1 0
* 0 1 0 1
0 1 1 0
(6)
(5)
Sum of the partial products
0 0 0 0 0 1 1 0
+ 0 0 0 0
0 0 1 1 1 1 0
Partial Products
2.53
••••
1010* 1011
10101010_
0000__+ 1010___
01101110
M (Multiplicand)Q (Multiplier)
PP(Partial Products)
P (Product)
2.54
•
•
1001* 0110
00001001_
1001__+ 0000___00110110
= -7= +6
= +54
…
1001* 0110
000000001111001_111001__
+ 00000___11010110
= -7= +6
= -42
…
2.55
•––
1100* 1010
000000001111100_000000__
+ 11100___11011000
= -4= -6
= -40
… …
1100* 1010
000000001111100_000000__
+ 00100___00011000
= -4= -6
= +24
Place Value: -8Multiply -4 by -1
Main Point: Signed and Unsigned Multiplication require different techniques… Thus different assembly instructions.
2.56
•
•
10 1 0 1 1
0 1 0 1 r.1
-1 0
0 1-0 0
1 1-1 0
0 1
(2)10 (11)10
(5 r.1)10
2.57
••
10 1 0 1 1
0 1 0 1 r.1
-1 0
0 1-0 0
1 1-1 0
0 1
(2)10 (11)10
(5 r.1)10
3.58
10 1 0 1 1
0
10 (2) goes into 1, 0 times. Since it doesn’t, bring in the next bit.
3.59
10 (2) goes into 10, 1 time. Multiply, subtract, and bring down the next bit.
10 1 0 1 1
0 1
-1 0
0 1
3.60
10 (2) goes into 01, 0 times. Multiply, subtract, and bring down the next bit.
10 1 0 1 1
0 1 0
-1 0
0 1-0 0
1 1
3.61
10 (2) goes into 11, 1 time. Multiply and subtract. The remainder is 1.
10 1 0 1 1
0 1 0 1 r.1
-1 0
0 1-0 0
1 1-1 0
0 1