2Main MemoryMain memory holds information such as computer programs, numeric data, or documents created by a word processor.Main memory is made up of capacitors.If a capacitor is charged, then its state is said to be 1, or ON.We could also say the bit is set.If a capacitor does not have a charge, then its state is said to be 0, or OFF.We could also say that the bit is reset or cleared.
3Main Memory (con’t)Memory is divided into cells, where each cell contains 8 bits (a 1 or a 0). Eight bits is called a byte.Each of these cells is uniquely numbered.The number associated with a cell is known as its address.Main memory is volatile storage. That is, if power is lost, the information in main memory is lost.
4Main Memory (con’t) Other computer components can get the information held at a particular address in memory, known as a READ,or store information at a particular address in memory, known as a WRITE.Writing to a memory location alters its contents.Reading from a memory location does not alter its contents.
5Main Memory (con’t)All addresses in memory can be accessed in the same amount of time.We do not have to start at address 0 and read everything until we get to the address we really want (sequential access).We can go directly to the address we want and access the data (direct or random access).That is why we call main memory RAM (Random Access Memory).
6Secondary Storage Media Disks -- floppy, hard, removable (random access)Tapes (sequential access)CDs (random access)DVDs (random access)Secondary storage media store files that containcomputer programsdataother types of informationThis type of storage is called persistent (permanent) storage because it is non-volatile.
7I/O (Input/Output) Devices Information input and output is handled by I/O (input/output) devices.More generally, these devices are known as peripheral devices.Examples:monitorkeyboardmousedisk drive (floppy, hard, removable)CD or DVD driveprinterscanner
8Bits, Bytes, and Words A bit is a single binary digit (a 1 or 0). A byte is 8 bitsA word is 32 bits or 4 bytesLong word = 8 bytes = 64 bitsQuad word = 16 bytes = 128 bitsProgramming languages use these standard number of bits when organizing data storage and access.
9Number SystemsThe on and off states of the capacitors in RAM can be thought of as the values 1 and 0, respectively.Therefore, thinking about how information is stored in RAM requires knowledge of the binary (base 2) number system.Let’s review the decimal (base 10) number system first.
10The Decimal Number System The decimal number system is a positional number system.Example:X 100 = 1X 101 = 206 X 102 = 6005 X 103 = 5000
11The Decimal Number System (con’t) The decimal number system is also known as base 10. The values of the positions are calculated by taking 10 to some power.Why is the base 10 for decimal numbers?Because we use 10 digits, the digits 0 through 9.
12The Binary Number System The binary number system is also known as base 2. The values of the positions are calculated by taking 2 to some power.Why is the base 2 for binary numbers?Because we use 2 digits, the digits 0 and 1.
13The Binary Number System (con’t) The binary number system is also a positional numbering system.Instead of using ten digits, 0 - 9, the binary system uses only two digits, 0 and 1.Example of a binary number and the values of the positions:
14Converting from Binary to Decimal X 20 = 1X 21 = 01 X 22 = 420 = X 23 = 821 = X 24 = 022 = X 25 = 023 = X 26 = 6424 =25 = 3226 = 64
15Converting from Binary to Decimal (con’t) Practice conversions:Binary Decimal11101100111
16Converting From Decimal to Binary (con’t) Make a list of the binary place values up to the number being converted.Perform successive divisions by 2, placing the remainder of 0 or 1 in each of the positions from right to left.Continue until the quotient is zero.Example: 421042/2 = R = 021/2 = R = 110/2 = R = 05/2 = 2 R = 12/2 = 1 R = 01/2 = 0 R = 1=
17Converting From Decimal to Binary (con’t) Practice conversions:Decimal Binary5982175
18Working with Large Numbers = ?Humans can’t work well with binary numbers; there are too many digits to deal with.Memory addresses and other data can be quite large. Therefore, we sometimes use the hexadecimal number system.
19The Hexadecimal Number System The hexadecimal number system is also known as base 16. The values of the positions are calculated by taking 16 to some power.Why is the base 16 for hexadecimal numbers ?Because we use 16 symbols, the digits 0 and 1 and the letters A through F.
20The Hexadecimal Number System (con’t) Binary Decimal Hexadecimal Binary Decimal HexadecimalABCDEF
21The Hexadecimal Number System (con’t) Example of a hexadecimal number and the values of the positions:3 C B
22Example of Equivalent Numbers Binary:Decimal:Hexadecimal: 50A716Notice how the number of digits gets smaller as the base increases.
23Goals of Today’s Lecture Binary numbersWhy binary?Converting base 10 to base 2Octal and hexadecimalIntegersUnsigned integersInteger additionSigned integersC bit operatorsAnd, or, not, and xorShift-left and shift-rightFunction for counting the number of 1 bitsFunction for XOR encryption of a message
24Why Bits (Binary Digits)? Computers are built using digital circuitsInputs and outputs can have only two valuesTrue (high voltage) or false (low voltage)Represented as 1 and 0Can represent many kinds of informationBoolean (true or false)Numbers (23, 79, …)Characters (‘a’, ‘z’, …)PixelsSoundCan manipulate in many waysRead and writeLogical operationsArithmetic…
25Base 10 and Base 2 Base 10 Each digit represents a power of 10 4173 = 4 x x x x 100Base 2Each bit represents a power of 210110 = 1 x x x x x 20 = 22Divide repeatedly by 2 and keep remainders12/2 = R = 06/2 = R = 03/2 = R = 11/2 = R = 1Result =
26Writing Bits is Tedious for People Octal (base 8)Digits 0, 1, …, 7In C: 00, 01, …, 07Hexadecimal (base 16)Digits 0, 1, …, 9, A, B, C, D, E, FIn C: 0x0, 0x1, …, 0xf0000 = 01000 = 80001 = 11001 = 90010 = 21010 = A0011 = 31011 = B0100 = 41100 = C0101 = 51101 = D0110 = 61110 = E0111 = 71111 = FThus the 16-bit binary numberconverted to hex isB2A9
27Representing Colors: RGB Three primary colorsRedGreenBlueStrength8-bit number for each color (e.g., two hex digits)So, 24 bits to specify a colorIn HTML, on the course Web pageRed: <font color="#FF0000"><i>Symbol Table Assignment Due</i>Blue: <font color="#0000FF"><i>Fall Recess</i></font>Same thing in digital camerasEach pixel is a mixture of red, green, and blue
28Storing Integers on the Computer Fixed number of bits in memoryShort: usually 16 bitsInt: 16 or 32 bitsLong: 32 bitsUnsigned integerNo sign bitAlways positive or 0All arithmetic is modulo 2nExample of unsigned int 1 15 16 33 255
29Adding Two Integers: Base 10 From right to left, we add each pair of digitsWe write the sum, and add the carry to the next column0 1 1SumCarry1 9 8SumCarry46121111
30Binary Sums and Carries a b Sum a b CarryXORAND69103172
31Fractional NumbersExamples: = 4 x x x x x 10-2= 1 x x x x x x 2-2= / ¼= =Conversion from binary number system to decimal systemExamples: = 1 x x x x x 2-2= / ¼ =Examples:½ ¼ 1/8x
32Fractional numbers 4 2 1 Examples: 7.7510 = (?)2 Conversion of the integer part: same as before – repeated division by 27 / 2 = 3 (Q), 1 (R) 3 / 2 = 1 (Q), 1 (R) 1 / 2 = 0 (Q), 1 (R) = 1112Conversion of the fractional part: perform a repeated multiplication by 2 and extract the integer part of the result0.75 x 2 =1.50 extract 10.5 x 2 = extract = 0.112 stop Combine the results from integer and fractional part, =How about choose some ofExamples: try 5.625write in the same order4211/21/41/8=0.5=0.25=0.125
33Fractional Numbers (cont.) Exercise 2: Convert (0.6)10 to its binary formSolution:Exercise 1: Convert (0.625)10 to its binary formSolution: x 2 = 1.25 extract 10.25 x 2 = 0.5 extract 00.5 x 2 = 1.0 extract 10.0 stop (0.625)10 = (0.101)20.6 x 2 = 1.2 extract 10.2 x 2 = 0.4 extract 00.4 x 2 = 0.8 extract 00.8 x 2 = 1.6 extract 10.6 x 2 = (0.6)10 = ( …)2
34Fractional Numbers (cont.) Exercise 3: Convert (0.8125)10 to its binary formSolution: x 2 = extract 10.625 x 2 = 1.25 extract 10.25 x 2 = 0.5 extract 00.5 x 2 = 1.0 extract 10.0 stop (0.8125)10 = (0.1101)2
35Fractional Numbers (cont.) ErrorsOne source of error in the computations is due to back and forth conversions between decimal and binary formatsExample: (0.6)10 + (0.6)10 = 1.210Since (0.6)10 = ( …)2Lets assume a 8-bit representation: (0.6)10 = ( )2 , thereforeLets reconvert to decimal system:( )b= 1 x x x x x x x x x 2-8= 1 + 1/8 + 1/16 + 1/128 = Error = 1.2 –=
36One’s and Two’s Complement One’s complement: flip every bitE.g., b (i.e., 69 in base 10)One’s complement isThat’s simplySubtracting from is easy (no carry needed!)Two’s complementAdd 1 to the one’s complementE.g., (255 – 69) + 1 bone’s complement
37Putting it All Together Computing “a – b” for unsigned integersSame as “a – b”Same as “a + (255 – b) + 1”Same as “a + onecomplement(b) + 1”Same as “a + twocomplement(b)”Example: 172 – 69The original number 69:One’s complement of 69:Two’s complement of 69:Add to the number 172:The sum comes to:Equals: 103 in base 10
38Signed Integers Sign-magnitude representation Use one bit to store the signZero for positive numberOne for negative numberExamplesE.g., 44E.g., -44Hard to do arithmetic this way, so it is rarely usedComplement representationOne’s complementFlip every bitE.g., -44Two’s complementFlip every bit, then add 1E.g., -44
39Overflow: Running Out of Room Adding two large integers togetherSum might be too large to store in the number of bits allowedWhat happens?
40Bitwise Operators: AND and OR Bitwise AND (&)Mod on the cheap!E.g., h = 53 & 15;Bitwise OR (|)&1|115311515
41Bitwise Operators: Shift Left/Right Shift left (<<): Multiply by powers of 2Shift some # of bits to the left, filling the blanks with 0Shift right (>>): Divide by powers of 2Shift some # of bits to the rightFor unsigned integer, fill in blanks with 0What about signed integers? Varies across machines…Can vary from one machine to another!15353<<25311153>>2111
42XOR EncryptionProgram to encrypt text with a keyInput: original text in stdinOutput: encrypted text in stdoutUse the same program to decrypt text with a keyInput: encrypted text in stdinOutput: original text in stdoutBasic ideaStart with a key, some 8-bit number (e.g., )Do an operation that can be invertedE.g., XOR each character with the 8-bit number^^
43Conclusions Computer represents everything in binary Integers, floating-point numbers, characters, addresses, …Pixels, sounds, colors, etc.Binary arithmetic through logic operationsSum (XOR) and Carry (AND)Two’s complement for subtractionBinary operations in CAND, OR, NOT, and XORShift left and shift rightUseful for efficient and concise code, though sometimes cryptic