CSC255 Lecture 4 Negative Numbers Images Parity Brookshear
CSC-255 Lecture 4 Negative Numbers, Images, Parity (Brookshear Chapter 1, pages 46 -72) Modified by Ufuk Verun from Jim Janossy © 2002, De. Paul University CTI – Chicago and Brookshear © 2003 Pearson Education, Inc. CSC 255 -702/703 CTI/De. Paul 1
Resources This Power. Point presentation is available for download at www. depaul. edu/~uverun/classes/CSC 255/fal l 2002/slides/Lecture_04. ppt Textbook: Chapter 1 Print slides at 6 slides/page, avoid waste! Exams are based on slide content, homework and assigned readings CSC 255 -702/703 CTI/De. Paul 2
8 -Bit Binary Numbers So far we used no sign; i. e. , all positive 1111 Highest 8 -bit pure 255 binary value 0000 0 Lowest 8 -bit pure binary value CSC 255 -702/703 CTI/De. Paul 3
How To Store Negative Numbers? We can “trade” some of the number range capacity of binary numbers to represent negative integers For example, 8 bits can store -128 to +127 rather than 0 to 255 Two’s complement and excess notation are two ways to do this CSC 255 -702/703 CTI/De. Paul 4
Two’s Complement System Takes half of the bit patterns and uses them for negative numbers Provides computational advantage: addition and subtraction are the same operation! Most commonly used today with 32 -bit binary numbers CSC 255 -702/703 CTI/De. Paul 5
Two’s Complement CSC 255 -702/703 CTI/De. Paul 6
Two’s Complement Example Using only 3 bits to simplify things… Pure binary Two’s complement 111 110 101 100 011 010 001 000 7 6 5 4 3 2 1 0 -1 -2 -3 -4 3 2 1 0 CSC 255 -702/703 CTI/De. Paul 011 010 001 000 111 110 101 100 3 2 1 0 -1 -2 -3 -4 7
Two’s Complement Signs Leftmost bit indicates sign 0: +, 1: Pure binary Two’s complement 111 110 101 100 011 010 001 000 7 6 5 4 3 2 1 0 -1 -2 -3 -4 3 2 1 0 CSC 255 -702/703 CTI/De. Paul 011 010 001 000 111 110 101 100 3 2 1 0 -1 -2 -3 -4 8
The Two’s Complement “Forming” Rule Take a positive binary value Copy from right to left unchanged until you pass the first “ 1” encountered After the first “ 1”, continue copying right to left but write the “complement” (0 for 1, 1 for 0) CSC 255 -702/703 CTI/De. Paul 9
Example: Finding Two’s Complement CSC 255 -702/703 CTI/De. Paul 10
Example: Forming Two’s Complement for -26 What is two’s complement code for value -26 using 8 bits? +26: 0 0 0 1 1 0 Complement remaining bits -26: 1 1 1 0 0 1 1 0 CSC 255 -702/703 CTI/De. Paul 11
Exercise: Two’s Complement for -117 What is two’s complement code for value -117 using 8 bits? +117: ? ? ? ? -117: ? ? ? ? CSC 255 -702/703 CTI/De. Paul 12
Exercise: Two’s Complement for -210 What is two’s complement code for -210 using 8 bits? (hint: trick question) +210: ? ? ? ? -210: ? ? ? ? CSC 255 -702/703 CTI/De. Paul 13
“Decoding” Rule for Two’s Complement Values If the sign bit is 0, the number is positive. You can read it as if it were pure binary. Example: 0 0 0 1 0 1 = decimal 21 CSC 255 -702/703 CTI/De. Paul 14
The “Decoding” Rule for Two’s Complement Values If the sign bit 1, the number is negative. You must: 1. Copy bits right to left unchanged until you pass through the first “ 1” 2. Continue copying right to left but write the “complement” (0 for 1, 1 for 0) 3. Interpret the resulting binary value and put “-” in front of it CSC 255 -702/703 CTI/De. Paul 15
Example: Decoding a Two’s Complement Value What is the decimal value of the following two’s complement binary number? 00001100 Leading digit is 0, so it’s positive. Convert it from binary to decimal: Value is 12 Make sure you know why! CSC 255 -702/703 CTI/De. Paul 16
Example: Decoding Two’s Complement Value In two’s complement system, what is the decimal value of 11111010 ? n n Leading digit is 1, so it’s negative. You must decode it, then convert it from binary to decimal. . . CSC 255 -702/703 CTI/De. Paul 17
Example: Decoding a Two’s Complement Value Decode it: 11111010 Complement remaining bits 00000110 This is 6 -6 CSC 255 -702/703 CTI/De. Paul 18
Exercise: Decoding a Two’s Complement Value What is the decimal value of 10010101 in two’s complement system? CSC 255 -702/703 CTI/De. Paul 19
Exercise: Ranges for Two’s Complement Numbers What is the maximum and minimum numbers that can be represented using 16 -bit two’s complement system? CSC 255 -702/703 CTI/De. Paul 20
Exercise: Ranges for Two’s Complement Numbers What is the maximum and minimum numbers that can be represented in 16 -bit two’s complement system? Answer: Two’s complement divides the 16 -bit range into two: n -215 to -1 Minimum: 100000000 = -32768 n 0 to 215 – 1 Maximum: 011111111= +32767 CSC 255 -702/703 CTI/De. Paul 21
Common Two’s Complement Mistakes This value is -6 in two’s complement: 11111010 It is incorrect to write -6 as -00000110 It is incorrect to write -6 as 10000110 The fact that 11111010 is negative is contained entirely in the leading “ 1” and two’s complement notation CSC 255 -702/703 CTI/De. Paul 22
Addition in Two’s Complement Notation CSC 255 -702/703 CTI/De. Paul 23
Overflow problem Overflow happens when a resultant value of a mathematical operation falls outside of the range that can be represented with a given number of bits Example: In pure binary system, 254 + 6 produces 260, but 8 bits cannot hold 260 Small values can accumulate to produce large values and cause overflow! CSC 255 -702/703 CTI/De. Paul 24
Overflow in Pure Binary Add 134 and 130: 1 1 1 + 134: + 130: 10000110 10000010 0 0 1 0 0 0 = +8 ? ? ? Overflow! This bit is lost - The answer is wrong! CSC 255 -702/703 CTI/De. Paul 25
Subtraction in Two’s Complement. . . Computing 6 - 2 is same as 6 + (-2): 1 1 1 1 + 6: - 2: 00000110 11111110 0 0 1 0 0 = +4 Ignore overflow. Addition of positive and negative numbers will set the sign bit correctly. In this case, 0: positive. CSC 255 -702/703 CTI/De. Paul 26
Subtraction in Two’s Complement. . . Computing -112 - 112: 1 1 -112: 100100000 Cannot ignore overflow. Addition of two negative numbers should set the sign bit to 1. In this case, 0: positive, which is not correct. CSC 255 -702/703 CTI/De. Paul 27
Addition in Two’s Complement. . . Computing 112 + 112: 1 1 1 +112: 011100000 Addition of two positive numbers should set the sign bit to 0. In this case, 1: negative, which is not correct. CSC 255 -702/703 CTI/De. Paul 28
Exercise: Addition in Two’s Complement. . . Compute 68 – 120 in 8 -bit two’s complement representation +68: -120: n ? ? ? ? ? ? Is your answer correct, or did it result in overflow? How do you know? Explain. CSC 255 -702/703 CTI/De. Paul 29
Arithmetic Operations in Two’s Complement System Subtraction is handled by addition Multiplication can be done by repeated addition Division can be done by repeated subtraction So the same circuitry used to perform two’s complement addition can handle all four arithmetic operations! CSC 255 -702/703 CTI/De. Paul 30
Excess Notation A different way to reassign half the range of bit patterns to hold negative numbers First pattern with high order “ 1” represents value 0, next highest represents value 1, and so on Values lower represent -1, -2, -3, . . . CSC 255 -702/703 CTI/De. Paul 31
Example: 4 -bit Excess Notation CSC 255 -702/703 CTI/De. Paul 32
Excess Notation Example Using only 3 bits to simplify things… Pure binary 111 110 101 100 011 010 001 000 7 6 5 4 3 2 1 0 Excess notation 111 110 101 100 011 010 001 000 3 2 1 0 -1 -2 -3 -4 CSC 255 -702/703 CTI/De. Paul 33
Excess Notation… Named “excess” because pattern in binary is always a fixed number greater than the interpretation of the same pattern in excess notation Excess does not offer the computational advantage that two’s complement does Excess is used in floating point notation CSC 255 -702/703 CTI/De. Paul 34
Floating Point Notation Binary and two’s complement are good for integers (whole numbers) only: (1, 2, 17, 3753647) Floating point is used for numbers with fractional parts (3. 1416, 16. 53798) Think of floating point associated with scientific notation, like 6. 28462 x 107 CSC 255 -702/703 CTI/De. Paul 35
Example: Floating Point Notation Bits to be used for number storage are divided into three groups: 0 1 1 sign bit mantissa exponent CSC 255 -702/703 CTI/De. Paul 36
Example: Floating Point… 0 1 1 Sign bit: 0 = positive, 1 = negative Mantissa: pure binary value with assumed radix point at left; eg. . 1011 Exponent: excess notation, 110 = 2, how many places right (+) or left (-) to move the radix: 10. 11 = 2 + 1/4 CSC 255 -702/703 CTI/De. Paul 37
Exercise: Floating Point What is the decimal value of the following 8 -bit floating point binary number? (3 bits exponent, 4 bits mantissa) 00101101 CSC 255 -702/703 CTI/De. Paul 38
Exercise: Floating Point What is the decimal value of the following 16 -bit floating point binary number? (7 bits exponent, 8 bits mantissa) 1101011111011001 CSC 255 -702/703 CTI/De. Paul 39
Imprecision in Number Storage Truncation: not enough bits to store number, so least significant digits lost Nonterminating expansions like 1/3 =. 3333… More nonterminating expansions exist in binary than in decimal Users are generally shielded from these effects by application software CSC 255 -702/703 CTI/De. Paul 40
Example: Floating Point Representation Using 8 Bits What is sign bit, what is exponent? CSC 255 -702/703 CTI/De. Paul 41
Images CSC 255 -702/703 CTI/De. Paul 42
Image as Bitmap (. BMP) Image is collection of dots (pixels) Pixel = “picture element” Black & white: one bit per pixel Color: each pixel represented by combination of red, green, blue (RGB) in varying intensity, to form all colors. Three bytes per pixel: one byte for each color intensity, 0 -255 value CSC 255 -702/703 CTI/De. Paul 43
. BMP With 3 bytes/pixel, how many bytes are needed for following resolutions? n n n 2400 x 1800 = 4. 3 M x 3 b 1600 x 1200 = 1. 9 M x 3 b 1280 x 960 = 1. 3 M x 3 b 800 x 600 = 480 K x 3 b 640 x 480 = 307 K x 3 b 320 x 240 = 76. 8 K x 3 b CSC 255 -702/703 CTI/De. Paul = 13 Mb = 5. 8 Mb = 3. 9 Mb = 1. 44 Mb = 921 Kb = 230 Kb 44
Problems with Bitmaps Bitmap image cannot be enlarged, since pixels get bigger and image gets grainy or “blocky”. GIF and. JPG formats compress images Raw bitmaps are not efficient, . GIF and. JPG are CSC 255 -702/703 CTI/De. Paul 45
GIF (. GIF) Image Graphics Interchange Format Developed by Compuserve (an ISP) Stores only 256 colors Loses some picture quality but is simple and fast Common in computer action games CSC 255 -702/703 CTI/De. Paul 46
JPEG (. JPG) Image Joint Photographic Experts Group Stores differences between adjacent pixels, not absolute values Uses variable-length data (values take a minimum number of bits to store) On the average, uses only 5% of the space of bitmaps CSC 255 -702/703 CTI/De. Paul 47
. JPG With 3 bytes/pixel, how many bytes are needed for the following resolutions? n n n 2400 x 1800 = 4. 3 M x 3 b = 13 Mb 650 K 1600 x 1200 = 1. 9 M x 3 b = 5. 8 Mb 290 K 1280 x 960 = 1. 3 M x 3 b = 3. 9 Mb 195 K 800 x 600 = 480 K x 3 b = 1. 44 Mb 72 K 640 x 480 = 307 K x 3 b = 921 Kb 46 K 320 x 240 = 76. 8 K x 3 b = 230 Kb 12 K CSC 255 -702/703 CTI/De. Paul 48
Vector Images Pixels are not mapped Equations for the lines and curves making up the image are stored Image is stored as the instructions for drawing the image Images are easily scaled CSC 255 -702/703 CTI/De. Paul 49
Vector Images… Modern type fonts are vector images Used in computer aided design (CAD) systems for “blueprint” drawings Good for three-dimensional drawings Windows metafile (. wmf) or Visio (. vsd) Cannot produce photographic images CSC 255 -702/703 CTI/De. Paul 50
Error Detection and Correction CSC 255 -702/703 CTI/De. Paul 51
Bit Errors Data storage mediums like memory, magnetic disk, CD/ROM can introduce errors (bits can get “flipped”) Noisy communication circuits can do this too Techniques are used to overcome these limitations and make data storage and communication reliable CSC 255 -702/703 CTI/De. Paul 52
Detecting Bit Error Parity bit: an extra bit used to detect error Even Parity: Parity bit is set to make number of 1’s even; n Example: 1 0 0 1 0 1 Odd parity system is identical except that bit is set to make number of 1’s odd Receiving circuitry can detect if one bit has been flipped CSC 255 -702/703 CTI/De. Paul 53
Example: Generating Odd Parity Value CSC 255 -702/703 CTI/De. Paul 54
Memory with Parity Some memory systems use 9 bits for each 8 -bit byte Extra bit is for parity checking Hardware checks parity periodically behind the scenes Your programs don’t “see” the extra bit CSC 255 -702/703 CTI/De. Paul 55
Variations on Parity Checks Checkbytes or checksums - “hash” total computed when data is stored, recomputed when data is read Cyclic redundancy codes (CRC) CSC 255 -702/703 CTI/De. Paul 56
Error Correction Hamming codes (named for inventor, R. W. Hamming) - can be used to correct as well as detect some flawed bits, based on algebraic coding theory CSC 255 -702/703 CTI/De. Paul 57
Example: Hamming Code for Symbols A through H CSC 255 -702/703 CTI/De. Paul 58
Example: 1 -Bit Error Correction for Pattern 010100 Calculate the number of bits different between each code on table and the given pattern n n n n Distance Distance between between A(000000) and 010100 = B(001111) and 010100 = C(010011) and 010100 = D(011100) and 010100 = E(100110) and 010100 = F(101001) and 010100 = G(110101) and 010100 = H(111010) and 010100 = CSC 255 -702/703 CTI/De. Paul 2 4 3 1 3 5 2 4 59
Example: 1 -Bit Error Correction for Pattern 010100 Based on 1 -bit error correction, D is the pattern CSC 255 -702/703 CTI/De. Paul 60
- Slides: 60