COMP155 ch3
3) R's and (R-1)'s Complement
Mathematicians have used the Complements system forever - There are R's Complement & (R-1)'s Complements in all number bases - Allows us to do subtraction by just adding the complement! R-1's Comp - Subtract each digit of your number from the largest digit in the system - Example: in a 3-digit decimal system, we store -23 as: 999- 23 = 976 (the 9 indicates our number is actually negative) NB in a three digit system, we get magnitudes of only 2 digits NB we would also use: (1000-1) - 123 or (103 -1) - 23 (hence R-1's name) R's Comp - Subtract your number from Rnumber-of-digits or 100010 in our case - So, -23 is (103 - 23) = (1000 - 23) = 977
ASCII (ass-key) American Standard Code for Information Interchange
Originally 7-bit code 27 (128) permutations: 26 lowercase; 26 uppercase; 10 digits; some 30 punctuation; a set of codes to control the machine IBM PC (and others) decided to make it 8 bits (one byte) and they designed another 128 characters for the extended ASCII set (smiley faces; cards suits; etc etc etc). There is a defined standard for 8 bits: ISO/IEC 8859 But lots of folks still use their own character sets for "extended ASCII" REMEMBER: not everyone in the word uses this
How is knowledge stored in computers
using [0,1] to represent other knowledge
2) Floating Point realm
we limit the accuracy but get a vastly larger range of number : - So programmers must be aware of roundoff errors
1) Integer realm
we limit the range of numbers: - So in 32 bits, we can store roughly 2 billion - If we try to store the USA National Debt as a 32-bit integer, it simply won't fit - So programmers must be aware of overflow and underflow errors
3 cont.) Nine's Complement Arithmetic
3 Digit System -23 = 103 -1 - 23 = 976 36 - 23 = 36 + (-23) = 036 + 976 1,012 We must do a "wrap-around carry", adding the 1 that was carried out into the 1000's column back in to our answer wrap around carry is one of the kinks in (R-1)'s arithmetic = 12 + 1 = 13
3 cont.) Ten's Complement Arithmetic
3 digit example: -23 stored as 1000 - 23 = 977 So 36 - 23 = 36 + (-23) = 036 + 977 = 1, 013 We "throw out" the carry into the 4th column, giving 13 as the answer We can do subtraction by just adding the complement!
3 cont.) One's Complement Arithmetic
5 - 3 in One's Comp 5 is stored as just 0101 ('regular' binary) -3 is stored as 1100 So we can 'subtract' by just adding 0101 + 1100 = 1 0001 That left-most 1 was 'carried out' of the msb One's comp says we must do a 'wrap-around' carry and add it back to our result 1 + 0001 = 0010 (or 2, so 5-3 'works'!) Additionally, what is the value of 1's comp: 1111? Again, our mathematical friends go ape
3 cont.) Two's Complement Arithmetic
5 - 3 in Two's Comp 5 is stored as just 0101 ('regular' binary) -3 is stored as 1101 So we can 'subtract' by just adding 0101 + 1101 = 0010 No hinky wrap-around carry stuff Two's comp has only one rep for zero What is -0 in One's Comp is one 'extra negative number in Two's Comp
Other Codes: we move to file types (codecs)
A codec is a device or computer program for encoding or decoding a digital data stream or signal. (Wikipedia) Images (art) A veritable Tower of Babel .BMP, .JPG, .GIF, .PNG, .TIFF, etc etc etc Audio (music) .MP3, .MPE, .WAV, .WMA, etc etc etc Video (movies) .AVI, .MP4, .MOV, .SWF, etc etc etc
Text representation in code
ASCII (ass-key) American Standard Code for Information Interchange
1) Sign and Magnitude
Decide to sacrifice one bit as a sign Zero for positive; One for negative Example: 5 as a signed magnitude number (in 4 bits) 0101 is +5 1101 is -5 It is easy to understand and to code But there are some problems - You get positive and negative zero!!! (mathematicians go ape) - We must do some acrobatics to get the machine to do arithmetic
3 cont.) R's and (R-1)'s Complement in Binary
In BINARY we get to One's Complement (e.g. 4 bits) In binary we store -5 as: 10000 - 1 - 0101 = 1010 (FLIP FLOP 0s and 1s from 0101 to 1010) We store -3 as 10000 - 1 - 0011 = 1100 That is, we just "flip the bits" Two's Complement -5 is stored as 1000 - 0101 = 1011 -3 is stored as 1000 - 0011 = 1101 That is, we "flip the bits and add one" In both systems, a leading one is a 'tell' that we have a negative
Infinite set of numbers : Finite set of bits 2 solutions
Integer (natural numbers): severely limit the range; absolute precision Real (floating point; has a fractional part): Expand the Range; give up some precision IEEE 754 BUT Negative numbers raises another problem: no '-' in [0,1]
Two Separate Solutions
Integers vs Floating Point numbers
How does a computer know how to interpret bits?
The coder must TELL IT
2) Excess Notation
We add an offset to our number, so all numbers look like positives We just remember to subtract off the offset Example: let "excess" be a 1 in the most significant bit, followed by (n-1) zeroes. So in a 4-bit number, the "excess" is 10002 or 810 +5 will be stored as 5 + 8 = 13 = 1101 -5 will be stored as -5 + 8 = 3 = 0011 We did get rid of the two zeroes, but there is still some falderal about doing arithmetic.
Integers (Natural Numbers)
We already know how to convert to/from decimal. But we never discussed negative numbers: Binary has no '+' or '-' There are three common choices used: 1) Sign & magnitude 2) Excess notation 3) Two's Complement
The Fundamental Problem
We have an INFINITE set of numbers But only a FINITE number of bits to represent them Consider a 32-bit "word" There are 232 (4,294,967,296 or 4 gig) permutations So, of the INFINITE set of numbers, we can store about 4 billion