Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Numbers 1. The Decimal Number System (cont) 2 The decimal number system is also known as base 10. The values of the positions are calculated by.

Similar presentations


Presentation on theme: "Binary Numbers 1. The Decimal Number System (cont) 2 The decimal number system is also known as base 10. The values of the positions are calculated by."— Presentation transcript:

1 Binary Numbers 1

2 The Decimal Number System (cont) 2 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.

3 The Decimal Number System - base 10 3 The decimal number system is a positional number system with a base 10. Example: = = 1 x x x x 2 0 = x 2 3 0x2 2 1 x 2 1 1x 2 0

4 The Binary Number System 4 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.

5 The Binary Number System – base 2 5 The decimal number system is a positional number system with a base 10. Example: = = 5 x x x x x x x x 10 0

6 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 – … 6

7 Base 10 and Base 2 Base 10 – Each digit represents a power of 10 – = 5 x x x x x 10 0 Base 2 – Each bit represents a power of 2 – = 1 x x x x x 2 0 =

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

9 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

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

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

12 Example We repeatedly divide the decimal number by 2 and keep remainders – 12/2 = 6 and R = 0 – 6/2 = 3and R = 0 – 3/2 = 1and R = 1 – 1/2 = 0and R = 1 The binary number representing 12 is 1100

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

14 Exercises Find the binary the decimal number represented by the following binary sequences: – – Represent the number 135 in base 2.

15 15 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

16 16 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

17 17 Fractional Numbers (cont.) Solution : (0.625) 10 = (0.101) 2 Exercise 2: Convert (0.6) 10 to its binary form 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.6 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

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

19 19 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 – =

20 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. 20

21 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

22 Example = =

23 23 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 – =

24 Binary subtraction

25 Binary subtraction (Cont)

26

27 Exercise =? = ?

28 Spring 2007, Jan. 17ELEC 2200 (Agrawal)28 Binary Multiplication two = 8 ten multiplicand two = 9 ten multiplier ____________ partial products ____________ two = 72 ten

29 Spring 2007, Jan. 17ELEC 2200 (Agrawal)29 Binary Division 1 3 Quotient 1 1 / 1 4 7Divisor / Dividend Partial remainder Remainder /

30 30 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

31 Boolean Algebra to Logic Gates Logic circuits are built from components called logic gates. The logic gates correspond to Boolean operations +, *,. Binary operations have two inputs, unary has one OR + AND * NOT

32 AND A B Logic Gate: ABA*B Truth Table: A*B

33 A B Logic Gate: ABA+B Truth Table: A+B OR

34 NOT ALogic Gate: (also called an inverter) aA Truth Table: A or A

35 n -input Gates Because + and * are binary operations, they can be cascaded together to OR or AND multiple inputs. A B C A B C A+B+C A B A B C ABC

36 NAND and NOR Gates NAND and NOR gates can greatly simplify circuit diagrams. As we will see, can you use these gates wherever you could use AND, OR, and NOT. NAND NORAB A B AB

37 XOR and XNOR Gates XOR is used to choose between two mutually exclusive inputs. Unlike OR, XOR is true only when one input or the other is true, not both. XOR XNOR AB A B AB

38 Binary Sums and Carries abSumabCarry XORAND

39 Spring 2007, Jan. 17ELEC 2200 (Agrawal)39 Design Hardware Bit by Bit Adding two bits: abhalf_sumcarry_out Half-adder circuit a b half_sum carry_out XOR AND

40 Half Adder (1-bit) ABS(um)C(arry) Half Adder AB S C

41 Half Adder (1-bit) ABS(um)C(arry) A B Sum Carry

42 Full Adder CinABS(um)Cout Full Adder AB S Cout Carry In (Cin)

43 Full Adder A B Cin Cout S H.A.

44 Full Adder Cout S Half Adder S C A B Half Adder S C A B B A Cin

45 4-bit Ripple Adder using Full Adder Full Adder AB Cin Cout S S0 A0B0 Full Adder AB Cin Cout S S1 A1B1 Full Adder AB Cin Cout S S2 A2B2 Full Adder AB Cin Cout S S3 A3B3 Carry A B S C Half Adder A B Cin Cout S H.A. Full Adder

46 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.

47 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. 47

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

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

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

51 Summary Convert binary to decimal Decimal to binary Binary operation Logic gates Use of logic gates to perform binary operations – Half adder – Full adder The need of Hexadecimal Hexadecimal

52 Next lecture (Data representation) Put this all together – negative and position integer representation – unsigned – Signed – Excess – Tows complement Floating point representation – Single and double precision Character, colour and sound representation


Download ppt "Binary Numbers 1. The Decimal Number System (cont) 2 The decimal number system is also known as base 10. The values of the positions are calculated by."

Similar presentations


Ads by Google