Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Numbers.

Similar presentations


Presentation on theme: "Binary Numbers."— 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 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 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: X 100 = 1 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:

14 Converting from Binary to Decimal
X 20 = 1 X 21 = 0 1 X 22 = 4 20 = X 23 = 8 21 = X 24 = 0 22 = X 25 = 0 23 = X 26 = 64 24 = 25 = 32 26 = 64

15 Converting from Binary to Decimal (con’t)
Practice conversions: Binary Decimal 11101 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 42/2 = R = 0 21/2 = R = 1 10/2 = R = 0 5/2 = 2 R = 1 2/2 = 1 R = 0 1/2 = 0 R = 1 =

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

18 Working 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.

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)
Binary Decimal Hexadecimal Binary Decimal Hexadecimal A B C D E F

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

22 Example of Equivalent Numbers
Binary: Decimal: 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 x x x 100 Base 2 Each bit represents a power of 2 10110 = 1 x x x x x 20 = 22 Divide repeatedly by 2 and keep remainders 12/2 = R = 0 6/2 = R = 0 3/2 = R = 1 1/2 = R = 1 Result =

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 = 0 1000 = 8 0001 = 1 1001 = 9 0010 = 2 1010 = A 0011 = 3 1011 = B 0100 = 4 1100 = C 0101 = 5 1101 = D 0110 = 6 1110 = E 0111 = 7 1111 = F Thus the 16-bit binary number 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  1  15  16  33  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 Sum Carry 1 9 8 Sum Carry 4 6 1 2 1 1 1 1

30 Binary Sums and Carries
a b Sum a b Carry XOR AND 69 103 172

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

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

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 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 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 = ( …)2

34 Fractional Numbers (cont.)
Exercise 3: Convert (0.8125)10 to its binary form Solution: x 2 =  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 = ( …)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 One’s and Two’s Complement
One’s complement: flip every bit E.g., b (i.e., 69 in base 10) One’s complement is That’s simply Subtracting from is easy (no carry needed!) Two’s complement Add 1 to the one’s complement E.g., (255 – 69) + 1  b one’s complement

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: One’s complement of 69: Two’s complement of 69: Add to the number 172: The sum comes to: Equals: 103 in base 10

38 Signed Integers Sign-magnitude representation
Use one bit to store the sign Zero for positive number One for negative number Examples E.g.,  44 E.g.,  -44 Hard to do arithmetic this way, so it is rarely used Complement representation One’s complement Flip every bit E.g.,  -44 Two’s complement Flip every bit, then add 1 E.g.,  -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 Bitwise Operators: AND and OR
Bitwise AND (&) Mod on the cheap! E.g., h = 53 & 15; Bitwise OR (|) & 1 | 1 1 53 1 15 1 5

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! 1 53 53<<2 53 1 1 1 53>>2 1 1 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., ) Do an operation that can be inverted E.g., XOR each character with the 8-bit number ^ ^

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


Download ppt "Binary Numbers."

Similar presentations


Ads by Google