Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number...

37
30000BC Palaeolithic peoples in central Europe and France record numbers on bones. 5000BC A decimal number system is in use in Egypt. 4000BC Babylonian and Egyptian calendars in use. 3400BC The first symbols for numbers, simple straight lines, are used in Egypt. 3000BC The abacus is developed in the Middle East and in areas around the Mediterranean. A somewhat different type of abacus is used in China. 3000BC Hieroglyphic numerals in use in Egypt. 3000BC Babylonians begin to use a sexagesimal number system for recording financial transactions. It is a place-value system without a zero place value. 2000BC Harappans adopt a uniform decimal system of weights and measures. 1950BC Babylonians solve quadratic equations . 1900BC The Moscow papyrus is written. It gives details of Egyptian geometry. 1850BC Babylonians know Pythagoras 's Theorem. 1800BC Babylonians use multiplication tables. 1750BC The Babylonians solve linear and quadratic algebraic equations, compile tables of square and cube roots. They use Pythagoras 's theorem and use mathematics to extend knowledge of astronomy. 1700BC The Rhind papyrus (sometimes called the Ahmes papyrus) is written. It shows that Egyptian mathematics has developed many techniques to solve problems. Multiplication is based on repeated doubling, and division uses successive halving. 1360BC A decimal number system with no zero starts to be used in China. 1000BC Chinese use counting boards for calculation. 540BC Counting rods used in China. 500BC The Babylonian sexagesimal number system is used to record and predict the positions of the Sun, Moon and planets. Egyptian Numerals

Transcript of Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number...

Page 1: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

• 30000BCPalaeolithic peoples in central Europe and France record numbers on bones.

• 5000BCA decimal number system is in use in Egypt.

• 4000BCBabylonian and Egyptian calendars in use.

• 3400BCThe first symbols for numbers, simple straight lines, are used in Egypt.

• 3000BCThe abacus is developed in the Middle East and in areas around the Mediterranean. A somewhat different type of abacus is used in China.

• 3000BCHieroglyphic numerals in use in Egypt.

• 3000BCBabylonians begin to use a sexagesimal number system for recording financial transactions. It is a place-value system without a zero place value.

• 2000BCHarappans adopt a uniform decimal system of weights and measures.

• 1950BCBabylonians solve quadratic equations.

• 1900BCThe Moscow papyrus is written. It gives details of Egyptian geometry.

• 1850BCBabylonians know Pythagoras's Theorem.

• 1800BCBabylonians use multiplication tables.

• 1750BCThe Babylonians solve linear and quadratic algebraic equations, compile tables of square and cube roots. They use Pythagoras's theorem and use mathematics to extend knowledge of astronomy.

• 1700BCThe Rhind papyrus (sometimes called the Ahmes papyrus) is written. It shows that Egyptian mathematics has developed many techniques tosolve problems. Multiplication is based on repeated doubling, and division uses successive halving.

• 1360BCA decimal number system with no zero starts to be used in China.

• 1000BCChinese use counting boards for calculation.

• 540BCCounting rods used in China.

• 500BCThe Babylonian sexagesimal number system is used to record and predict the positions of the Sun, Moon and planets.

Egyptian Numerals Egyptian number system is additive.

Page 2: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Mesopotamia CivilizationAbove: Babylonian sexagesimal (base 60) number. It is the first positional number system.Left: Oldest cuneiform writing by Sumerian.Babylonian numerals

Page 3: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

4359

Chinese numerals

Indian numerals

Greek number systems

Page 4: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Roman NumeralsI 1II 2III 3IV 4V 5 VI 6VII 7VIII 8IX 9X 10 L 50C 100D 500M 1000MMMDCCCLXXVIII 3878Mayan mathematics

[8;14;3;1;12] represents 12 + 1 x 20 + 3 x 18 x 20 + 14 x 18 x 202 + 8 x 18 x 203 = 1253912.

250 AD to 900 AD, this period was built on top of a civilization which had lived in the region from about 2000 BC.

The numerals from al-Sizji'streatise of 969

Page 5: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Abaci

Boethius (Hindu-Arabic) vs Pythagoras (counting board) Chinese AbacusLogarithm and Slide RuleIf ay=x, then y = logaxlog (u v) = log (u) + log(v) John Napier of Scotland developed the concept of logarithm around AD 1600. Slide rule based on the property of logarithm was invented in the late 1700s. Decimal Numbers: Base 10

Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Example:3271 =

(3x103) + (2x102) + (7x101) + (1x100)

Page 6: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Numbers: positional notation

• Number Base B ⇒ B symbols per digit:– Base 10 (Decimal): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Base 2 (Binary): 0, 1

• Number representation: – d31d30 ... d1d0 is a 32 digit number– value = d31 ×××× B31 + d30 ×××× B30 + ... + d1 ×××× B1 + d0 ×××× B0

• Binary: 0,1 (In binary digits called “bits”)– 0b11010 = 1××××24 + 1××××23 + 0××××22 + 1××××21 + 0××××20

= 16 + 8 + 2= 26

– Here 5 digit binary # turns into a 2 digit decimal #– Can we find a base that converts to binary easily?

#s often written0b…

Hexadecimal Numbers: Base 16

• Hexadecimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F– Normal digits + 6 more from the alphabet– In C, written as 0x… (e.g., 0xFAB5)

• Conversion: Binary ⇔Hex– 1 hex digit represents 16 decimal values– 4 binary digits represent 16 decimal values⇒1 hex digit replaces 4 binary digits

• One hex digit is a “ nibble ”. Two is a “ byte ”• Example:

– 1010 1100 0011 (binary) = 0x_____ ?

Decimal vs. Hexadecimal vs. Binary

Examples:1010 1100 0011 (binary) = 0xAC310111 (binary) = 0001 0111 (binary) = 0x170x3F9 = 11 1111 1001 (binary)How do we convert between hex and Decimal?

00 0 000001 1 000102 2 001003 3 001104 4 010005 5 010106 6 011007 7 011108 8 100009 9 100110 A 101011 B 101112 C 110013 D 110114 E 111015 F 1111

Examples:1010 1100 0011 (binary) = 0xAC310111 (binary) = 0001 0111 (binary) = 0x170x3F9 = 11 1111 1001 (binary)How do we convert between hex and Decimal?

Page 7: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

What to do with representations of numbers?

• Just what we do with numbers!– Add them– Subtract them– Multiply them– Divide them– Compare them

• Example: 10 + 7 = 17– …so simple to add in binary that we can

build circuits to do it!– subtraction just as you would in decimal– Comparison: How do you tell if X > Y ?

1 0 1 0

+ 0 1 1 1

-------------------------

1 0 0 0 1

11

Which base do we use?• Decimal: great for humans, especially when doing

arithmetic• Hex: if human looking at long strings of binary

numbers, its much easier to convert to hex and look 4 bits/symbol

– Terrible for arithmetic on paper• Binary: what computers use;

you will learn how computers do +, -, *, /– To a computer, numbers always binary– Regardless of how number is written:

32ten == 3210 == 0x20 == 1000002 == 0b100000– Use subscripts “ten”, “hex”, “two” in book, slides whe n might

be confusing

BIG IDEA: Bits can represent anything!!

• Characters?– 26 letters ⇒ 5 bits (2 5 = 32)– upper/lower case + punctuation ⇒ 7 bits (in 8) (“ASCII”)– standard code to cover all the world’s languages ⇒ 8,16,32 bits

(“Unicode”)www.unicode.com

• Logical values?– 0 ⇒ False, 1 ⇒ True

• colors ? Ex:• locations / addresses? commands?• MEMORIZE: N bits ⇔⇔⇔⇔ at most 2 N things

Red (00) Green (01) Blue (11)

Page 8: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

How to Represent Negative Numbers?

• So far, unsigned numbers• Obvious solution: define leftmost bit to be sign!

– 0 ⇒ +, 1 ⇒ -– Rest of bits can be numerical value of number

• Representation called sign and magnitude• MIPS uses 32-bit integers. +1 ten would be:

0000 0000 0000 0000 0000 0000 0000 0001• And –1 ten in sign and magnitude would be:

1000 0000 0000 0000 0000 0000 0000 0001

Shortcomings of sign and magnitude?

• Arithmetic circuit complicated– Special steps depending whether signs are the same or not

• Also, two zeros– 0x00000000 = +0ten

– 0x80000000 = -0ten

– What would two 0s mean for programming?

• Therefore sign and magnitude abandoned

Another try: complement the bits

• Example: 7 10 = 001112 -710 = 110002

• Called One’s Complement• Note: positive numbers have leading 0s, negative

numbers have leadings 1s.

00000 00001 01111...

111111111010000 ...

• What is -00000 ? Answer: 11111• How many positive numbers in N bits?• How many negative ones?

Page 9: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Shortcomings of One’s complement?

• Arithmetic still a somewhat complicated.• Still two zeros

– 0x00000000 = +0ten

– 0xFFFFFFFF = -0ten

• Although used for awhile on some computer products, one’s complement was eventually abandoned because another solution was better.

Standard Negative Number Representation

• What is result for unsigned numbers if tried to sub tract large number from a small one?– Would try to borrow from string of leading 0s,

so result would have a string of leading 1s» 3 - 4 ⇒ 00…0011 - 00…0100 = 11…1111

– With no obvious better alternative, pick representa tion that made the hardware simple

– As with sign and magnitude, leading 0s ⇒ positive, leading 1s ⇒ negative

» 000000...xxx is ≥ 0, 111111...xxx is < 0» except 1…1111 is -1, not -0 (as in sign & mag.)

• This representation is Two’s Complement

Sign and Magnitude

0000

0111

0011

1011

11111110

1101

1100

1010

1001

10000110

0101

0100

0010

0001

+0+1

+2

+3

+4

+5

+6

+7-0

-1

-2

-3

-4

-5

-6

-7

0 100 = + 4 1 100 = - 4

+

-High order bit is sign: 0 = positive (or zero), 1 = negativeRemaining low order bits is the magnitude: 0 (000) thru 7 (111)Number range for n bits = +/- 2n-1 - 1Representations for 0?Operations: =, <, >, +, - ??? Example: N = 4

Page 10: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Ones Complement (algebraically)N is positive number, then N is its negative 1's complementN = (2n - 1) - NExample: 1's complement of 7 2 = 10000-1 = 000011111-7 = 01111000-7 in 1's comp.Bit manipulation:simply complement each of the bits0111 -> 1000 4Ones Complement on the number wheelSubtraction implemented by addition & 1's complementSign is easy to determineClosure under negation. If A can be represented, so can -AStill two representations of 0! If A = B then is A – B == 0 ?Addition is almost clockwise advance, like unsigned

0000

0111

0011

1011

11111110

1101

1100

1010

1001

10000110

0101

0100

0010

0001

+0+1

+2

+3

+4

+5

+6

+7-7-6

-5

-4

-3

-2

-1

-0

0 100 = + 4 1 011 = - 4

+

-

Twos Complement number wheel

0000

0111

0011

1011

11111110

1101

1100

1010

1001

10000110

0101

0100

0010

0001

+0+1

+2

+3

+4

+5

+6

+7-8-7

-6

-5

-4

-3

-2

-1

0 100 = + 4 1 100 = - 4

+

-Easy to determine sign (0?)Only one representation for 0Addition and subtraction just as in unsigned caseSimple comparison: A < B iff A – B < 0One more negative number than positive number- one number has no additive inverselike 1's compexcept shiftedone positionclockwise

Page 11: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Twos Complement (algebraically)N* = 2n - NExample: Twos complement of 7 2 = 100007 = 01111001 = repr. of -7Example: Twos complement of -7 42 = 10000-7 = 10010111 = repr. of 74subsubBit manipulation:Twos complement: take bitwise complement and add one0111 -> 1000 + 1 -> 1001 (representation of -7)1001 -> 0110 + 1 -> 0111 (representation of 7)How is addition performed in each number system?

• Operands may be positive or negative

Sign Magnitude Addition4+ 37 010000110111 -4+ (-3)-7 110010111111result sign bit is thesame as the operands'sign 4- 31 010010110001 -4+ 3-1 110000111001Operand have same sign: unsigned addition of magnit udes

Operands have different signs:

subtract smaller from larger and keep sign of the l arger

Page 12: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Ones complement addition4+ 37 010000110111 -4+ (-3)-7 1011110010111110004- 31 010011001000010001 -4+ 3-1 101100111110End around carryEnd around carryPerform unsigned addition, then add in the end-arou nd carry

When carry occurs

0000

0111

0011

1011

11111110

1101

1100

1010

1001

10000110

0101

0100

0010

0001

+0+1

+2

+3

+4

+5

+6

+7-7

-6

-5

-4

-3

-2

-1

-0

0 100 = + 4 1 011 = - 4

+

-

M – N where M > N

-M - N

Why does end-around carry work?End-around carry work is equivalent to subtracting 2n and adding 1M - N = M + N = M + (2 - 1 - N) = (M - N) + 2 - 1n n (when M > N)-M + (-N) = M + N = (2 - M - 1) + (2 - N - 1)= 2 + [2 - 1 - (M + N)] - 1n nn n M + N < 2 n-1after end around carry:= 2 - 1 - (M + N)nthis is the correct form for representing -(M + N) in 1's comp!N = (2n - 1) - NRecall:

Page 13: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Twos Complement Addition4+ 37 010000110111 -4+ (-3)-7 11001101110014- 31 0100110110001 -4+ 3-1 110000111111Simpler addition scheme makes twos complement the most commonchoice for integer number systems within digital systemsPerform unsigned addition and

Discard the carry out.

Overflow?

Twos Complement number wheel

0000

0111

0011

1011

11111110

1101

1100

1010

1001

10000110

0101

0100

0010

0001

+0+1

+2

+3

+4

+5

+6

+7-8

-7

-6

-5

-4

-3

-2-1

0 100 = + 4 1 100 = - 4

+

-

-M + -N where N + M ≤ 2n-1

-M + N when N > M

2s Comp: ignore the carry out-M + N when N > M:M* + N = (2 - M) + N = 2 + (N - M)n nIgnoring carry-out is just like subtracting 2n-M + -N where N + M ≤ 2n-1-M + (-N) = M* + N* = (2 - M) + (2 - N)= 2 - (M + N) + 2n nAfter ignoring the carry, this is just the right twos compl.representation for -(M + N)!n n

Page 14: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

2s Complement OverflowAdd two positive numbers to get a negative numberor two negative numbers to get a positive number5 + 3 = -8! -7 - 2 = +7!

0000

0001

0010

0011

1000

0101

0110

0100

1001

1010

1011

1100

1101

0111

1110

1111

+0

+1

+2

+3

+4

+5

+6

+7-8

-7

-6

-5

-4

-3

-2

-1

0000

0001

0010

0011

1000

0101

0110

0100

1001

1010

1011

1100

1101

0111

1110

1111

+0

+1

+2

+3

+4

+5

+6

+7-8

-7

-6

-5

-4

-3

-2

-1

How can you tell an overflow occurred ?

2s comp. Overflow Detection53-8 0 1 1 10 1 0 10 0 1 11 0 0 0 -7-27 1 0 0 01 0 0 11 1 0 01 0 1 1 1527 0 0 0 00 1 0 10 0 1 00 1 1 1 -3-5-8 1 1 1 11 1 0 11 0 1 11 1 0 0 0Overflow OverflowNo overflow No overflowOverflow occurs when carry in to sign does not equal carry outTwo’s Complement for N=32

0000 ... 0000 0000 0000 0000two = 0ten0000 ... 0000 0000 0000 0001two = 1ten0000 ... 0000 0000 0000 0010two = 2ten. . .0111 ... 1111 1111 1111 1101two = 2,147,483,645ten0111 ... 1111 1111 1111 1110two = 2,147,483,646ten0111 ... 1111 1111 1111 1111two = 2,147,483,647ten1000 ... 0000 0000 0000 0000two = –2,147,483,648ten1000 ... 0000 0000 0000 0001two = –2,147,483,647ten1000 ... 0000 0000 0000 0010two = –2,147,483,646ten. . . 1111 ... 1111 1111 1111 1101two = –3ten1111 ... 1111 1111 1111 1110two = –2ten1111 ... 1111 1111 1111 1111two = –1ten

• One zero; 1st bit called sign bit• 1 “extra” negative:no positive 2,147,483,648ten

Page 15: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Two’s Complement Formula

• Can represent positive and negative numbers in terms of the bit value times a power of 2:

d31 x -(231) + d30 x 230 + ... + d2 x 22 + d1 x 21 + d0 x 20

• Example: 1101two

= 1x-(23) + 1x22 + 0x21 + 1x20

= -23 + 22 + 0 + 20

= -8 + 4 + 0 + 1 = -8 + 5= -3ten

Two’s Complement shortcut: Negation

• Change every 0 to 1 and 1 to 0 (invert or complemen t), then add 1 to the result

• Proof: Sum of number and its (one’s) complement mus t be 111...111two

However, 111...111 two= -1tenLet x’ ⇒ one’s complement representation of xThen x + x’ = -1 ⇒ x + x’ + 1 = 0 ⇒ x’ + 1 = -x

• Example: -3 to +3 to -3

• x : 1111 1111 1111 1111 1111 1111 1111 1101two

• x’: 0000 0000 0000 0000 0000 0000 0000 0010two

• +1: 0000 0000 0000 0000 0000 0000 0000 0011two

• ()’: 1111 1111 1111 1111 1111 1111 1111 1100two

• +1: 1111 1111 1111 1111 1111 1111 1111 1101two

You should be able to do this in your head…

Two’s comp. shortcut: Sign extension

• Convert 2’s complement number rep. using n bits to more than n bits

• Simply replicate the most significant bit (sign bit) of smaller to fill new bits–2’s comp. positive number has infinite 0s–2’s comp. negative number has infinite 1s–Binary representation hides leading bits; sign extension restores some of them–16-bit -4 ten to 32-bit:

1111 1111 1111 1100two

1111 1111 1111 1111 1111 1111 1111 1100two

Page 16: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

What if too big?• Binary bit patterns above are simply representatives of numbers.

Strictly speaking they are called “numerals”.• Numbers really have an ∞∞∞∞ number of digits

– with almost all being same (00…0 or 11…1) except for a few of the rightmost digits

– Just don’t normally show leading digits

• If result of add (or -, *, / ) cannot be represente d by these rightmost HW bits, overflow is said to have occurred.

00000 00001 00010 1111111110

unsigned

Kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta

• Confusing! Common usage of “kilobyte” means 1024 bytes, but the “correct” SI value is 1000 bytes

• Hard Disk manufacturers & Telecommunications are the only computing groups that use SI factors, so what is ad vertised as a 30 GB drive will actually only hold about 28 x 2 30 bytes, and a 1 Mbit/sconnection transfers 10 6 bps.

1024 = 1,000,000,000,000,000,000,000,000280 = 1,208,925,819,614,629,174,706,176YYotta

1021 = 1,000,000,000,000,000,000,000270 = 1,180,591,620,717,411,303,424ZZetta

1018 = 1,000,000,000,000,000,000260 = 1,152,921,504,606,846,976EExa

1015 = 1,000,000,000,000,000250 = 1,125,899,906,842,624PPeta

1012 = 1,000,000,000,000240 = 1,099,511,627,776TTera

109 = 1,000,000,000230 = 1,073,741,824GGiga

106 = 1,000,000220 = 1,048,576MMega

103 = 1,000210 = 1,024KKilo

SI sizeFactorAbbrName

physics.nist.gov/cuu/Units/binary.html

kibi, mebi, gibi, tebi, pebi, exbi, zebi, yobi

• International Electrotechnical Commission (IEC) in 1 999 introduced these to specify binary quantities.– Names come from shortened versions of the original SI prefixes (same

pronunciation) and bi is short for “binary”, but pronounced “bee” :-(– Now SI prefixes only have their base-10 meaning and never have a base-2

meaning.

280 = 1,208,925,819,614,629,174,706,176

270 = 1,180,591,620,717,411,303,424

260 = 1,152,921,504,606,846,976

250 = 1,125,899,906,842,624

240 = 1,099,511,627,776

230 = 1,073,741,824

220 = 1,048,576

210 = 1,024

Factor

Yiyobi

Zizebi

Eiexbi

Pipebi

Titebi

Gigibi

Mimebi

Kikibi

AbbrName

en.wikipedia.org/wiki/Binary_prefix

Page 17: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

• What is 2 34? How many bits addresses (I.e., what’s ceil log 2 = lg of) 2.5 TiB?

• Answer! 2 XY means…X=0 ⇒ ---X=1 ⇒ kibi ~10 3

X=2 ⇒ mebi ~10 6

X=3 ⇒ gibi ~10 9

X=4 ⇒ tebi ~10 12

X=5 ⇒ tebi ~10 15

X=6 ⇒ exbi ~10 18

X=7 ⇒ zebi ~1021

X=8 ⇒ yobi ~10 24

The way to remember #s

Y=0 ⇒ 1Y=1 ⇒ 2Y=2 ⇒ 4Y=3 ⇒ 8Y=4 ⇒ 16Y=5 ⇒ 32Y=6 ⇒ 64Y=7 ⇒ 128Y=8 ⇒ 256Y=9 ⇒ 512

MEMORIZE!

Comparing the signed number systems• Here are all the 4-bit numbers

in the different systems.• Positive numbers are the

same in all three representations.

• Signed magnitude and one’s complement have two ways of representing 0. This makes things more complicated.

• Two’s complement has asymmetric ranges; there is one more negative number than positive number. Here, you can represent -8 but not +8.

• However, two’s complement is preferred because it has only one 0, and its addition algorithm is the simplest.

Decimal S.M. 1’s comp. 2’s comp. 7 0111 0111 0111 6 0110 0110 0110 5 0101 0101 0101 4 0100 0100 0100 3 0011 0011 0011 2 0010 0010 0010 1 0001 0001 0001 0 0000 0000 0000 -0 1000 1111 — -1 1001 1110 1111 -2 1010 1101 1110 -3 1011 1100 1101 -4 1100 1011 1100 -5 1101 1010 1011 -6 1110 1001 1010 -7 1111 1000 1001 -8 — — 1000

And in Conclusion...• We represent “things” in computers as particular bi t patterns: N

bits ⇒ 2N

• Decimal for human calculations, binary for computer s, hex to write binary more easily

• 1’s complement - mostly abandoned

• 2’s complement universal in computing: cannot avoid, so learn

• Overflow: numbers ∞∞∞∞; computers finite, errors!

Page 18: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Numbers represented in memory

• Memory is a place to store bits

• A word is a fixed number of bits (eg, 32) at an address

• Addresses are naturally represented as unsigned numbers in C

101101100110

00000

11111 = 2k - 1

01110

Signed vs. Unsigned Variables

• Java just declares integers int– Uses two’s complement

• C has declaration int also– Declares variable as a signed integer– Uses two’s complement

• Also, C declaration unsigned int– Declares a unsigned integer– Treats 32-bit number as unsigned integer, so most s ignificant

bit is part of the number , not a sign bit

Binary Codes for Decimal Digits

There are over 8,000 ways that you can chose 10 elements from the 16 binary numbers of 4 bits. A few are useful:

Decimal 8,4,2,1 Excess3 8,4,-2,-1 0 0000 0011 0000 1 0001 0100 0111 2 0010 0101 0110 3 0011 0110 0101 4 0100 0111 0100 5 0101 1000 1011 6 0110 1001 1010 7 0111 1010 1001 8 1000 1011 1000 9 1001 1100 1111

Page 19: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Binary Coded Decimal (BCD)

Binary Coded Decimal or 8,4,2,1 Code.

This code is the simplest, most intuitive binary code for decimal digits and uses the same weights as a binary number, but only encodes the first ten values from 0 to 9. Examples: 1001 is 8 + 1 = 9

0011 is 2 + 1 = 3 0100 is 4 1010 is an illegal code.

Other Decimal Codes

The Excess-3 Code adds binary 0011 to the BCD code.

The BCD (8,4, 2, 1) Code, and the (8,4,-2,-1) Code are examples of weighted codes.

Each bit has a "weight" associated with it and you can compute the decimal value by adding the weights where a 1 exists in the code-word. Example: 1111 in (8,4,-2,-1) is 8 + 4 + (-2) + (-1) = 9

Warning: Conversion or Coding?

DO NOT mix up CONVERSION of a decimal number to a binary number with CODING a decimal number with a BINARY CODE. 1310 = 11012 (This is CONVERSION) 13 ⇔⇔⇔⇔ 00010011 (This is CODING)

Page 20: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Binary Addition: Half Adder

Ai 0 0 1 1

Bi 0 1 0 1

Sum 0 1 1 0

Carry 0 0 0 1

AiBi

0 1

0

1

0 1

1 0

Sum = Ai Bi + Ai Bi

= Ai + Bi

AiBi

0 1

0

1

0 0

10

Carry = Ai BiHalf-adder SchematicCarry

Sum A i

B i

But each bit position may have a carry in…

Full-Adder

A 0 0 0 0 1 1 1 1

B 0 0 1 1 0 0 1 1

CI 0 1 0 1 0 1 0 1

S 0 1 1 0 1 0 0 1

CO 0 0 0 1 0 1 1 1

A BCI

0

1

00 01 11 10

0

1

1

0

1

0

0

1

A BCI

0

1

00 01 11 10

0

0

0

1

0

1

1

1

S

COS = CI xor A xor BCO = B CI + A CI + A B = CI (A + B) + A BNow we can connect them up to do multiple bits…

0 0 1 1

+ 0 0 1 0

0 1 0 1

1

A

B

S

CinCo

Ripple Carry

+

A3 B3

S3

+

A2 B2

S2

+

A1 B1

S1

+

A0 B0

S0C1C2C3

Page 21: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Full Adder from Half Adders (little aside)Alternative Implementation: 5 GatesA B + CI (A xor B) = A B + B CI + A CIStandard Approach: 6 GatesA

AA

B

BB CI

CIS

CO

Half Adder

A

B

Half Adder

A + B

CI

A + B + CIS S

COCOCI (A + B)A B

S

CO

Delay in the Ripple Carry AdderCritical delay: the propagation of carry from low to high order stagesA

A

B

B

CI CO

@0@0

@0@0

@N

@1

@1

@N+1

@N+2

latearrivingsignal two gate delaysto compute CO4 stageadder final sum andcarryA 0

B 0

C 0

S 0 @2

A 1

B 1

C 1 @2

S 1 @3

A 2

B 2

C 2 @4

S 2 @5

A 3

B 3

C 3 @6

S 3 @7

C 4 @8

0

1

2

3

Ripple Carry TimingCritical delay: the propagation of carry from low to high order stages1111 + 0001worst caseaddition T0: Inputs to the adder are validT2: Stage 0 carry out (C1)T4: Stage 1 carry out (C2)T6: Stage 2 carry out (C3)T8: Stage 3 carry out (C4) 2 delays to compute sumbut last carry not readyuntil 6 delays laterT0 T2 T4 T6 T8

S0, C1 Valid S1, C2 Valid S2, C3 Valid S3, C4 Valid

Page 22: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Adders (cont.)

Ripple Adder

Ripple adder is inherently slow because, in generals7 must wait for c7 which must wait for c6 …

T α n, Cost α n

How do we make it faster, perhaps with more cost?

FA

c0a0b0

s0c1

c2c3c4c5c6c7

s7 s6

Or use a MUX !!!

Classic approach: Carry Look-Ahead

Carry Select Adder

T = Tripple_adder / 2 + TMUX

COST = 1.5 * COSTripple_adder+ (n+1) * COSTMUX

0

1c8

FA

0a4a5a6a7b7 b6 b5 b4c0

a0b0

s0

a1a2a3b3 b2 b1

s1s2s3

FA

1a4a5a6a7b7 b6 b5 b4

1 0 1 01 0 1 0

s4s5s6s7

Extended Carry Select Adder

• What is the optimal # of blocks and # of bits/block ?– If # blocks too large delay dominated by total mux d elay– If # blocks too small delay dominated by adder dela y per block

1

0

1 0 1 0 1 0 1 0

4-bit Adder

4-bitAdder

1

0

1 0 1 0 1 0 1 0

4-bit Adder

4-bitAdder

1

0

1 0 1 0 1 0 1 0

4-bit Adder

4-bitAdder

4-bit Adder

a3-a0b3-b0

cincout

a11-a8b11-b8a15-a12b15-b12 b7-b4 a7-a4

bits N of stages N T α sqrt(N),Cost ≈2*ripple + muxes

Page 23: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Carry Select Adder Performance

• Compare to ripple adder delay:T total = 2 sqrt(N) T FA – TFA, assuming T FA = TMUX

For ripple adder T total = N TFA

“cross-over” at N=3, Carry select faster for any valu e of N>3.

• Is sqrt(N) really the optimum?– From right to left increase size of each block to b etter match delays

– Ex: 64-bit adder, use block sizes [12 11 10 9 8 7 7]

• How about recursively defined carry select?

1

0

1 0 1 0 1 0 1 0

4-bit Adder

4-bitAdder

1

0

1 0 1 0 1 0 1 0

4-bit Adder

4-bitAdder

1

0

1 0 1 0 1 0 1 0

4-bit Adder

4-bitAdder

4-bit Adder

a3-a0b3-b0

cincout

a11-a8b11-b8a15-a12b15-b12 b7-b4 a7-a4

What really happens with the carries

FA

c0a0b0

s0c1

c2c3c4c5c6c7

s7 s6

A B Cout S

0 0 0 Cin

0 1 Cin ~Cin

1 0 Cin ~Cin

1 1 1 Cin

Carry action

kill

Propagate

propagate

generateCarry Generate Gi = Ai Bi must generate carry when A = B = 1Carry Propagate Pi = Ai xor Bi carry in will equal carry out hereAi

BiGi

Ai

BiPi

All generates and propagates in parallel at first s tage. No ripple.

Carry Look Ahead LogicCarry Generate Gi = Ai Bi must generate carry when A = B = 1Carry Propagate Pi = Ai xor Bi carry in will equal carry out hereSi = Ai xor Bi xor Ci = Pi xor CiCi+1 = Ai Bi + Ai Ci + Bi Ci= Ai Bi + Ci (Ai + Bi)= Ai Bi + Ci (Ai xor Bi)= Gi + Ci PiSum and Carry can be reexpressed in terms of generate/propagate:Gi

Ci

Pi

Ci

PiSi

Ci+1

Page 24: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

All Carries in ParallelReexpress the carry logic for each of the bits:C1 = G0 + P0 C0C2 = G1 + P1 C1 = G1 + P1 G0 + P1 P0 C0C3 = G2 + P2 C2 = G2 + P2 G1 + P2 P1 G0 + P2 P1 P0 C0C4 = G3 + P3 C3 = G3 + P3 G2 + P3 P2 G1 + P3 P2 P1 G0 + P3 P2 P1 P0 C0Each of the carry equations can be implemented in a two-level logicnetworkVariables are the adder inputs and carry in to stage 0!CLA Implementation Adder with Propagate and Generate OutputsIncreasingly complex logicPi @ 1 gate delay

Ci Si @ 2 gate delays

BiAi

Gi @ 1 gate delay

C0C0

C0

C0P0P0

P0

P0

G0G0

G0

G0

C1

P1

P1

P1

P1

P1

P1 G1

G1

G1

C2P2

P2

P2

P2

P2

P2

G2

G2

C3

P3

P3

P3

P3

G3

C4

How do we extend this to larger adders?

• Faster carry propagation– 4 bits at a time

• But still linear• Can we get to log?• Compute propagate and generate for each adder BLOCK

44

4

A3-0 B3-0

S3-0

44

4

A7-4 B7-4

S7-4

44

4

A11-8 B11-8

S11-8

44

4

A15-12 B15-12

S15-12

Page 25: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Cascaded Carry Lookahead4 bit adders with internal carry lookaheadsecond level carry lookahead unit, extends lookahead to 16 bitsOne more level to 64 bits4-bit Adder

4 4

4

A [15-12] B [15-12] C 12 C 16

S [15-12]

P G 4-bit Adder

4 4

4

A [1 1-8] B [1 1-8] C 8

S [1 1-8]

P G 4-bit Adder

4 4

4

A [7-4] B [7-4] C 4

S [7-4]

P G 4-bit Adder

4 4

4

A [3-0] B [3-0] C 0

S [3-0]

P G

Lookahead Carry Unit C 0

P 0 G 0 P 1 G 1 P 2 G 2 P 3 G 3 C 3 C 2 C 1

C 0

P 3-0 G 3-0

C 4

@3 @2

@0

@4

@4 @3 @2 @5

@7

@3 @2 @5

@8 @8

@3 @2

@5

@5 @3

@0

C 16

Trade-offs in combinational logic design• Time vs. Space Trade-offsDoing things fast requires more logic and thus more spaceExample: carry lookahead logic • Simple with lots of gates vs complex with fewer• Arithmetic Logic UnitsCritical component of processor datapathInner-most "loop" of most computer instructions2s comp. Overflow Detection53-8 0 1 1 10 1 0 10 0 1 11 0 0 0 -7-27 1 0 0 01 0 0 11 1 0 01 0 1 1 1527 0 0 0 00 1 0 10 0 1 00 1 1 1 -3-5-8 1 1 1 11 1 0 11 0 1 11 1 0 0 0Overflow OverflowNo overflow No overflowOverflow occurs when carry in to sign does not equal carry out

Page 26: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

2s Complement Adder/SubtractorA - B = A + (-B) = A + B + 1A B

CO

S

+ CI

A B

CO

S

+ CI

A B

CO

S

+ CI

A B

CO

S

+ CI

0 1

Add/Subtract

A 3 B 3 B 3

0 1

A 2 B 2 B 2

0 1

A 1 B 1 B 1

0 1

A 0 B 0 B 0

Sel Sel Sel Sel

S 3 S 2 S 1 S 0

Overflow

Summary

• Circuit design for unsigned addition– Full adder per bit slice– Delay limited by Carry Propagation

» Ripple is algorithmically slow, but wires are short

• Carry select– Simple, resource-intensive– Excellent layout

• Carry look-ahead– Excellent asymptotic behavior– Great at the board level, but wire length effects a re significant on chip

• Digital number systems– How to represent negative numbers– Simple operations– Clean algorithmic properties

• 2s complement is most widely used– Circuit for unsigned arithmetic– Subtract by complement and carry in– Overflow when cin xor cout of sign-bit is 1

Basic Arithmetic and the ALU

• Now– Integer multiplication

» Booth’s algorithm– Integer division

» Restoring, non-restoring– Floating point representation– Floating point addition, multiplication

Page 27: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Multiplication

• Flashback to 3 rd grade– Multiplier– Multiplicand– Partial products– Final sum

• Base 10: 8 x 9 = 72– PP: 8 + 0 + 0 + 64 = 72

• How wide is the result?– log(n x m) = log(n) + log(m)– 32b x 32b = 64b result

0001001

0001

0000

0000

0001

1001x

0001

Combinational Multiplier

• Generating partial products– 2:1 mux based on multiplier[i] selects multiplicand or 0x0– 32 partial products (!)

• Summing partial products– Build Wallace tree of CSA

Carry Save Adder

A + B => SSave carries A + B => S, C out

Use C in A + B + C => S1, S2 (3# to 2# in parallel)Used in combinational multipliers by building a Wal lace Tree

c b a

c s

CSA

Page 28: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Wallace Tree

abc def

CSA

CSA

CSA

CSA

Multicycle Multipliers

• Combinational multipliers– Very hardware-intensive– Integer multiply relatively rare– Not the right place to spend resources

• Multicycle multipliers– Iterate through bits of multiplier– Conditionally add shifted multiplicand

Multiplier (F4.25) 0001001 0001 0000 0000 0001 1001x 0001

Page 29: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Multiplier (F4.26)

Done

1. Test M ultip lier0

1a . Add m ultip licand to p roduct and place the result in Product reg iste r

2. Sh if t the M ultip licand register le ft 1 b it

3. Sh ift the M u ltiplie r registe r righ t 1 bit

32nd repe tit ion?

Sta rt

M ultip lier0 = 0M ultip lier0 = 1

No: < 32 repe titions

Yes: 32 repet itions0001001 0001 0000 0000 0001 1001x 0001

Multiplier Improvements

• Do we really need a 64-bit adder?– No, since low-order bits are not involved

– Hence, just use a 32-bit adder» Shift product register right on every step

• Do we really need a separate multiplier register?– No, since low-order bits of 64-bit product are init ially unused– Hence, just store multiplier there initially

Multiplier (F4.31)

Control testWrite

32 bits

64 bits

Shift rightProduct

Multiplicand

32-bit ALU

0001001 0001 0000 0000 0001 1001x 0001

Page 30: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Multiplier (F4.32)

D one

1. Test P roduct0

1a. A dd multiplicand to the le ft ha lf of the product and p lace the resu lt in the le ft ha lf of the Product register

2 . S hift the Product reg ister righ t 1 bit

32nd repetition?

Start

Product0 = 0Product0 = 1

No: < 32 repeti tions

Yes: 32 repeti tions0001001 0001 0000 0000 0001 1001x 0001Signed Multiplication

• Recall– For p = a x b, if a<0 or b<0, then p < 0– If a<0 and b<0, then p > 0– Hence sign(p) = sign(a) xor sign(b)

• Hence– Convert multiplier, multiplicand to positive number with (n-1) bits– Multiply positive numbers– Compute sign, convert product accordingly

• Or,– Perform sign-extension on shifts for F4.31 design– Right answer falls out

Booth’s Encoding

• Recall grade school trick– When multiplying by 9:

» Multiply by 10 (easy, just shift digits left)» Subtract once

– E.g.» 123454 x 9 = 123454 x (10 – 1) = 1234540 – 123454» Converts addition of six partial products to one sh ift and one

subtraction

• Booth’s algorithm applies same principle– Except no ‘9’ in binary, just ‘1’ and ‘0’– So, it’s actually easier!

Page 31: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Booth’s Encoding

• Search for a run of ‘1’ bits in the multiplier– E.g. ‘0110’ has a run of 2 ‘1’ bits in the middle– Multiplying by ‘0110’ (6 in decimal) is equivalent to multiplying by 8

and subtracting twice, since 6 x m = (8 – 2) x m = 8 m – 2m

• Hence, iterate right to left and:– Subtract multiplicand from product at first ‘1’– Add multiplicand to product after first ‘1’– Don’t do either for ‘1’ bits in the middle

Booth’s Algorithm 00001111000000011110000000111100000001111000Example NothingMiddle of a run of ‘0’00 AddEnd of a run of ‘1’10 NothingMiddle of run of ‘1’11 SubtractBegins run of ‘1’01 OperationExplanationBit to rightCurrent bitInteger Division

• Again, back to 3 rd grade 1111011 000000 101110 00001 Remainder-001- Dividend0010001Divisor Quotient

Page 32: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Integer Division

• How does hardware know if division fits?– Condition: if remainder � divisor

– Use subtraction: (remainder – divisor) � 0

• OK, so if it fits, what do we do?– Remainder n+1 = Remainder n – divisor

• What if it doesn’t fit?– Have to restore original remainder

• Called restoring division

Integer Division (F4.40)

Done

Test Remainder

2a. Shift the Quotient register to the left, setting the new rightmost bit to 1

3. Shift the Divisor register right 1 bit

33rd repetition?

Start

Remainder < 0

No: < 33 repetitions

Yes: 33 repetitions

2b. Restore the original value by adding the Divisor register to the Remainder

register and place the sum in the Remainder register. Also shift the

Quotient register to the left, setting the new least significant bit to 0

1. Subtract the Divisor register from the Remainder register and place the result in the Remainder register

Remainder > 0

–1111011 000000 101110 00001 Remainder-001- Dividend0010001Divisor QuotientInteger Division

64-bit ALU

Control test

QuotientShift left

RemainderWrite

DivisorShift right

64 bits

64 bits

32 bits

1111011 000000 101110 00001 Remainder-001- Dividend0010001Divisor Quotient

Page 33: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Division Improvements

• Skip first subtract– Can’t shift ‘1’ into quotient anyway– Hence shift first, then subtract

» Undo extra shift at end

• Hardware similar to multiplier– Can store quotient in remainder register– Only need 32b ALU

» Shift remainder left vs. divisor right

Improved Divider(F4.40)

D o n e. S h if t le f t h a l f o f R e m a ind e r r ig h t 1 b it

T e s t R em a in d e r

3 a . S h if t th e R e m a ind e r re g is te r to t h e le ft, s e t tin g t he ne w r ig h tm os t b i t to 1

3 2 n d rep et it io n ?

S ta r t

R e m a in de r < 0

N o : < 3 2 re p e ti t io n s

Y e s : 3 2 r e p et it io n s

3 b . R e s t or e th e or ig in a l v a lu e b y ad d ing t h e D iv is or r eg is te r to th e le f t h a lf o f th e

R e m a in d e r r e g is t e r a n d p la ce th e su m in th e le f t h a l f o f t h e R em a in d e r reg is te r .

A ls o sh i ft t h e R em ain d er reg is te r to th e le f t , s e t tin g t h e n e w rig h tm o s t b i t t o 0

2 . S u bt ra c t t he D iv is o r re g ist e r f ro m th e le f t h a lf o f th e R e m a ind e r re g is te r a nd p lac e th e r e s u l t in th e le ft h a l f o f th e

R e m a in d e r re g is te r

R e m a in de r 0

1 . S h i ft t h e R em a in d e r re g ist e r le f t 1 b i t

–>

Improved Divider (F4.41)

Write

32 bits

64 bits

Shift leftShift right

Remainder

32-bit ALU

Divisor

Control test

Page 34: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Further Improvements

• Division still takes:– 2 ALU cycles per bit position

» 1 to check for divisibility (subtract)» One to restore (if needed)

• Can reduce to 1 cycle per bit– Called non-restoring division– Avoids restore of remainder when test fails

Non-restoring Division

• Consider remainder to be restored:Ri = Ri-1 – d < 0

– Since R i is negative, we must restore it, right?– Well, maybe not. Consider next step i+1:

Ri+1 = 2 x (Ri) – d = 2 x (R i – d) + d

• Hence, we can compute R i+1 by not restoring R i, and adding d instead of subtracting d

– Same value for R i+1 results

• Throughput of 1 bit per cycle

NR Division Example 0001 00110010Shift Rem right by 1 0010 00110010Rem > 0 (sub next), sll 1 0001 00010010Rem = Rem – Div4 0011 000100103a: Rem > 0 (sub next), sll 1 0001 100000102: Rem = Rem + Div3 1111 100000103b: Rem < 0 (add next), sll 0 1111 110000102: Rem = Rem + Div2 1101 110000103b: Rem < 0 (add next), sll 0 1110 111000102: Rem = Rem - Div1 0000 11100010Shift rem left 1 0000 01110010Initial values0 RemainderDivisorStepIteration

Page 35: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

2’s Complement Number “line”: N = 5• 2N-1 non-negatives • 2N-1 negatives• one zero• how many

positives?

00000 0000100010

1111111110

10000 0111110001

0 12

-1-2

-15 -16 15

.

.

.

.

.

.

-311101

-411100

00000 00001 01111...

111111111010000 ...

So what about subtraction?

• Develop subtraction circuit using the same process

– Truth table for each bit slice– Borrow in from slice of lesser

significance– Borrow out to slice of greater

significance– Very much like carry chain

• Homework exercise

0000

0111

0011

1011

1111

1110

1101

1100

1010

1001

1000

0110

0101

0100

0010

0001

+0

+1

+2

+3

+4

+5

+6

+7+8

+9

+10

+11

+12

+13

+14

+15

-

Finite representation?

• What happens whenA + B > 2 N - 1 ?

– Overflow– Detect?

» Carry out

• What happens when A - B < 0 ?

– Negative numbers?

– Borrow out?

0000

0111

0011

1011

1111

1110

1101

1100

1010

1001

1000

0110

0101

0100

0010

0001

+0

+1

+2

+3

+4

+5

+6

+7+8

+9

+10

+11

+12

+13

+14

+15

-

Page 36: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Number Systems• Desirable properties:

– Efficient encoding (2 n bit patterns. How many numbers?)– Positive and negative

» Closure (almost) under addition and subtraction• Except when overflow

» Representation of positive numbers same in most sys tems» Major differences are in how negative numbers are r epresented

– Efficient operations» Comparison: =, <, >» Addition, Subtraction» Detection of overflow

– Algebraic properties?» Closure under negation?» A == B iff A – B == 0

• Three Major schemes:– sign and magnitude– ones complement– twos complement– (excess notation)

Booth’s Encoding

• Really just a new way to encode numbers– Normally positionally weighted as 2 n

– With Booth, each position has a sign bit– Can be extended to multiple bits

0

0

2-bit Booth-2+2

1-bit Booth-10+1

Binary110

2-bits/cycle Booth Multiplier

• For every pair of multiplier bits– If Booth’s encoding is ‘-2’

» Shift multiplicand left by 1, then subtract– If Booth’s encoding is ‘-1’

» Subtract– If Booth’s encoding is ‘0’

» Do nothing– If Booth’s encoding is ‘1’

» Add– If Booth’s encoding is ‘2’

» Shift multiplicand left by 1, then add

Page 37: Egyptian Numerals - huji.ac.ilcomarc/slides/lect2-3.pdf · on bones. • 5000BC A decimal number system is in use in Egypt. • 4000BC Babylonian and Egyptian calendars in use. •

Booth’s Example

• Negative multiplicand: -6 x 6 = -361010 x 0110, 0110 in Booth’s encoding is +0-0Hence: 1101 1100 (-36)Final Sum: 1101 0000x +11101 0000 0000 0000x 01110 1000 0000 1100x –11111 0100 0000 0000x 01111 1010

Booth’s Example

• Negative multiplier: -6 x -2 = 121010 x 1110, 1110 in Booth’s encoding is 00-0Hence: 0000 1100 (-12)Final Sum: 0000 0000x 01101 0000 0000 0000x 01110 1000 0000 1100x –11111 0100 0000 0000x 01111 1010