Addition : 101 +111 _________________ Binary Numbers (contd)

Post on 21-Dec-2015

241 views 6 download

Transcript of Addition : 101 +111 _________________ Binary Numbers (contd)

• Addition :

1 0 1

+ 1 1 1

_________________

Binary Numbers (contd)

• Addition – Our “basic” addition table is really easy now:

0 + 0 = 0

0 + 1 = 1

1 + 0 = 1

1 + 1 = 10

• What about 1 + 1 + 1?

• 1 + 1 + 1 = 11

Binary Numbers (contd)

• Addition – just like usual:

1 0 1

+ 1 1 1

_______________

0 + 0 = 0

0 + 1 = 1

1 + 0 = 1

1 + 1 = 10

1 + 1 + 1 = 11

Binary Numbers (contd)

• Addition – just like usual:

1

1 0 1

+ 1 1 1

_______________

0

Binary Numbers (contd)

0 + 0 = 0

0 + 1 = 1

1 + 0 = 1

1 + 1 = 10

1 + 1 + 1 = 11

• Addition – just like usual:

1 1

1 0 1

+ 1 1 1

_______________

0 0

Binary Numbers (contd)

0 + 0 = 0

0 + 1 = 1

1 + 0 = 1

1 + 1 = 10

1 + 1 + 1 = 11

• Addition – just like usual:

1 1

1 0 1

+ 1 1 1

_______________

1 1 0 0

Binary Numbers (contd)

0 + 0 = 0

0 + 1 = 1

1 + 0 = 1

1 + 1 = 10

1 + 1 + 1 = 11

• Can also do:

• Subtraction, Multiplication, etc

• Negative Numbers

• Fractions

• Great, but where do Logic Circuits fit in?

Binary Numbers (contd)

• And you thought we were done with truth tables

X Y Z

0 0 0

0 1 1

1 0 1

1 1 10

Problem:Output can

be 2 bitssometimes

Binary Numbers (contd)

• For Addition on small numbers, can make a truth table:

• Addition Truth Table with Multiple Outputs:

X Y AB

0 0 0 0

0 1 0 1

1 0 0 1

1 1 1 0

Binary Numbers (contd)

• But we know how to deal with multiple outputs:

• Now we can convert into 2 circuits!

X Y A

0 0 0

0 1 0

1 0 0

1 1 1

X Y B

0 0 0

0 1 1

1 0 1

1 1 0

Binary Numbers (contd)

X Y B

0 0 0

0 1 1

1 0 1

1 1 0

ORB

ANDX

Y

ANDX

Y

AAND

X

Y

X Y A

0 0 0

0 1 0

1 0 0

1 1 1

Binary Numbers (contd)

X Y B

0 0 0

0 1 1

1 0 1

1 1 0

ORB

ANDX

Y

ANDX

Y

This is often called the eXclusive OR (XOR) circuitWe say that B is the exclusive OR of X and Y.

We write B = X Y.

Binary Numbers (contd)

• Bob will go to the party if Ed goes OR Dan goes.

• Dan will go if Xena does NOT go AND Yanni goes.

• Ed will go if Xena goes AND Yanni does NOT go.

• Turns out this is addition without the carry

• Bob goes if the XOR of Xena and Yanni go.

A Logic Puzzle

• We can represent information by bits.(So we interpret bits to mean things like numbers.)

• Then we re-interpret those bits as Logical True/False values and construct truth tables for operations on information (e.g. addition)

• Finally we use Universal Method to construct circuits for these operations

A Simple Breakthrough

Intermission

• Questions??

• How do we actually build a circuit for addition?

X1 X2 Y1 Y2 Z1 Z2 C

0 0 0 0 0 0 0

0 0 0 1 0 1 0

0 0 1 0 1 0 0

0 0 1 1 1 1 0

0 1 0 0 0 1 0

0 1 0 1 1 0 0

0 1 1 0 1 1 0

0 1 1 1 0 0 1

1 0 0 0 1 0 0

1 0 0 1 1 1 0

1 0 1 0 0 0 1

1 0 1 1 0 1 1

1 1 0 0 1 1 0

1 1 0 1 0 0 1

1 1 1 0 0 1 1

1 1 1 1 1 0 1

X1 X2 Y1 Y2 ========= C Z1 Z2

C is the carry bit

Addition

X1 X2 Y1 Y2 Z1 Z2 C

0 0 0 0 0 0 0

0 0 0 1 0 1 0

0 0 1 0 1 0 0

0 0 1 1 1 1 0

0 1 0 0 0 1 0

0 1 0 1 1 0 0

0 1 1 0 1 1 0

0 1 1 1 0 0 1

1 0 0 0 1 0 0

1 0 0 1 1 1 0

1 0 1 0 0 0 1

1 0 1 1 0 1 1

1 1 0 0 1 1 0

1 1 0 1 0 0 1

1 1 1 0 0 1 1

1 1 1 1 1 0 1

X1 X2 Y1 Y2 ========= C Z1 Z2

C is the carry bit

Could have 3 circuits(for Z1, Z2, C)16 inputs each

2 w/ 8 ANDs, 1 OR1 w/ 6 ANDs, 1 OR

18+7 = 25 gates

Addition

X1 X2 Y1 Y2 ========= C Z1 Z2

Rewrite as

X2 X1

Y2 Y1

==== C2

C2 Z2 ========

C Z1

Simplifying the Addition Circuit

X2

Y2

====

C2 Z2

X1

Y1

C2

====

C Z1

2 circuits (C2, Z2)

4 gates

X1 Y1 C2 C Z1

0 0 0 0 0

0 0 1 0 1

0 1 0 0 1

0 1 1 1 0

1 0 0 0 1

1 0 1 1 0

1 1 0 1 0

1 1 1 1 1

2 circuits (C, Z1)

10 gates

Addition

Z2

C2

X1

Y1

CircuitFor Z1 and carry

CircuitFor Z2 and carry

Z1

Carry

X2

Y2

Addition

• Can be cascaded for adding longer numbers• We can use 2 building blocks to add numbers of any length.

Adding Binary Numbers of Any Length

• Actually we need only 1 building block• lowest-bit circuit also has 3 inputs, but carry in is always 0

ith bit of X

ith bit of Y

carry bit in

ith bit of Z

carry bit out

Abstraction in action -- This is a piece of a carry-ripple adder

Adding Binary Numbers of Any Length

• Write the numbers as– X4 X3 X2 X1 X0

– Y4 Y3 Y2 Y1 Y0

• Represent the sum as– C Z4 Z3 Z2 Z1 Z0

Adding Binary Numbers of Any Length

• Write the numbers as

– X4 X3 X2 X1 X0

– Y4 Y3 Y2 Y1 Y0

• Represent the sum as

– C Z4 Z3 Z2 Z1 Z0

• Start with X0, Y0, 0 in, Z0 and C0 out

• Then X1,Y1,C0 in and Z1 and C1 out.

• And so on

Adding Binary Numbers of Any Length

At the ith stage

ith bit of X

ith bit of Y

carry bit in

ith bit of Z

carry bit out

Abstraction in action -- This is a piece of a carry-ripple adder

Addition

Circuit

Adding Binary Numbers of Any Length

At each stage, take 2 addends and a bit telling whether there is a carry in, and produce a sum bit and a bit telling if there is a carry out

We can build a ‘black box’ to do this (abstraction)

addend

addend

carry bit in

Sum bit

carry bit out

Carry-Ripple Adder

Z0

C0

Black BoxCircuit

X0

Y0

Z1

C1

Black BoxCircuit

X1

Y1

Z2

C2

Black BoxCircuit

X2

Y2

X2 X1 X0 Y2 Y1 Y0============C2 Z2 Z1 Z0

0

Fixed at 0

What’s inside the box?

Zi

Cout

Black BoxCircuit

Xi

Yi

Cin

What’s inside the box?

Zi

Cout

UniversalCircuit

Xi

Yi

Cin

Abstraction in Action: Use Truth Tables and the Universal Method

Carry ripple adders

• Useful for some applications• Too slow for other

– Delay while waiting for the carry to ripple

• One solution – add larger blocks– Details in the homework assignment

Arithmetic Logical Unit (ALU)

• This is the part of the CPU that does arithmetic, logical and comparison operations

• We’ve built a piece of the ALU• Using abstraction, we could build the other parts

– Multiplication/subtraction/division

– Comparison

• Then we would write a language to let the user talk to the ALU (programming)

Pause

• Questions??

• Back to representing information

Representing information

• How do we represent characters?– How many characters might we want to represent?

– What characters might we want to represent?

Representing information

• How do we represent characters?– How many characters might we want to represent?

– What characters might we want to represent?• A-Z 26

• A-Z and a-z 52

• All the keys on my keyboard 104

• Maybe use a power of 2? 128

• Maybe use an even power of 2? 256

• Maybe an even bigger power of 2? 65536

Representing characters

• ASCII is the American Standard Code for Information Interchange. It is a 7-bit code.

• Many 8-bit codes contain ASCII as the lower half of their range of values

• The ASCII standard was published by the United States of America Standards Institute (USASI) in 1968

• There are other character coding standards: EBCDIC, and more recently Unicode

Unicode

• Universal Character Set (UCS) contains all characters of all other character set standards. It also guarantees round-trip compatibility, i.e., conversion tables can be built such that no information is lost when a string is converted from any other encoding to UCS and back.

• UCS contains the characters required to represent almost all known languages. In addition to the many languages which use extensions of the Latin script, this also includes the following scripts and

languages: Greek, Cyrillic, Hebrew, Arabic, Armenian, Gregorian, Japanese, Chinese, Hiragana, Katakana, Korean, Hangul, Devangari, Bengali, Gurmukhi, Gujarati, Oriya, Tamil, Telugu, Kannada, Malayam, Thai, Lao, Bopomofo, and a number of others. Work is going on to include further scripts like Tibetian, Khmer, Runic, Ethiopian, Hieroglyphics, various Indo-European languages, and many others.

• It uses 31 bits (32768 possible characters)

What do we do in practice?

• Problems– Bits represent too little information – too many are needed– Decimal numbers don’t translate well into hardware

• So …– Group into blocks of 4 and 8 bits

• 8 bits = 256 characters, holds ASCII• 8 bits make 1 byte – things are organized into bytes• 4 bits make 1 nibble• Hexadecimal numbers use 1 nibble per slot or ‘hex digit’

– Digits go from 0-9 and then A-F, for 16 possible values

0

1

2

3

4

5

6

7

0 0 0 0

0 0 0 1

0 0 1 0

0 0 1 1

0 1 0 0

0 1 0 1

0 1 1 0

0 1 1 1

8

9

A

B

C

D

E

F

1 0 0 0

1 0 0 1

1 0 1 0

1 0 1 1

1 1 0 0

1 1 0 1

1 1 1 0

1 1 1 1

Shorthand: Hexadecimal

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Dec Hex Binary Dec Hex Binary

Hexadecimal

• We can add hex numbers– 1+1=2, 2+2=4, 4+4=8, 4+8=C, 2+8=A, …

• We can combine 2 hexadecimal numbers to make a byte (2C)

• Hex is easier to read than binary– Decimal 22 is 16 in hex (1*161 + 6*160) rather than 10110 in binary

• In ASCII – Hex 41 through 5A represent A to Z

– Hex 61 through 7A represent a to z

Summary

• Logic gates and the Universal Method

• Binary representation of numbers and arithmetic

• Universal method can lead to very big circuits

• Solve by reducing to smaller circuits

• Demonstrated with cascading smaller circuits– Carry-ripple adder

• Represented characters– Hexadecimal

– Bytes, nibbles

Now that we can represent it, how do we store it??

Next Time: Memory