CSC255-702/703 CTI/DePaul1 CSC-255 Lecture 4 Negative Numbers, Images, Parity (Brookshear Chapter 1,...
-
date post
20-Dec-2015 -
Category
Documents
-
view
219 -
download
2
Transcript of CSC255-702/703 CTI/DePaul1 CSC-255 Lecture 4 Negative Numbers, Images, Parity (Brookshear Chapter 1,...
CSC255-702/703 CTI/DePaul 1
CSC-255
Lecture 4Negative 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.
CSC255-702/703 CTI/DePaul 2
Resources
This PowerPoint presentation is available for download at www.depaul.edu/~uverun/classes/CSC255/fall2002/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
CSC255-702/703 CTI/DePaul 3
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
CSC255-702/703 CTI/DePaul 4
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
CSC255-702/703 CTI/DePaul 5
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
CSC255-702/703 CTI/DePaul 7
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
CSC255-702/703 CTI/DePaul 8
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
CSC255-702/703 CTI/DePaul 9
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)
CSC255-702/703 CTI/DePaul 11
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
CSC255-702/703 CTI/DePaul 12
Exercise: Two’s Complement for -117
What is two’s complement code for value -117 using 8 bits?
+117: ? ? ? ? ? ? ? ?
-117: ? ? ? ? ? ? ? ?
CSC255-702/703 CTI/DePaul 13
Exercise: Two’s Complement for -210
What is two’s complement code for -210 using 8 bits? (hint: trick question)
+210: ? ? ? ? ? ? ? ?
-210: ? ? ? ? ? ? ? ?
CSC255-702/703 CTI/DePaul 14
“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
CSC255-702/703 CTI/DePaul 15
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
CSC255-702/703 CTI/DePaul 16
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 12Make sure you know why!
CSC255-702/703 CTI/DePaul 17
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...
CSC255-702/703 CTI/DePaul 18
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
CSC255-702/703 CTI/DePaul 19
Exercise: Decoding a Two’s Complement Value
What is the decimal value of 10010101 in two’s complement system?
CSC255-702/703 CTI/DePaul 20
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?
CSC255-702/703 CTI/DePaul 21
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:
-215 to -1Minimum: 1000000000000000 = -32768
0 to 215 – 1Maximum: 0111111111111111= +32767
CSC255-702/703 CTI/DePaul 22
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 -11111010It is incorrect to write -6 as -00000110It is incorrect to write -6 as 10000110The fact that 11111010 is negative is contained entirely in the leading “1” and two’s complement notation
CSC255-702/703 CTI/DePaul 24
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!
CSC255-702/703 CTI/DePaul 25
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!
CSC255-702/703 CTI/DePaul 26
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.
CSC255-702/703 CTI/DePaul 27
Subtraction in Two’s Complement...
Computing -112 - 112: 1 1 1 1
-112: 1 0 0 1 0 0 0 0-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.
CSC255-702/703 CTI/DePaul 28
Addition in Two’s Complement...
Computing 112 + 112: 1 1 1
+112: 0 1 1 1 0 0 0 0+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.
CSC255-702/703 CTI/DePaul 29
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.
CSC255-702/703 CTI/DePaul 30
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 subtractionSo the same circuitry used to perform two’s complement addition can handle all four arithmetic operations!
CSC255-702/703 CTI/DePaul 31
Excess Notation
A different way to reassign half the range of bit patterns to hold negative numbersFirst pattern with high order “1” represents value 0, next highest represents value 1, and so onValues lower represent -1, -2, -3, . . .
CSC255-702/703 CTI/DePaul 33
Excess Notation Example
Using only 3 bits to simplify things… Pure binary Excess 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
CSC255-702/703 CTI/DePaul 34
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
CSC255-702/703 CTI/DePaul 35
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
CSC255-702/703 CTI/DePaul 36
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
CSC255-702/703 CTI/DePaul 37
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
CSC255-702/703 CTI/DePaul 38
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
CSC255-702/703 CTI/DePaul 39
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
CSC255-702/703 CTI/DePaul 40
Imprecision in Number Storage
Truncation: not enough bits to store number, so least significant digits lostNonterminating expansions like 1/3 = .3333…More nonterminating expansions exist in binary than in decimalUsers are generally shielded from these effects by application software
CSC255-702/703 CTI/DePaul 41
Example: Floating Point Representation Using 8 Bits
What is sign bit, what is exponent?
CSC255-702/703 CTI/DePaul 43
Image as Bitmap (.BMP)
Image is collection of dots (pixels)Pixel = “picture element”Black & white: one bit per pixelColor: 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
CSC255-702/703 CTI/DePaul 44
.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
CSC255-702/703 CTI/DePaul 45
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
CSC255-702/703 CTI/DePaul 46
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
CSC255-702/703 CTI/DePaul 47
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
CSC255-702/703 CTI/DePaul 48
.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
CSC255-702/703 CTI/DePaul 49
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
CSC255-702/703 CTI/DePaul 50
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
CSC255-702/703 CTI/DePaul 52
Bit Errors
Data storage mediums like memory, magnetic disk, CD/ROM can introduce errors (bits can get “flipped”)Noisy communication circuits can do this tooTechniques are used to overcome these limitations and make data storage and communication reliable
CSC255-702/703 CTI/DePaul 53
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
CSC255-702/703 CTI/DePaul 55
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
CSC255-702/703 CTI/DePaul 56
Variations on Parity Checks
Checkbytes or checksums - “hash” total computed when data is stored, recomputed when data is read
Cyclic redundancy codes (CRC)
CSC255-702/703 CTI/DePaul 57
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
CSC255-702/703 CTI/DePaul 59
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