Download Number Systems - Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Addition wikipedia , lookup

Big O notation wikipedia , lookup

Location arithmetic wikipedia , lookup

Arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
3. Representing Integer Data
Chapt. 4
ITEC 1011
Introduction to Information Technologies
Ranges for Data Formats
No. of bits
Binary
1
0–1
2
0–3
3
0–7
4
0 – 15
5
0 – 31
6
0 – 63
7
0 – 127
8
0 – 255
9
0 – 511
16
24
BCD
0–9
0 – 99
0–9
0 - 65,535
0 – 9999
0 – 99
0 – 16,777,215
0 – 999999
0 – 999
Etc.
ITEC 1011
ASCII
Introduction to Information Technologies
In General (binary)
Binary
No. of bits
n
ITEC 1011
Min
Max
0
2n - 1
Introduction to Information Technologies
Signed Integers
• Previous examples were for “unsigned
integers” (positive values only!)
• Must also have a mechanism to represent
“signed integers” (positive and negative
values!)
• E.g., -510 = ?2
• Two common schemes: sign-magnitude and
twos complement
ITEC 1011
Introduction to Information Technologies
Sign-Magnitude
• Extra bit on left to represent sign
• 0 = positive value
• 1 = negative value
• E.g., 6-bit sign-magnitude representation of
+5 and –5:
+5:
+ve
ITEC 1011
0 0 0 1 0 1
5
-5:
1 0 0 1 0 1
-ve
Introduction to Information Technologies
5
Ranges (revisited)
No. of bits
1
2
3
4
5
6
Etc.
ITEC 1011
Binary
Unsigned
Sign-magnitude
Min
Max
Min
Max
0
1
0
3
-1
1
0
7
-3
3
0
15
-7
7
0
31
-15
15
0
63
-31
31
Introduction to Information Technologies
In General (revisited)
Binary
No. of bits
n
ITEC 1011
Unsigned
Min
0
Max
n
Sign-magnitude
Min
Max
n-1
n-1
2 - 1 -(2
- 1) 2
Introduction to Information Technologies
-1
Difficulties with Sign-Magnitude
• Two representations of zero
• Using 6-bit sign-magnitude…
• 0: 000000
• 0: 100000
• Arithmetic is awkward!
pp. 95-96
ITEC 1011
Introduction to Information Technologies
Complementary Representations
•
•
•
•
9’s complement
10’s complement
1’s complement
Read sections 4.4 and 4.5 (pp. 102-113)
ITEC 1011
Introduction to Information Technologies
Exercises – Complementary Notations
• What is the 3-digit 10’s complement of 247?
• Answer:
• What is the 3-digit 10’s complement of 17?
• Answer:
• 777 is a 10’s complement representation of what
decimal value?
• Answer:
Skip answer
ITEC 1011
Introduction to Information Technologies
Answer
Exercises – Complementary Notations
Answer
• What is the 3-digit 10’s complement of 247?
• Answer: 753
• What is the 3-digit 10’s complement of 17?
• Answer: 983
• 777 is a 10’s complement representation of what
decimal value?
• Answer: 223
See p. 110-111
ITEC 1011
Introduction to Information Technologies
Twos Complement
•
•
•
Most common scheme of representing negative
numbers in computers
Affords natural arithmetic (no special rules!)
To represent a negative number in 2’s
complement notation…
1.
2.
3.
4.
ITEC 1011
Decide upon the number of bits (n)
Find the binary representation of the +ve value in n-bits
Flip all the bits (change 1’s to 0’s and vice versa)
Add 1
Introduction to Information Technologies
Twos Complement Example
•
Represent -5 in binary using 2’s
complement notation
1. Decide on the number of bits
6 (for example)
2. Find the binary representation of the +ve value in
6 bits
000101
+5
3. Flip all the bits
111010
4. Add 1
ITEC 1011
111010
+
1
111011
Introduction to Information Technologies
-5
Sign Bit
• In 2’s complement notation, the MSB is the
sign bit (as with sign-magnitude notation)
• 0 = positive value
• 1 = negative value
+5:
0 0 0 1 0 1
+ve
ITEC 1011
5
-5:
1 1 1 0 1 1
-ve
? (previous slide)
Introduction to Information Technologies
“Complementary” Notation
• Conversions between positive and negative
numbers are easy
• For binary (base 2)…
2’s C
+ve
-ve
2’s C
ITEC 1011
Introduction to Information Technologies
Example
ITEC 1011
+5
0 0 0 1 0 1
2’s C
1 1 1 0 1 0
+
1
-5
1 1 1 0 1 1
2’s C
0 0 0 1 0 0
+
1
+5
0 0 0 1 0 1
Introduction to Information Technologies
Exercise – 2’s C conversions
• What is -20 expressed as an 8-bit binary
number in 2’s complement notation?
• Answer:
• 1100011 is a 7-bit binary number in 2’s
complement notation. What is the decimal
value?
• Answer:
Skip answer
ITEC 1011
Introduction to Information Technologies
Answer
Exercise – 2’s C conversions
Answer
• What is -20 expressed as an 8-bit binary
number in 2’s complement notation?
• Answer: 1101100
• 1100011 is a 7-bit binary number in 2’s
complement notation. What is the decimal
value?
• Answer:
ITEC 1011
-29
Introduction to Information Technologies
Range for 2’s Complement
• For example, 6-bit 2’s complement notation
100000
100001
-32
-31
111111
...
Negative, sign bit = 1
ITEC 1011
-1
000000
0
000001
1
011111
...
31
Zero or positive, sign bit = 0
Introduction to Information Technologies
Ranges (revisited)
Binary
No. of
bits
1
2
3
4
5
6
Unsigned
Min
Max
0
1
0
3
0
7
0
15
0
31
0
63
Sign-magnitude 2’s complement
Min
Max
Min
Max
-1
1
-2
1
-3
-7
-15
-31
3
7
15
31
-4
-8
-16
-32
3
7
15
31
Etc.
ITEC 1011
Introduction to Information Technologies
In General (revisited)
Binary
No. of Unsigned
bits
Min Max
n
ITEC 1011
0
n
Sign-magnitude 2’s complement
Min
2 - 1 -(2
n-1
Max Min
n-1
- 1) 2 -1 -2
n-1
Introduction to Information Technologies
Max
2
n-1
-1
2’s Complement Addition
• Easy
• No special rules
• Just add
ITEC 1011
Introduction to Information Technologies
What is -5 plus +5?
• Zero, of course, but let’s see
Sign-magnitude
-5:
+5:
ITEC 1011
10000101
+00000101
10001010
Twos-complement
-5:
+5:
11 1 1 1 1 11
11111011
+00000101
00000000
Introduction to Information Technologies
2’s Complement Subtraction
• Easy
• No special rules
• Just subtract, well … actually … just add!
A – B = A + (-B)
add
ITEC 1011
2’s complement of B
Introduction to Information Technologies
What is 10 subtract 3?
• 7, of course, but…
• Let’s do it (we’ll use 6-bit values)
10 – 3 = 10 + (-3) = 7
+3: 000011
1s C: 111100
+1:
1
-3: 111101
ITEC 1011
001010
+111101
000111
Introduction to Information Technologies
What is 10 subtract -3?
(-(-3)) = 3
• 13, of course, but…
• Let’s do it (we’ll use 6-bit values)
10 – (-3) = 10 + (-(-3)) = 13
-3: 111101
1s C: 000010
+1:
1
+3: 000011
ITEC 1011
001010
+000011
001101
Introduction to Information Technologies
Thank You
ITEC 1011
Introduction to Information Technologies