Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC255-702/703 CTI/DePaul1 CSC-255 Lecture 4 Negative Numbers, Images, Parity (Brookshear Chapter 1, pages 46-72) Modified by Ufuk Verun from Jim Janossy.

Similar presentations


Presentation on theme: "CSC255-702/703 CTI/DePaul1 CSC-255 Lecture 4 Negative Numbers, Images, Parity (Brookshear Chapter 1, pages 46-72) Modified by Ufuk Verun from Jim Janossy."— Presentation transcript:

1

2 CSC255-702/703 CTI/DePaul1 CSC-255 Lecture 4 Negative Numbers, Images, Parity (Brookshear Chapter 1, pages 46-72) Modified by Ufuk Verun from Jim Janossy © 2002, DePaul University CTI – Chicago and Brookshear © 2003 Pearson Education, Inc.

3 CSC255-702/703 CTI/DePaul2 Resources This PowerPoint presentation is available for download at www.depaul.edu/~uverun/classes/CSC255/fal l2002/slides/Lecture_04.ppt www.depaul.edu/~uverun Textbook: Chapter 1 Print slides at 6 slides/page, avoid waste! Exams are based on slide content, homework and assigned readings

4 CSC255-702/703 CTI/DePaul3 8-Bit Binary Numbers So far we used no sign; i.e., all positive 11111111255 000000000 Highest 8-bit pure binary value Lowest 8-bit pure binary value

5 CSC255-702/703 CTI/DePaul4 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

6 CSC255-702/703 CTI/DePaul5 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

7 CSC255-702/703 CTI/DePaul6 Two’s Complement

8 CSC255-702/703 CTI/DePaul7 Two’s Complement Example Using only 3 bits to simplify things… Pure binary Two’s complement 1 1 1 7 1 1 1 -1 0 1 1 3 1 1 0 6 1 1 0 -2 0 1 0 2 1 0 1 5 1 0 1 -3 0 0 1 1 1 0 0 4 1 0 0 -4 0 0 0 0 0 1 1 3 0 1 1 3 1 1 1 -1 0 1 0 2 0 1 0 2 1 1 0 -2 0 0 1 1 0 0 1 1 1 0 1 -3 0 0 0 0 0 0 0 0 1 0 0 -4

9 CSC255-702/703 CTI/DePaul8 Two’s Complement Signs Leftmost bit indicates sign 0:+, 1:- Pure binary Two’s complement 1 1 1 7 1 1 1 -1 0 1 1 3 1 1 0 6 1 1 0 -2 0 1 0 2 1 0 1 5 1 0 1 -3 0 0 1 1 1 0 0 4 1 0 0 -4 0 0 0 0 0 1 1 3 0 1 1 3 1 1 1 -1 0 1 0 2 0 1 0 2 1 1 0 -2 0 0 1 1 0 0 1 1 1 0 1 -3 0 0 0 0 0 0 0 0 1 0 0 -4

10 CSC255-702/703 CTI/DePaul9 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)

11 CSC255-702/703 CTI/DePaul10 Example: Finding Two’s Complement

12 CSC255-702/703 CTI/DePaul11 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 1 0 -26:1 1 1 0 0 1 1 0 Complement remaining bits

13 CSC255-702/703 CTI/DePaul12 Exercise: Two’s Complement for -117 What is two’s complement code for value -117 using 8 bits? +117:? ? ? ? ? ? ? ? -117:? ? ? ? ? ? ? ?

14 CSC255-702/703 CTI/DePaul13 Exercise: Two’s Complement for -210 What is two’s complement code for -210 using 8 bits? (hint: trick question) +210:? ? ? ? ? ? ? ? -210:? ? ? ? ? ? ? ?

15 CSC255-702/703 CTI/DePaul14 “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 0 1 = decimal 21

16 CSC255-702/703 CTI/DePaul15 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

17 CSC255-702/703 CTI/DePaul16 Example: Decoding a Two’s Complement Value What is the decimal value of the following two’s complement binary number? 0 0 0 0 1 1 0 0 Leading digit is 0, so it’s positive. Convert it from binary to decimal: Value is 12 Make sure you know why!

18 CSC255-702/703 CTI/DePaul17 Example: Decoding Two’s Complement Value In two’s complement system, what is the decimal value of 1 1 1 1 1 0 1 0 ? Leading digit is 1, so it’s negative. You must decode it, then convert it from binary to decimal...

19 CSC255-702/703 CTI/DePaul18 Example: Decoding a Two’s Complement Value Decode it: 1 1 1 1 1 0 1 0 0 0 0 0 0 1 1 0 This is 6 - 6 Complement remaining bits

20 CSC255-702/703 CTI/DePaul19 Exercise: Decoding a Two’s Complement Value What is the decimal value of 10010101 in two’s complement system?

21 CSC255-702/703 CTI/DePaul20 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?

22 CSC255-702/703 CTI/DePaul21 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: -2 15 to -1 Minimum: 1000000000000000 = -32768 0 to 2 15 – 1 Maximum: 0111111111111111= +32767

23 CSC255-702/703 CTI/DePaul22 Common Two’s Complement Mistakes This value is -6 in two’s complement: 1 1 1 1 1 0 1 0 It is incorrect to write -6 as -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

24 CSC255-702/703 CTI/DePaul23 Addition in Two’s Complement Notation

25 CSC255-702/703 CTI/DePaul24 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!

26 CSC255-702/703 CTI/DePaul25 Overflow in Pure Binary Add 134 and 130: 1 1 1 + 134: 1 0 0 0 0 1 1 0 + 130: 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 = +8 ??? O v e r f l o w ! This bit is lost - The answer is wrong!

27 CSC255-702/703 CTI/DePaul26 Subtraction in Two’s Complement... Computing 6 - 2 is same as 6 + (-2): 1 1 1 1 1 1 1 + 6: 0 0 0 0 0 1 1 0 - 2: 1 1 1 1 1 1 1 0 0 0 0 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.

28 CSC255-702/703 CTI/DePaul27 Subtraction in Two’s Complement... Computing -112 - 112: 1 1 1 1 -112: 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 Cannot ignore overflow. Addition of two negative numbers should set the sign bit to 1. In this case, 0:positive, which is not correct.

29 CSC255-702/703 CTI/DePaul28 Addition in Two’s Complement... Computing 112 + 112: 1 1 1 +112: 0 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 Addition of two positive numbers should set the sign bit to 0. In this case, 1:negative, which is not correct.

30 CSC255-702/703 CTI/DePaul29 Exercise: Addition in Two’s Complement... Compute 68 – 120 in 8-bit two’s complement representation +68:? ? ? ? ? ? ? ? -120: ? ? ? ? ? ? ? ? ? ? ? ? Is your answer correct, or did it result in overflow? How do you know? Explain.

31 CSC255-702/703 CTI/DePaul30 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!

32 CSC255-702/703 CTI/DePaul31 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,...

33 CSC255-702/703 CTI/DePaul32 Example: 4-bit Excess Notation

34 CSC255-702/703 CTI/DePaul33 Excess Notation Example Using only 3 bits to simplify things… Pure binaryExcess notation 1 1 1 7 1 1 1 3 1 1 0 6 1 1 0 2 1 0 1 5 1 0 1 1 1 0 0 4 1 0 0 0 0 1 1 3 0 1 1 -1 0 1 0 2 0 1 0 -2 0 0 1 1 0 0 1 -3 0 0 0 0 0 0 0 -4

35 CSC255-702/703 CTI/DePaul34 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

36 CSC255-702/703 CTI/DePaul35 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 10 7

37 CSC255-702/703 CTI/DePaul36 Example: Floating Point Notation Bits to be used for number storage are divided into three groups: 0 1 1 0 1 0 1 1 sign bit mantissa exponent

38 CSC255-702/703 CTI/DePaul37 Example: Floating Point… 0 1 1 0 1 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/2 + 1/4

39 CSC255-702/703 CTI/DePaul38 Exercise: Floating Point What is the decimal value of the following 8-bit floating point binary number? (3 bits exponent, 4 bits mantissa) 0 0 1 0 1 1 0 1

40 CSC255-702/703 CTI/DePaul39 Exercise: Floating Point What is the decimal value of the following 16-bit floating point binary number? (7 bits exponent, 8 bits mantissa) 1 1 0 1 0 1 1 1 1 1 0 1 1 0 0 1

41 CSC255-702/703 CTI/DePaul40 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

42 CSC255-702/703 CTI/DePaul41 Example: Floating Point Representation Using 8 Bits What is sign bit, what is exponent?

43 CSC255-702/703 CTI/DePaul42 Images

44 CSC255-702/703 CTI/DePaul43 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

45 CSC255-702/703 CTI/DePaul44.BMP With 3 bytes/pixel, how many bytes are needed for following resolutions? 2400 x 1800 = 4.3 M x 3b= 13 Mb 1600 x 1200 = 1.9 M x 3b= 5.8 Mb 1280 x 960 = 1.3 M x 3b= 3.9 Mb 800 x 600 = 480 K x 3b= 1.44 Mb 640 x 480 = 307 K x 3b= 921 Kb 320 x 240 = 76.8 K x 3b= 230 Kb

46 CSC255-702/703 CTI/DePaul45 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

47 CSC255-702/703 CTI/DePaul46 GIF (.GIF) Image G raphics I nterchange F ormat Developed by Compuserve (an ISP) Stores only 256 colors Loses some picture quality but is simple and fast Common in computer action games

48 CSC255-702/703 CTI/DePaul47 JPEG (.JPG) Image J oint P hotographic E xperts G roup 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

49 CSC255-702/703 CTI/DePaul48.JPG With 3 bytes/pixel, how many bytes are needed for the following resolutions? 2400 x 1800 = 4.3 M x 3b = 13 Mb 650K 1600 x 1200 = 1.9 M x 3b = 5.8 Mb 290K 1280 x 960 = 1.3 M x 3b = 3.9 Mb 195K 800 x 600 = 480 K x 3b = 1.44 Mb 72K 640 x 480 = 307 K x 3b = 921 Kb 46K 320 x 240 = 76.8 K x 3b = 230 Kb 12K

50 CSC255-702/703 CTI/DePaul49 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

51 CSC255-702/703 CTI/DePaul50 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

52 CSC255-702/703 CTI/DePaul51 Error Detection and Correction

53 CSC255-702/703 CTI/DePaul52 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

54 CSC255-702/703 CTI/DePaul53 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; Example: 1 0 0 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

55 CSC255-702/703 CTI/DePaul54 Example: Generating Odd Parity Value

56 CSC255-702/703 CTI/DePaul55 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

57 CSC255-702/703 CTI/DePaul56 Variations on Parity Checks Checkbytes or checksums - “hash” total computed when data is stored, recomputed when data is read Cyclic redundancy codes (CRC)

58 CSC255-702/703 CTI/DePaul57 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

59 CSC255-702/703 CTI/DePaul58 Example: Hamming Code for Symbols A through H

60 CSC255-702/703 CTI/DePaul59 Example: 1-Bit Error Correction for Pattern 010100 Calculate the number of bits different between each code on table and the given pattern Distance between A(000000) and 010100= 2 Distance between B(001111) and 010100= 4 Distance between C(010011) and 010100= 3 Distance between D(011100) and 010100= 1 Distance between E(100110) and 010100= 3 Distance between F(101001) and 010100= 5 Distance between G(110101) and 010100= 2 Distance between H(111010) and 010100= 4

61 CSC255-702/703 CTI/DePaul60 Example: 1-Bit Error Correction for Pattern 010100 Based on 1-bit error correction, D is the pattern


Download ppt "CSC255-702/703 CTI/DePaul1 CSC-255 Lecture 4 Negative Numbers, Images, Parity (Brookshear Chapter 1, pages 46-72) Modified by Ufuk Verun from Jim Janossy."

Similar presentations


Ads by Google