Nitric Acid 0 2n(n5) 24-9824 v2 1 1 1 23-Jul-2010 Us-English-local on Apr-29-2013
Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices:...
Transcript of Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices:...
![Page 1: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/1.jpg)
2.1
![Page 2: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/2.jpg)
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
![Page 3: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/3.jpg)
2.3
•––––
–
![Page 4: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/4.jpg)
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
![Page 5: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/5.jpg)
2.5
![Page 6: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/6.jpg)
2.6
•
––
•
••
•–
![Page 7: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/7.jpg)
2.7
•
•–
–•••
CS:APP 2.3.3
![Page 8: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/8.jpg)
2.8
•
––
•
010011
101100
+ 1
101101
-32 16 8 4 2 1
![Page 9: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/9.jpg)
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
![Page 10: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/10.jpg)
2.10
![Page 11: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/11.jpg)
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.
![Page 12: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/12.jpg)
2.12
•–––
••
–
––
•
CS:APP 2.3.1CS:APP 2.3.2
![Page 13: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/13.jpg)
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
![Page 14: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/14.jpg)
2.14
•
–•
1001+ 0011
1100
11(9)(3)
(12)
(-7)(3)
(-4)
If unsigned If signed
![Page 15: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/15.jpg)
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
![Page 16: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/16.jpg)
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)
![Page 17: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/17.jpg)
2.17
•…
–
–
![Page 18: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/18.jpg)
2.18
![Page 19: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/19.jpg)
2.19
•
•
––
CS:APP 2.3.1
CS:APP 2.3.2
![Page 20: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/20.jpg)
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
![Page 21: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/21.jpg)
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
![Page 22: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/22.jpg)
2.22
•
•––
1101+ 0100
0001
11(13)(4)
(17)
(-3)(4)
(+1)
0110+ 0101
1011
10(6)(5)
(11)
(6)(5)
(-5)
![Page 23: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/23.jpg)
2.23
•
•––
(7)(8)
(-1)
(7)(-8)(15)
0111- 1000
0111_01110111
+ 11111 (15) (-1)
![Page 24: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/24.jpg)
2.24
![Page 25: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/25.jpg)
2.25
•char v
–• v = 1;
–• v = 0xf0;
–• v = 0;
–•
––––
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? 1
1 1 1 1 ? ? ? ?
? ? ? ? ? ? 0 0
![Page 26: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/26.jpg)
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'
![Page 27: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/27.jpg)
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
![Page 28: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/28.jpg)
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
![Page 29: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/29.jpg)
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}
![Page 30: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/30.jpg)
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);}
![Page 31: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/31.jpg)
2.31
![Page 32: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/32.jpg)
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… …
![Page 33: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/33.jpg)
2.33
•–
–
•–––
• …
• …
Right shift
Left shift
Right shift
Left shift
0
0
0
Copies of MSB are shifted in
![Page 34: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/34.jpg)
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
![Page 35: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/35.jpg)
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
![Page 36: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/36.jpg)
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
![Page 37: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/37.jpg)
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
▪▪
▪▪
![Page 38: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/38.jpg)
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
![Page 39: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/39.jpg)
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
![Page 40: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/40.jpg)
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
![Page 41: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/41.jpg)
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; }
![Page 42: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/42.jpg)
P1.42
![Page 43: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/43.jpg)
2.43
![Page 44: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/44.jpg)
2.44
• + - * /
•–––
•
![Page 45: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/45.jpg)
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
![Page 46: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/46.jpg)
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
![Page 47: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/47.jpg)
2.47
0110
+ 0111
(6)
(7)
( )
8 4 2 1
![Page 48: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/48.jpg)
2.48
0110
+ 0111
1101
(6)
(7)
(13)
110
8 4 2 1
![Page 49: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/49.jpg)
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
![Page 50: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/50.jpg)
2.50
•–
4 D16+ B 5161 0 216
1 1
256 16 1
![Page 51: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/51.jpg)
2.51
![Page 52: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/52.jpg)
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
![Page 53: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/53.jpg)
2.53
••••
1010* 1011
10101010_
0000__+ 1010___
01101110
M (Multiplicand)Q (Multiplier)
PP(Partial Products)
P (Product)
![Page 54: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/54.jpg)
2.54
•
•
1001* 0110
00001001_
1001__+ 0000___00110110
= -7= +6
= +54
…
1001* 0110
000000001111001_111001__
+ 00000___11010110
= -7= +6
= -42
…
![Page 55: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/55.jpg)
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.
![Page 56: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/56.jpg)
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
![Page 57: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/57.jpg)
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
![Page 58: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/58.jpg)
3.58
10 1 0 1 1
0
10 (2) goes into 1, 0 times. Since it doesn’t, bring in the next bit.
![Page 59: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/59.jpg)
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
![Page 60: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/60.jpg)
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
![Page 61: Unit 2 - GitHub Pages · Unit 2 Integer Operations. Summary of Unit 1 2.2 • n bits, 2n choices: [0, 2n-1] unsigned and [-2n-1, 2n-1-1] signed range ... 1 0 0 1 0 0 1 1 = -109 Unsigned](https://reader033.fdocuments.in/reader033/viewer/2022042810/5f9d383e29a1bb6b1636a67e/html5/thumbnails/61.jpg)
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