100 likes | 221 Views
CS 111 – Sept. 8. Finish image rep’n Adding binary numbers Integer rep’n in general Unsigned Signed the most important of the 3 Biased Commitment: Meet here tomorrow Please read pp. 58-64 Quiz Friday. RGB examples. Q: How do we get “other” shades of blue?.
E N D
CS 111 – Sept. 8 • Finish image rep’n • Adding binary numbers • Integer rep’n in general • Unsigned • Signed the most important of the 3 • Biased • Commitment: • Meet here tomorrow • Please read pp. 58-64 • Quiz Friday
RGB examples Q: How do we get “other” shades of blue?
Indexed color • Do we really need 24 bits to represent color of one pixel? • This means we allocate 16,777,216 colors! • About 200 would be more practical • Indexed color is a “compressed” RGB • 6 values of each primary color, not 256 • Use hex values 00, 33, 66, 99, cc, ff • This is the color system used on the Web. • 1 byte per pixel instead of 3 • Use “dithering” to simulate in-between colors.
Binary addition • Analogous to decimal addition you know • Only a few cases to consider – just watch out for carry. • 0 + 0 = 0 (no carry) • 0 + 1 = 1 (no carry) • 1 + 1 = 10 (sum = 0, carry = 1) • 1 + 1 + 1 = 11 (sum = 1, carry = 1) • Example, 6-bit addition: 001110 + 001100 • Can check our answer in base 10 • Overflow: correct answer is beyond possible range
Integer rep’n • How do we represent integers inside the computer? • Scheme I: unsigned • Scheme II: signed (a.k.a. Two’s complement) • Scheme III: biased (a.k.a. Excess notation) • Scheme I: Unsigned • This is the scheme you already know. • Cannot handle negative numbers. • For n bits, possible range is 0 to 2n – 1. • Scheme II: Signed • Basic idea: half of the representations should be negative. • Ex. For 5 bits, 16 of the 32 values are negative, so the range goes from –16 to +15. • For n bits, possible range is –2n–1 to 2n–1 – 1.
Signed rep’n continued • How do we represent a number in signed? • If positive, same as unsigned. • Ex. 6-bit signed rep’n of 13 is 001101. • Ex. 6-bit signed rep’n of 31 is 011111. (the largest #) • If negative: 3 steps to represent –x: • Find rep’n of +x. • Invert the bits. • Add 1. • Try some examples of negative numbers, and check answers.
Closer look… • In 5-bit unsigned… • Smallest number is 00000 (= 0) • Largest number is 11111 (= 31) • In 5-bit signed… • Smallest number is 10000 (= –16) • Largest number is 01111 (= 15) • Given a bit pattern, its signed and unsigned values differ by how much? • Try some examples. • In signed: • Leftmost bit is the sign bit. • Positive #’s have same rep’n as unsigned. • Technique for –x doesn’t work for lowest number. Special case.
Signed + and – • Signed + is like unsigned. • Watch out for overflow. • The correct mathematical result can’t be represented. • (Pos) + (Pos) = (Neg) • (Neg) + (Neg) = (Pos) • Example: 01111 + 00001. • To subtract, add the opposite. Example: 10111 – 00111 • First, –(00111) = 11001 • Turn into addition problem: 10111 + 11001 = __________ • Is there overflow?
Scheme III: biased • Another way to represent integers that allows for negatives. • (It will soon help us see how real numbers are stored.) • The “bias” is the number we subtract from unsigned range. • If B is the bias, the lowest number is –B. • When working with a biased rep’n, you have to be given the bias. • Ex. For 6-bits, bias is typically 31 or 32. • Ex. For 8-bits, bias is typically 127 or 128. • So, a “6 bit biased-31 rep’n” is based on 6-bit unsigned, except that 000000 is now –31 instead of 0.
How to convert • How do we represent a number n in biased (B)? • Add the bias: n + B. • Determine the unsigned rep’n of this number. • Example: What is the 6-bit biased-31 rep’n of –9? • It’s the same as the unsigned rep’n of –9 + 31 = 22. • 22 in 6 bit unsigned is 010110. • How do we convert a biased number back into base 10? • Interpret the number as unsigned • Subtract the bias. • Example: 101010 is the 6-bit biased-31 rep’n of what number? • If unsigned, 101010 = 32+8+2 = 42. • 42 – 31 = 11.