Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Binary Numbers. 2 Main Memory Main memory holds information such as computer programs, numeric data, or documents created by a word processor. Main.

Similar presentations


Presentation on theme: "1 Binary Numbers. 2 Main Memory Main memory holds information such as computer programs, numeric data, or documents created by a word processor. Main."— Presentation transcript:

1 1 Binary Numbers

2 2 Main Memory Main 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.

3 3 Main Memory (cont) 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.

4 4 Main Memory (cont) Other computer components can o get the information held at a particular address in memory, known as a READ, o 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.

5 5 Main Memory (cont) 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).

6 6 Secondary Storage Media Disks -- floppy, hard, removable (random access) Tapes (sequential access) CDs (random access) DVDs (random access) Secondary storage media store files that contain computer programs data other types of information This type of storage is called persistent (permanent) storage because it is non-volatile.

7 7 I/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: monitor keyboard mouse disk drive (floppy, hard, removable) CD or DVD drive printer scanner

8 8 Bits, Bytes, and Words A bit is a single binary digit (a 1 or 0). A byte is 8 bits A word is 32 bits or 4 bytes Long word = 8 bytes = 64 bits Quad word = 16 bytes = 128 bits Programming languages use these standard number of bits when organizing data storage and access.

9 9 Number Systems The 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. Lets review the decimal (base 10) number system first.

10 10 The Decimal Number System The decimal number system is a positional number system. Example: X 10 0 = X 10 1 = 20 6 X 10 2 = X 10 3 = 5000

11 11 The Decimal Number System (cont) 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? o Because we use 10 digits, the digits 0 through 9.

12 12 The 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? o Because we use 2 digits, the digits 0 and 1.

13 13 The Binary Number System (cont) 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:

14 14 Converting from Binary to Decimal X 2 0 = X 2 1 = 0 1 X 2 2 = = 1 1 X 2 3 = = 2 0 X 2 4 = = 4 0 X 2 5 = = 8 1 X 2 6 = = = = 64

15 15 Converting from Binary to Decimal (cont) Practice conversions: Binary Decimal

16 16 Converting From Decimal to Binary (cont) 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: /2 = 21 R = 0 21/2 = 10 R = 1 10/2 = 5 R = 0 5/2 = 2 R = 1 2/2 = 1 R = 0 1/2 = 0 R = =

17 17 Converting From Decimal to Binary (cont) Practice conversions: Decimal Binary

18 18 Working with Large Numbers = ? Humans cant 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.

19 19 The 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.

20 20 The Hexadecimal Number System (cont) Binary Decimal Hexadecimal A B C D E F

21 21 The Hexadecimal Number System (cont) Example of a hexadecimal number and the values of the positions: 3 C 8 B

22 22 Example of Equivalent Numbers Binary: Decimal: Hexadecimal: 50A7 16 Notice how the number of digits gets smaller as the base increases.

23 23 Goals of Todays Lecture Binary numbers Why binary? Converting base 10 to base 2 Octal and hexadecimal Integers Unsigned integers Integer addition Signed integers C bit operators And, or, not, and xor Shift-left and shift-right Function for counting the number of 1 bits Function for XOR encryption of a message

24 24 Why Bits (Binary Digits)? Computers are built using digital circuits Inputs and outputs can have only two values True (high voltage) or false (low voltage) Represented as 1 and 0 Can represent many kinds of information Boolean (true or false) Numbers (23, 79, …) Characters (a, z, …) Pixels Sound Can manipulate in many ways Read and write Logical operations Arithmetic …

25 25 Base 10 and Base 2 Base 10 Each digit represents a power of = 4 x x x x 10 0 Base 2 Each bit represents a power of = 1 x x x x x 2 0 = 22 Divide repeatedly by 2 and keep remainders 12/2 = 6 R = 0 6/2 = 3 R = 0 3/2 = 1 R = 1 1/2 = 0 R = 1 Result = 1100

26 26 Writing Bits is Tedious for People Octal (base 8) Digits 0, 1, …, 7 In C: 00, 01, …, 07 Hexadecimal (base 16) Digits 0, 1, …, 9, A, B, C, D, E, F In C: 0x0, 0x1, …, 0xf 0000 = = = = = = A 0011 = = B 0100 = = C 0101 = = D 0110 = = E 0111 = = F Thus the 16-bit binary number converted to hex is B2A9

27 27 Representing Colors: RGB Three primary colors Red Green Blue Strength 8-bit number for each color (e.g., two hex digits) So, 24 bits to specify a color In HTML, on the course Web page Red: Symbol Table Assignment Due Blue: Fall Recess Same thing in digital cameras Each pixel is a mixture of red, green, and blue

28 28 Storing Integers on the Computer Fixed number of bits in memory Short: usually 16 bits Int: 16 or 32 bits Long: 32 bits Unsigned integer No sign bit Always positive or 0 All arithmetic is modulo 2 n Example of unsigned int

29 29 Adding Two Integers: Base 10 From right to left, we add each pair of digits We write the sum, and add the carry to the next column Sum Carry Sum Carry

30 30 Binary Sums and Carries abSumabCarry XORAND

31 31 Fractional Numbers Examples: = 4 x x x x x = 1 x x x x x x 2 -2 = /2 + ¼ = = Conversion from binary number system to decimal system Examples: = 1 x x x x x 2 -2 = /2 + ¼ = Examples: ½ ¼ 1/ xxxx

32 32 Fractional numbers Examples: = (?) 2 1. Conversion of the integer part: same as before – repeated division by 2 7 / 2 = 3 (Q), 1 (R) 3 / 2 = 1 (Q), 1 (R) 1 / 2 = 0 (Q), 1 (R) 7 10 = Conversion of the fractional part: perform a repeated multiplication by 2 and extract the integer part of the result 0.75 x 2 =1.50 extract x 2 = 1.0 extract = stop Combine the results from integer and fractional part, = How about choose some of Examples: try write in the same order 421 1/21/41/8 =0.5 =0.25=0.125

33 33 Fractional Numbers (cont.) Exercise 2: Convert (0.6) 10 to its binary form Solution: Exercise 1: Convert (0.625)10 to its binary form Solution: x 2 = 1.25 extract x 2 = 0.5 extract x 2 = 1.0 extract stop (0.625) 10 = (0.101) x 2 = 1.2 extract x 2 = 0.4 extract x 2 = 0.8 extract x 2 = 1.6 extract x 2 = (0.6) 10 = ( …) 2

34 34 Fractional Numbers (cont.) Exercise 3: Convert (0.8125) 10 to its binary form Solution: x 2 = extract x 2 = 1.25 extract x 2 = 0.5 extract x 2 = 1.0 extract stop (0.8125) 10 = (0.1101) 2

35 35 Fractional Numbers (cont.) Errors One source of error in the computations is due to back and forth conversions between decimal and binary formats Example: (0.6) 10 + (0.6) 10 = Since (0.6) 10 = ( …) 2 Lets assume a 8-bit representation: (0.6) 10 = ( ) 2, therefore Lets 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 – =

36 36 Ones and Twos Complement Ones complement: flip every bit E.g., b (i.e., 69 in base 10) Ones complement is Thats simply Subtracting from is easy (no carry needed!) Twos complement Add 1 to the ones complement E.g., (255 – 69) b ones complement

37 37 Putting it All Together Computing a – b for unsigned integers Same as a – b Same as a + (255 – b) + 1 Same as a + onecomplement(b) + 1 Same as a + twocomplement(b) Example: 172 – 69 The original number 69: Ones complement of 69: Twos complement of 69: Add to the number 172: The sum comes to: Equals: 103 in base

38 38 Signed Integers Sign-magnitude representation Use one bit to store the sign Zero for positive number One for negative number Examples E.g., E.g., Hard to do arithmetic this way, so it is rarely used Complement representation Ones complement Flip every bit E.g., Twos complement Flip every bit, then add 1 E.g.,

39 39 Overflow: Running Out of Room Adding two large integers together Sum might be too large to store in the number of bits allowed What happens?

40 40 Bitwise Operators: AND and OR Bitwise AND (&) Mod on the cheap! E.g., h = 53 & 15; & Bitwise OR (|) |

41 41 Bitwise Operators: Shift Left/Right Shift left (<<): Multiply by powers of 2 Shift some # of bits to the left, filling the blanks with 0 Shift right (>>): Divide by powers of 2 Shift some # of bits to the right For unsigned integer, fill in blanks with 0 What about signed integers? Varies across machines… Can vary from one machine to another! << >>2

42 42 XOR Encryption Program to encrypt text with a key Input: original text in stdin Output: encrypted text in stdout Use the same program to decrypt text with a key Input: encrypted text in stdin Output: original text in stdout Basic idea Start with a key, some 8-bit number (e.g., ) Do an operation that can be inverted E.g., XOR each character with the 8-bit number ^ ^

43 43 Conclusions Computer represents everything in binary Integers, floating-point numbers, characters, addresses, … Pixels, sounds, colors, etc. Binary arithmetic through logic operations Sum (XOR) and Carry (AND) Twos complement for subtraction Binary operations in C AND, OR, NOT, and XOR Shift left and shift right Useful for efficient and concise code, though sometimes cryptic


Download ppt "1 Binary Numbers. 2 Main Memory Main memory holds information such as computer programs, numeric data, or documents created by a word processor. Main."

Similar presentations


Ads by Google