Binary Numbers.

Presentation on theme: "Binary Numbers."— Presentation transcript:

Binary Numbers

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.

The Decimal Number System - base 10
The decimal number system is a positional number system with a base 10. Example: 1011 10112 = = 1 x x x x 20 = 1110 1000 000 10 1 1 x 23 0x22 1 x 21 1x 20

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.

The Binary Number System – base 2
The decimal number system is a positional number system with a base 10. Example: 5623 5623 = = 5 x x x x 100 5000 600 20 3 5 x 103 6 x102 2 x 101 3 x 100

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

Base 10 and Base 2 Base 10 Base 2 Each digit represents a power of 10
= 5 x x x x x 100 Base 2 Each bit represents a power of 2 101012= 1 x x x x x 20 = 2110

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:

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

Converting from Binary to Decimal (con’t)
Practice conversions: Binary Decimal

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 = and R = 0 21/2 = and R = 1 10/2 = and R = 0 5/2 = 2 and R = 1 2/2 = 1 and R = 0 1/2 = 0 and R = 1 =

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

Converting From Decimal to Binary (con’t)
Practice conversions: Decimal Binary

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

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

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

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

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

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

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.

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

Example = =

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

Binary subtraction

Binary subtraction (Cont)

Binary subtraction (Cont)

Exercise =? = ?

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

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

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

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

AND A Logic Gate: A*B B Truth Table: A B A*B 1

OR A Logic Gate: A+B B Truth Table: A B A+B 1

NOT Logic Gate: (also called an inverter) A A’ or A Truth Table: a A 1

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

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. A B AB 1 NAND A B AB 1 NOR

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. A B AB 1 XOR A B A B 1 XNOR

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

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

Half Adder (1-bit) A B A B S(um) C(arry) 1 S Half Adder C

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

Full Adder A B Cin A B S(um) Cout 1 S Full Adder Carry In (Cin) Cout

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

Cin Cout S S3 A3 B3 Carry Full Adder A B Cin Cout S S2 A2 B2 Full Adder A B Cin Cout S S1 A1 B1 A0 B0 Full Adder A B Cin Cout S A B Cin Cout S H.A. Full Adder S0 A B S C Half Adder

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.

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.

Binary Decimal Hexadecimal Binary Decimal Hexadecimal A B C D E F

Example of a hexadecimal number and the values of the positions: 3 C B

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

Summary Convert binary to decimal Decimal to binary Binary operation