170 likes | 270 Views
Abstraction – Number Systems and Data Representation. Memory. Computer memory is used to store data The smallest unit of memory is a bit ( B inary dig IT ) A bit can be off (no voltage) or on (has voltage) which we interpret to be 0 or 1
E N D
Memory • Computer memory is used to store data • The smallest unit of memory is a bit (Binary digIT) • A bit can be off (no voltage) or on (has voltage) which we interpret to be 0 or 1 • Memory is organized into 8 bit contiguous groups called bytes. A megabyte is 1,048,576 bytes (over 1 million bytes). A gigabyte is over 1 billion bytes.
How does Memory Represent Values? • The different patterns of the on and off bits in a byte determine the value stored • Numbers are stored using binary numbers • 101 is 1 * 20 + 0 * 21 + 1 * 22 = 5 • Characters are internally represented as numbers • Different numbers represent different characters • There are several systems for assigning numbers to characters: • ASCII, EBCDIC, and Unicode
Encodings Make Computers Powerful • Voltages are interpreted as numbers • Numbers can be interpreted as characters • Characters can be interpreted to be part of a link to Sun’s Java Site <a href=http://java.sun.com>Sun’s Java Site </a> a 0100 0001 off on off off off off off on
Notepad Exercise • Open notepad and type a sentence in it • Save the file • Check the size in bytes by leaving the cursor over the file name • Or right click and check properties • Now count the number of letters and spaces
Binary Numbers • A bit is a binary digit with a value of 0 or 1 • A group of 8 bits is a byte • Computer memory is allocated in bytes • Numbers are stored using the binary number system • With digits of 0 or 1 and powers of 2 • Other number systems • Decimal- digits of 0 to 9 and powers of 10 • Octal - digits of 0 to 7 and powers of 8 • Hexadecimal – digits of 0 to 9 and A, B, C, D, E, F and powers of 16. 05-DrawingInJava
Converting from Binary to Decimal • Multiply the digit value times the place value and add up the results to convert from binary to decimal • The place values start with 20 on the right (which is 1) and increase to the left
Converting from Decimal to Binary • Subtraction Method Keep subtracting out largest power of two until nothing remains
Converting from Decimal to Binary • Division Method Read result from top to bottom.
To add two decimal numbers you add the digits and if the total is greater than ten you carry the one into the next column To add two binary numbers 0 + 0 = 0 0 + 1 and 1 + 0 = 1 1 + 1 = 0 with a carry of 1 into the next column to the left 00 10 111 01 01 001 ---- --- ------ 01 11 1000 00111001010 01010101101 ------------------- 10001110111 Binary Addition
2’s Compliment Notation • Computers actually only know how to add • So, how do they handle subtraction? • Computers subtract by adding a negative number • How do you represent a negative number in memory? • Positive numbers in 2’s compliment are just the same as a binary number • For negative numbers reverse 0s and 1s and then add 1
2’s Compliment Example • To subtract 3 from 7 • First represent both as a binary number • 7 is 0000 0111 • 3 is 0000 0011 • Reverse the 0s and 1s and then add 1 to get -3 • 0000 0011 reversed is 1111 1100 • add 1 0000 0001 • The result is 1111 1101
Add the Negative Number • To subtract 3 from 7 • Add -3 to 7 • 7 is 0000 0111 • -3 is 1111 1101 • The result is 1 0000 0100 • Throw away the leftmost 1 • The answer is 0000 0100 which is 4
Patterns Exercise • How many different patterns of on and off bits are there in 3 bits? How many in 4 bits? How many in 8 bits? • 000 is one pattern • 001 is another pattern • 010 is another pattern
Does the number of patterns matter? • Some garage door openers in the 70s used 8 bits to set the code to use to open the door • Giving 256 different patterns • Which is enough that you won’t open your neighbors door • But small enough that someone could try each one
Remote Entry Systems • With 8 bits for a code you have a 1/256 chance of a random code working • You don’t want someone opening your car in a place with lots of cars (like a mall) • There are also radio scanners that can capture your code • So you want the code to change each time • Modern remote entry systems use a 40 bit rolling code