1 / 43

Binary Numbers

Binary Numbers. 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 .

Download Presentation

Binary Numbers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary Numbers

  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 isset. • 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. Main 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.

  4. Main 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.

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

  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. 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. 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. 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. • Let’s review the decimal (base 10) number system first.

  10. The Decimal Number System • The decimal number system is a positional number system. • Example: • 5 6 2 1 1 X 100 = 1 • 103 102 101 100 2 X 101 = 20 • 6 X 102 = 600 • 5 X 103 = 5000

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

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

  13. The 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: • 1001101 • 26 25 24 23 22 21 20

  14. Converting from Binary to Decimal • 10011011 X 20 = 1 • 26 25 24 23 22 21 20 0 X 21 = 0 • 1 X 22 = 4 • 20 = 1 1 X 23 = 8 • 21 = 20 X 24 = 0 • 22 = 40 X 25 = 0 • 23 = 81 X 26 = 64 • 24 = 16 7710 • 25 = 32 • 26 = 64

  15. Converting from Binary to Decimal (con’t) Practice conversions: BinaryDecimal 11101 1010101 100111

  16. Converting 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: 4210 • 25 24 23 22 21 20 • 32 16 8 4 2 1 • 101010 • 42/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 = 1 • 4210 = 1010102

  17. Converting From Decimal to Binary (con’t) Practice conversions: DecimalBinary 59 82 175

  18. Working with Large Numbers • 0 1 0 1 0 0 0 0 1 0 1 0 0 1 1 1 = ? • 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.

  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. The Hexadecimal Number System (con’t) BinaryDecimalHexadecimalBinaryDecimalHexadecimal 0 0 0 1010 10 A 1 1 1 1011 11 B 10 2 2 1100 12 C 11 3 3 1101 13 D 100 4 4 1110 14 E 101 5 5 1111 15 F 110 6 6 111 7 7 1000 8 8 1001 9 9

  21. The Hexadecimal Number System (con’t) • Example of a hexadecimal number and the values of the positions: • 3C8B051 • 166 165 164 163 162 161 160

  22. Example of Equivalent Numbers • Binary: 1 0 1 0 0 0 0 1 0 1 0 0 1 1 12 • Decimal: 2064710 • Hexadecimal: 50A716 • Notice how the number of digits gets smaller as the base increases.

  23. Goals of Today’s 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. 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. Base 10 and Base 2 • Base 10 • Each digit represents a power of 10 • 4173 = 4 x 103 + 1 x 102 + 7 x 101 + 3 x 100 • Base 2 • Each bit represents a power of 2 • 10110 = 1 x 24 + 0 x 23 + 1 x 22 + 1 x 21 + 0 x 20 = 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. 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 Thus the 16-bit binary number 1011 0010 1010 1001 converted to hex is B2A9

  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: <font color="#FF0000"><i>Symbol Table Assignment Due</i> • Blue: <font color="#0000FF"><i>Fall Recess</i></font> • Same thing in digital cameras • Each pixel is a mixture of red, green, and blue

  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 2n • Example of unsigned int • 00000001  1 • 00001111  15 • 00010000  16 • 00100001  33 • 11111111  255

  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 0 1 1 + 0 0 1 Sum Carry 1 9 8 + 2 6 4 Sum Carry 4 0 6 1 2 1 1 0 0 1 0 1

  30. Binary Sums and Carries a b Sum a b Carry 0 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 1 1 XOR AND 0100 0101 69 + 0110 0111 103 1010 1100 172

  31. 2 1 0 -1 -2 -3 22 21 20 2-1 2-2 2-3 4 2 1 ½ ¼ 1/8 x x x x Fractional Numbers Examples:456.7810 = 4 x 102 + 5 x 101 + 6 x 100 + 7 x 10-1+8 x 10-2 1011.112 = 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 + 1 x 2-1 + 1 x 2-2 = 8 + 0 + 2 + 1 + 1/2 + ¼ = 11 + 0.5 + 0.25 = 11.7510 • Conversion from binary number system to decimal system Examples: 111.112 = 1 x 22 + 1 x 21 + 1 x 20 + 1 x 2-1 + 1 x 2-2 = 4 + 2 + 1 + 1/2 + ¼ = 7.7510 Examples: 11.0112

  32. write in the same order Fractional numbers Examples: 7.7510 = (?)2 • 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) 710 = 1112 • 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 1 0.5 x 2 = 1.0  extract 1 0.7510 = 0.112 0.0  stop  Combine the results from integer and fractional part, 7.7510 = 111.112 How about choose some of Examples: try 5.625 4 2 1 1/2 1/4 1/8 =0.25 =0.125 =0.5

  33. 0.6 x 2 = 1.2  extract 1 • 0.2 x 2 = 0.4  extract 0 • 0.4 x 2 = 0.8  extract 0 • 0.8 x 2 = 1.6  extract 1 • 0.6 x 2 =  •  (0.6)10= (0.1001 1001 1001 …)2 Fractional Numbers (cont.) Exercise 2: Convert (0.6)10to its binary form Solution: Exercise 1: Convert (0.625)10 to its binary form • Solution: 0.625 x 2 = 1.25  extract 1 • 0.25 x 2 = 0.5  extract 0 • 0.5 x 2 = 1.0  extract 1 • 0.0  stop •  (0.625)10= (0.101)2

  34. Fractional Numbers (cont.) Exercise 3: Convert (0.8125)10 to its binary form • Solution: 0.8125 x 2 = 1.625  extract 1 • 0.625 x 2 = 1.25  extract 1 • 0.25 x 2 = 0.5  extract 0 • 0.5 x 2 = 1.0  extract 1 • 0.0  stop •  (0.8125)10= (0.1101)2

  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 = 1.210 Since (0.6)10= (0.1001 1001 1001 …)2 Lets assume a 8-bit representation: (0.6)10= (0 .10011001)2 , therefore 0.6 0.10011001 + 0.6  + 0.10011001 1.00110010 Lets reconvert to decimal system: (1.00110010)b= 1 x 20 + 0 x 2-1 + 0 x 2-2 + 1 x 2-3 + 1 x 2-4 + 0 x 2-5 + 0 x 2-6 + 1 x 2-7 + 0 x 2-8 = 1 + 1/8 + 1/16 + 1/128 = 1.1953125  Error = 1.2 – 1.1953125 = 0.0046875

  36. One’s and Two’s Complement • One’s complement: flip every bit • E.g., b 01000101 (i.e., 69 in base 10) • One’s complement is 10111010 • That’s simply 255-69 • Subtracting from 11111111 is easy (no carry needed!) • Two’s complement • Add 1 to the one’s complement • E.g., (255 – 69) + 1  1011 1011 1111 1111 - 0100 0101 b one’s complement 1011 1010

  37. Putting it All Together • Computing “a – b” for unsigned integers • Same as “a + 256 – 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: 0100 0101 • One’s complement of 69: 1011 1010 • Two’s complement of 69: 1011 1011 • Add to the number 172: 1010 1100 • The sum comes to: 0110 0111 • Equals: 103 in base 10 1010 1100 + 1011 1011 1 0110 0111

  38. Signed Integers • Sign-magnitude representation • Use one bit to store the sign • Zero for positive number • One for negative number • Examples • E.g., 0010 1100  44 • E.g., 1010 1100  -44 • Hard to do arithmetic this way, so it is rarely used • Complement representation • One’s complement • Flip every bit • E.g., 1101 0011  -44 • Two’s complement • Flip every bit, then add 1 • E.g., 1101 0100  -44

  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. | & 0 1 0 1 0 0 0 0 0 1 1 1 0 1 1 1 53 15 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 1 1 1 1 0 0 1 1 1 Bitwise Operators: AND and OR • Bitwise OR (|) • Bitwise AND (&) • Mod on the cheap! • E.g., h = 53 & 15;

  41. 53 0 0 1 1 0 1 0 0 53<<2 1 1 0 1 0 0 0 0 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! 53 0 0 1 1 0 1 0 0 53>>2 0 0 0 0 1 1 0 1

  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., 0110 0111) • Do an operation that can be inverted • E.g., XOR each character with the 8-bit number 0100 0101 0010 0010 ^ 0110 0111 ^ 0110 0111 0010 0010 0100 0101

  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) • Two’s 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

More Related