Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Negative Integer Representation
Dale Roberts Negative Numbers in Binary Four different representation schemes are used for negative numbers 1. Signed Magnitude Left most bit (LMB) is the sign bit : 0 positive (+) 1 negative (-) Remaining bits hold absolute magnitude Example: 2 10 b b Q: = ? = ? Try, b =-4 10
Dale Roberts 1’s Complement 2.One’s Complement – –Left most bit is the sign bit : 0 positive (+) 1 negative (-) – –The magnitude is Complemented Example: 2 10 b b Exercise: try using 1’s Complement Q: = ? = ? = ? Solution: 4 10 = b = b 1
Dale Roberts 2’s Complement 3.2’s Complement Sign bit same as above Magnitude is Complemented first and a “1” is added to the Complemented digits Example: 2 10 b 1’s Complement b b 7 10 1’s Complement Exercise: try using 2’s Complement b b b
Dale Roberts 2’s Complement 7 10 = b 3 10 = b 1’s complement b 2’s complement b (-3) Example: 7+(-3) [hint]: A – B = A + (~B) carry ignore 4 10
Dale Roberts Three Representation of Signed Integer
Dale Roberts Negative Numbers in Binary (cont.) 4. Excess Representation – – For a given fixed number of bits the range is remapped such that roughly half the numbers are negative and half are positive. Example: (as left) Excess – 8 notation for 4 bit numbers Binary value = 8 + excess-8 value MSB can be used as a sign bit, but If MSB =1, positive number If MSB =0, negative number Excess Representation is also called bias Numbers Binary Value Notation Excess – 8 Value
Dale Roberts Fundamental Data Types 2 byte unsigned (Default type is int ) 2 byte int ( 0 D ) ( 1 D ) ( 2 D ) … ( D ) ( D 2 15 ) … ( 2 16 –1) ( D ) ( D ) … ( - 2 D ) ( - 1 D ) ( 0 D ) ( 1 D ) ( 2 D ) … ( D ) With vs. without using sign bit With vs. without using sign bit For a 16 bit binary pattern:
Dale Roberts Fundamental Data Types Four Data Types in C Four Data Types in C (assume 2’s complement, byte machine) Data Type Abbreviation Size (byte) Range char ~ 127 unsigned char10 ~ 255 int 2 or ~ or ~ unsigned int unsigned 2 or 40 ~ or 0 ~ short int short ~ unsigned short int unsigned short 20 ~ long int long ~ unsigned long int unsigned long 40 ~ float4 double8 Note:2 7 = 128, 2 15 =32768, 2 15 = Complex and double complex are not available
Dale Roberts Acknowledgements These slides where originally prepared by Dr. Jeffrey Huang, updated by Dale Roberts.