Lecturer: Mahdi Soltani
Sharif University of Technology
Department of Computer Engineering
Fundamentals of Programming (C)
Group 7
Lecture 3 Number Systems
Fundamentals of Programming (C) Group 7 Lecturer: Mahdi Soltani - - PowerPoint PPT Presentation
Fundamentals of Programming (C) Group 7 Lecturer: Mahdi Soltani Lecture 3 Number Systems Sharif University of Technology Department of Computer Engineering Number Systems Lecture 3 Outline Numeral Systems Computer Data Storage
Lecturer: Mahdi Soltani
Sharif University of Technology
Department of Computer Engineering
Group 7
Lecture 3 Number Systems
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
2/52
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
3/52
– Computers are built using digital circuits – Inputs and outputs can have only two values: 0 and 1
– Writing out a binary number such as 1001001101 is tedious, and prone to errors
used by computers
– Octal number system (base 8) – Hexadecimal number system (base 16)
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
4/52
Hexadecimal Octal Binary Decimal 1 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 9 9 A (decimal value of 10) B (decimal value of 11) C (decimal value of 12) D (decimal value of 13) E (decimal value of 14) F (decimal value of 15)
Base B : 0 ≤ digit ≤ B -1
Base 2 : 0 ≤ digit ≤ 1 (2-1) Base 8 : 0 ≤ digit ≤ 7 (8 -1) Base 16 : 0 ≤ digit ≤ 15 (16 -1)
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
5/52
1 OR
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
6/52
1 1 1 1 2 1 1 3 1 2 1 1 1 2 1 1 3 1 4 1 1 5 1 1 6 1 1 1 7
1 2 3 1 1 1 2 1 1 3 1 4 1 1 5 1 1 6 1 1 1 7 1 8 1 1 9 1 1 10 1 1 1 11 1 1 12 1 1 1 13 1 1 1 14 1 1 1 1 15
1 1
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
7/52
1 2 3 4 5 6 7 1 1 1 2 1 1 3 … … … … … … … … …
Bit Order One Bit Memory Address 1 2 3 …
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
8/52
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
9/52
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
10/52
... 3
2 3 4 5 6 7 .. .
3
2 3 4 5 6 7 1 1 . 1 1 1 1 1
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
11/52
2-4 2-3 2-2 2 1
21 22 23 24 25 26 27
3
2 3 4 5 6 7 1 1 . 1 1 1 1 1
B = 2 … Position Weight Decimal Point
102 101 100 10-1 10-2 100s 10s 1s 1/10s 1/100s 9 8 7 . 5 6
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
12/52
0.0625 0.125 0.25 0.5 1 2 4 8 16 32 64 128
3
2 3 4 5 6 7 1 1 . 1 1 1 1 1 0.0625 * 1 0.125 * 0.25 * 1 0.5 * 1 * 1 2 * 1 4 * 8 * 1 16 * 32 * 64 * 1 128 * 1 0.0625 0.25 . 1 2 8 64 128
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
13/52
0.0625 0.125 0.25 0.5 1 2 4 8 16 32 64 128
3
2 3 4 5 6 7 1 1 . 1 1 1 1 1 0.0625 * 1 0.125 * 0.25 * 1 0.5* 1 * 1 2 * 1 4 * 8 * 1 16 * 32 * 64 * 1 128 * 1 0.0625 0.25 . 1 2 8 64 128
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
14/52
(a n-1 a n-2 … a 0 . a -1 … a -m ) B = (N)10 N = (a n-1 * B n-1) + (a n-2 * B n-2) + … + (a 0 * B 0) + (a -1 * B -1) + … + (a -m * B –m)
16 -1 16 -2
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
15/52
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
16/52
2 25 2 12 24 2 6 12 1 2 3 6 2 1 2 1 1
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
17/52
494 8 61 488 8 7 56 6 5 7
16 946 16 59 944 16 3 48 2 11 3
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
18/52
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
19/52
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
20/52
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
21/52
a3 a2 a1 a0
.
a-1 a-2
Octal 1 2 3 4 5 6 7 Binary 000 001 010 011 100 101 110 111
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
22/52
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
23/52
a3 a2 a1 a0
.
a-1 a-2
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
24/52
1111 0010 0101 . 0000 0011
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
25/52
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
26/52
1 1 1 1 carried digits (13)10 1 1 0 1 (23)10 1 0 1 1 1 (36)10 1 0 0 1 0 1 + 1 → 1 0 1 + 1 + 1 → 1 1 1 1 1 1 1 1 0 1 1 (1 + 1 ) 10 → (2) 10 = (10) 2
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
27/52
4 5 6 7 8 4 B D A 1 1 7 8 4 B D A 1 B 5 E (8 + D ) 16 → (8 + 13) 10 = (21) 10 = (15) 16 1 1 1 1 1 7 7 7 1 4 7 6 1 0 1 2 (4 + 6 ) 10 → (10) 10 = (12) 8
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
28/52
2 0 2 Borrowed digits (13)10 1 1 0 1 (7)10 1 1 1 (6)10 0 1 1 (2) 10 = (10) 2 1 1 1 1 1 1 Borrowed digit
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
29/52
16 + 0 = 16 16 + 3 = 19 2 3 5 + 16 =21 3 1 4 5 1 9 7 6 1 7 C F 3 6+ 8 = 14 4 6 7 3 7
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
30/52
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
31/52
– Positive: s = 0 – Negative: s= 1 – Range = {(-127)10 .. (+127) 10} – Two ways to represent zero:
– Examples:
N bits?
– Positive: 2 N-1 - 1 – Negative: 2 N-1 - 1
1 2 3 4 5 6 7
s Magnitude Sign
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
32/52
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
33/52
+ 3 is: 00000011
11111100
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
34/52
We note that 19 in binary is
00010011,
so -19 in one’s complement is:
11101100.
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
35/52
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
36/52
1. Invert all the bits through the number
2. Add one
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 Negative two's complement (11111101)
~(11111101) + 1
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
37/52
We note that 19 in binary is:
00010011,
so -19 using one’s complement is:
11101100,
and -19 using two’s complement is: 11101101.
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
38/52
38
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
39/52
39
But overflow into the sign bit does not always mean that we have an error.
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
40/52
40
Rule for detecting signed two’s complement overflow: When the “carry in” and the “carry out” of the sign bit differ,
carry out of the sign bit, no overflow has occurred.
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
41/52
41
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
42/52
42
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
43/52
43
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
44/52
44
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
45/52
45
Note: Although “significand” and “mantissa” do not technically mean the same thing, many people use these terms interchangeably. We use the term “significand” to refer to the fractional part of a floating point number.
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
46/52
46
– A floating-point number is 14 bits in length – The exponent field is 5 bits – The significand field is 8 bits
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
47/52
47
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
48/52
48
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
49/52
49
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
50/52
50
– 3.75 = -11.112 = -1.111 x 21 – The bias is 127, so we add 127 + 1 = 128 (this is our exponent) – The first 1 in the significand is implied, so we have: – Since we have an implied 1 in the significand, this equates to
(implied)
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
51/52
ASCII (“=”) = (3D) 16
F E D C B A 9 8 7 6 5 4 3 2 1 Hexadecimal SI SO CR FF VT LF TAB BS BEL ACK ENQ EOT ETX STX SOH NUL US RS GS FS ESC SUB EM CAN ETB SYN NAK DC4 DC3 DC2 DC1 DLE 1 / .
+ * ( ) ' & % $ # " ! 2 ? < = > ; : 9 8 7 6 5 4 3 2 1 3 O N M L K J I H G F E D C B A @ 4 _ ^ [ \ ] Z Y X W V U T S R Q P 5
m l k j i h g f e d c b a ` 6 ~ { | } z y x w v u t s r q p 7
row number column number
Number Systems – Lecture 3
Sharif University of Technology
Department of Computer Engineering
52/52
– Decimal, Binary, Octal, Hexadecimal
– Bit, Byte, Kilo byte, Giga byte,
– Convert between different bases
– Addition and subtraction
– Sing-magnitude: one sign bit + magnitude bits – Two’s complement : (-N) = ~(N) + 1