Lecture Slides Week3
Lecture Slides Week3
Language
(CS2523)
Week 3
2
Outline Week 3
• Binary Numbers
• Decimal Numbers
• Hexadecimal Numbers
• Base Conversions
• Integer Storage Sizes
• Binary and Hexadecimal Addition
• Signed Integers and 2's Complement Notation
• Binary and Hexadecimal subtraction
• Carry and Overflow
• Character Storage
3
Number Bases
• Binary: Base 2
(0, 1)
• Octal: Base 8
(0, 1, 2, 3, 4, 5, 6, 7)
• Decimal: Base 10
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
• Hexadecimal: Base 16
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F)
4
Binary Numbers
5
Placeholders
3125
So this number represents
• 3 thousands
• 1 hundred Mathematically, this is
• 2 tens
• 5 ones (3 x 1000) + (1 x 100) + (2 x 10) + (5 x 1)
= 3000 + 100 + 20 + 5 = 3125
6
More on Placeholders
312510
To avoid ambiguity, we often write the base of a number as a subscript. 7
Binary Numbers - Example
8’s place 4’s place 2’s place 1’s place
10102
This subscript denotes that this number is in Base 2 or “Binary”. 8
Binary Numbers - Example
8’s place 4’s place 2’s place 1’s place
10102
So this number represents
• 1 eight
• 0 fours Mathematically, this is
• 1 two
• 0 ones (1 x 8) + (0 x 4) + (1 x 2) + (0 x 1)
= 8 + 0 + 2 + 0 = 1010
9
Which Digits Are Available in which Bases
Base 10 Base 2 Base 16
2 digits
0 0 0
1 1 1
Add Placeholder Base 16
2 10 2 Cheat Sheet
3 3 A16 = 1010
10 digits
4 4 B16 = 1110
5 5
C16 = 1210
6 6
16 digits
D16 = 1310
7 7
8 8 E16 = 1410
9 9 F16 = 1510
10 Add Placeholder A
B
C
D
E
Note: Base 16 is also called “Hexadecimal” or “Hex”. F
10 Add Placeholder
10
Hexadecimal Numbers - Example
256’s place 16’s place 1’s place
3AB16
Note:
162 = 256
3AB16
C16 = 1210
D16 = 1310
E16 = 1410
F16 = 1510
So this number represents
• 3 two-hundred fifty-sixes
• 10 sixteens
• 11 ones
Mathematically, this is
12
Why Hexadecimal Is Important
What is the largest What is the largest Base 16
number you can number you can Cheat Sheet
represent using four represent using a A16 = 1010
binary digits? single hexadecimal B16 = 1110
_ _ _ _ digit? C16 = 1210
_
1 1 1 1
2 D16 = 1310
2 3
2 2
2 1
2 0 F = 1510 E16 = 1410
16
=
=
F16 = 1510
8 4 2 1 … the smallest
number?
_
8 + 4 + 2 + 1 = 1510
0 = 010 Note: You can represent the
… the smallest 16
same range of values with a
_ _ _ _
number?
0 0 0 0
2
single hexadecimal digit that
you can represent using four
2 3
2 2
2 1
2 0
binary digits!
0 + 0 + 0 + 0 = 010
13
Why Hexadecimal Is Important
It can take a lot of Hexadecimal
digits to represent numbers can be
numbers in binary. used to abbreviate
binary numbers.
Example:
5179410 = 11001010010100102
Starting at the least
Long strings of digits significant digit, split
can be difficult to your binary number
work with or look at. into groups of four
digits.
14
Windows
“Blue Screen of Death”
MSB LSB
1011001010011100
15 0
16
Binary Numbers
1 1 1 1 1 1 1 1
• Each digit (bit) is either 1 or 0 27 26 25 24 23 22 21 20
• Each bit represents a power of 2:
Every binary
number is a
sum of powers
of 2
17
Converting Decimal to Binary
Example:
We want to convert 12510 to binary.
125 / 2 = 62 R 1
62 / 2 = 31 R 0
31 / 2 = 15 R 1
15 / 2 = 7 R 1
7 / 2 = 3 R 1
3 / 2 = 1 R 1
1 / 2 = 0 R 1
12510 = 11111012
18
Convert Decimal to Binary
• Repeatedly divide the decimal integer by 2. Each remainder is a binary
digit in the translated value:
stop when
37 = 100101 quotient is zero
19
Converting Binary to Decimal
Weighted positional notation shows how to calculate the decimal value
of each binary bit:
Decimal = (dn-1 2n-1) + (dn-2 2n-2) + ... + (d1 21) + (d0 20)
d = binary digit
20
Hexadecimal Integers
Binary values are represented in hexadecimal.
21
Converting Binary Numbers to Hex
Recall the example binary number from the previous slide:
11001010010100102
16
22
Converting Binary to Hexadecimal
• Each hexadecimal digit corresponds to 4 binary bits.
• Example: Translate the binary integer
000101101010011110010100 to hexadecimal:
23
Converting Hexadecimal to Binary
How to convert from hexadecimal number to binary
number.
How to convert base 16 to base 2.
24
Converting Hexadecimal to Binary
Hex Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
25
Converting Hexadecimal to Binary
Example #1
Convert (4E)16 to binary:
(4)16 = (0100)2
(E)16 = (1110)2
So
(4E)16 = (01001110)2
Example #2
Convert (4A01)16 to binary:
(4)16 = (0100)2
(A)16 = (1010)2
(0)16 = (0000)2
(1)16 = (0001)2
So
(4A01)16 = (0100101000000001)2
26
Integer Storage Sizes
byte 8
word 16
Standard sizes: doubleword 32
quadword 64
27
Binary Addition
• Start with the least significant bit (rightmost bit)
• Add each pair of bits
• Include the carry in the addition, if present
carry: 1
0 0 0 0 0 1 0 0 (4)
+ 0 0 0 0 0 1 1 1 (7)
0 0 0 0 1 0 1 1 (11)
bit position: 7 6 5 4 3 2 1 0
28
Binary Addition-Tasks
• 10001 + 11101
• Solution:
• 1110 + 1111
• Solution:
29
Binary Addition-Tasks
• 101101 + 11001
• Solution:
• 10111 + 110101
• Solution:
30
Binary Addition-Tasks
• 1011001 + 111010
• Solution:
• 11011 + 1001010
• Solution:
31
Hexadecimal Addition
• Divide the sum of two digits by the number base (16). The quotient becomes the carry
value, and the remainder is the sum digit.
1 1
36 28 28 6A
42 45 58 4B
78 6D 80 B5
21 / 16 = 1, remainder 5
32
Hexadecimal Addition-Tasks
• 182AB + 5FCAA
33
34
Representation of signed numbers in
binary
• Several ways to represent a signed number
• Sign-Magnitude
• Biased
• 1's complement
• 2's complement
• Divide the range of values into 2 equal parts
• First part corresponds to the positive numbers (≥ 0)
• Second part correspond to the negative numbers (< 0)
• Focus will be on the 2's complement representation
• Has many advantages over other representations
• Used widely in processors to represent signed integers
35
Signed Magnitude Representation
• In signed magnitude
• the left-most bit represents the sign of the integer.
• 0 for positive numbers.
• 1 for negative numbers.
• The remaining bits represent magnitude of the
numbers.
36
Example
position 7 6 5 4 3 2 1 0
Bit pattern 1 0 0 1 1 1 0 1
contribution 24 23 22 20
-
37
Exercise
38
Problems with Signed Magnitude
• Addition, subtraction does not work the way we know
•5–2
• (0101) – (0010) 5-2
• (0101) + (1010) 5+(-2)
• =1111 (-7)
• They are two representations of 0
• 00000000 = + 010
• 10000000 = - 010
39
Two’s Complement Representation
40
Forming the Two's Complement
• Positive Numbers
• Like we normally calculate
• Negative Numbers
• Assign a negative weight to the most-significant bit
43
Addition and Subtraction (it works)
44
Carry C
• Overflow happens when there is a carry from bit 6 into bit 7, and
there is no external carry. No Overflow
Overflow
• It also happens when there is no carry from bit 6 into bit 7, but there
is an external carry.
• This indicates that bit 7 (the sign of the result) has been accidentally
changed
46
Overflow Flag
47
Examples
48
Examples Contd..
49
Examples Contd..
50
That is all for Week 3
51