R E P R E S E N T IN G IN T E G E R D A T A
Englander Ch. 4 ITEC 1011Introduction to Information TechnologiesB as ic D ef in it io n
•An integer is a number which has no fractional part. Examples:-2022 -213 0 1 514 323434565232U n si g n ed B in ar y a n d B in ar y -C o d ed D ec im al B C D : B in ar y -C o d ed D ec im al
•Each decimal digit individually converted to binary –Requires 4 bits per digit ⇒8-bit location hold 2 BCD digits —00 to 99 6810≡0110 1000BCD •Hexa: 4 bits can hold 16≠values, 0 to F •A to F not used in BCDITEC 1011Introduction to Information Technologies
R an g es f o r D at a F o rm at s
Etc.0 –9990 –9999990 –16,777,21524
0 –990 –99990 -65,53516
0 –5119
0 –90 –990 –2558
0 –1277
0 –636
0 –315
0 –90 –154
0 –73
0 –32
0 –11
ASCIIBCDBinaryNo. of bits ITEC 1011Introduction to Information Technologies
In G en er al ( b in ar y )
2n -1MaxMin 0n
Binary No. of bits Remember !! ITEC 1011Introduction to Information Technologies
S ig n ed I n te g er s
•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: 1) sign-magnitude 2) two’s complement ITEC 1011Introduction to Information TechnologiesS ig n -M ag n it u d e
•Extra bit on left to represent sign •0 = positive value •1 = negative value •E.g., 6-bit sign-magnitude representation o +5 and –5: +5:000101 +5-5:100101 -5
ITEC 1011Introduction to Information Technologies
R an g es ( re v is it ed )
Binary Etc.31-316306
15-153105
7-71504
3-3703
1-1302
101
MaxMaxMinMin
Sign-magnitudeUnsigned No. of bits ITEC 1011Introduction to Information Technologies
In G en er al ( re v is it ed )
Binary 2n-1 -1-(2n-1 -1)2n -10nMaxMaxMinMin
Sign-magnitudeUnsigned No. of bits ITEC 1011Introduction to Information Technologies
D if fi cu lt ie s w it h S ig n -M ag n it u d e
•Two representations of zero •0:000000 •0:100000 •Arithmetic is awkward!O n e’ s co m p le m en t
•Principle: Invert bits (01 and 10) •6:000110 •-6:111001 •RangeA d d / S u b i n 1 ’s c o m p le m en t O v er fl o w
Overflow sign of result≠sign both operands ITEC 1011Introduction to Information TechnologiesT w o ’s C o m p le m en t
•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.Decide upon the number of bits (n) 2.Find the binary representation of the positive value inn-bit 3.Flip all the bits (change 1’s to 0’s and vice versa) 4.Add 1 Learn!T w o ’s c o m p le m en t re p re se n ta ti o n
ITEC 1011Introduction to Information TechnologiesT w o ’s C o m p le m en t E x am p le
•Represent –5 in binary using 2’s complement notation 1.Decide on the number of bits, for example: 6 2.Find the binary representation of the positive (5) value in 6 bits 3.Flip all the bits 4.Add 1111010 111010 +1 111011-5
000101 +5 ITEC 1011Introduction to Information Technologies
S ig n B it
•In 2’s complement notation, the MSB is the sign bit (as with sign-magnitude notation) •0 = positive value •1 = negative value -5:111011 -+5:000101 +5? (previous slide) ITEC 1011Introduction to Information Technologies
“C o m p le m en ta ry ” N o ta ti o n
•Conversions between positive and negative numbers are easy •For binary (base 2)… +-2’s C 2’s C
ITEC 1011Introduction to Information Technologies
E x am p le
+5 2’s C -5 2’s C +5000101 111010 +1 111011 000100 +1 000101 ITEC 1011Introduction to Information Technologies
D et ai l fo r -2 0 1 1 1 0 1 1 0 0
-2010:PositiveValue = 00010100 “Flip”: 11101011 Add 1:+ 1 11101100(One’s complement) ITEC 1011Introduction to Information Technologies
D et ai l fo r 1 1 0 0 0 1 1 - 2 9
2’s Complement:1100011 “Flip”:1011100 Add One:+1 1011101 Converts to:= -29 kc(One’s complement) ITEC 1011Introduction to Information Technologies
R an g e fo r 2 ’s C o m p le m en t
•For example, 6-bit 2’s complement notatio -32-31...-101...31000000111111000001011111100000100001 Negative, sign bit = 1Zero or positive, sign bit =
ITEC 1011Introduction to Information Technologies
R an g es ( re v is it ed )
6331
15
7
3
1Max 000000
Min
Unsigned
Binary Etc.
31-3231-316
15-1615-155
7-87-74
3-43-33
1-21-12
1
MaxMaxMinMin
2’s complementSign-magnitudeNo. of bits ITEC 1011Introduction to Information Technologies
In G en er al ( re v is it ed )
2n -1Max 0
Min
Unsigned
Binary 2n-1 -1-2n-1 2n-1 -1-(2n-1 -1)n
MaxMaxMinMin
2’s complemenSign-magnitudeNo. of bits To remember ITEC 1011Introduction to Information Technologies
2 ’s C o m p le m en t A d d it io n
•Easy •No special rules •Just add ITEC 1011Introduction to Information TechnologiesW h at i s -5 p lu s + 5 ?
•Zero, of course, but let’s see -5:10000101 +5:+00000101 10001010Sign-magnitude -5:11111011 +5:+00000101 00000000 Two’s-complement 11111111
ITEC 1011Introduction to Information Technologies
2 ’s C o m p le m en t S u b tr ac ti o n
•Easy •No special rules •Just subtract, well …actually …just add! A–B=A+(-B) add2’s complement of B ITEC 1011Introduction to Information TechnologiesW h at i s 1 0 s u b tr ac t 3 ?
•7, of course, but… •Let’s do it (we’ll use 6-bit values) 10–3=10+(-3)=7 001010 +111101 000111+3:000011 1sC:111100 +1:1 -3:111101 ITEC 1011Introduction to Information Technologies
W h at i s 1 0 s u b tr ac t -3 ?
•13, of course, but… •Let’s do it (we’ll use 6-bit values) 10–(-3)=10+(-(-3))=13 001010 +000011 001101-3:111101 1sC:000010 +1:1 +3:000011
(-(-3))=3