1 cs 106 computing fundamentals ii chapter 9 “binary and decimal” herbert g. mayer, psu cs...

20
1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS Herbert G. Mayer, PSU CS status 6/17/2013 status 6/17/2013

Upload: doris-leonard

Post on 31-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

1

CS 106Computing Fundamentals II

Chapter 9“Binary and Decimal”

Herbert G. Mayer, PSU CSHerbert G. Mayer, PSU CSstatus 6/17/2013status 6/17/2013

Page 2: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

2

Syllabus

• George BooleGeorge Boole• Augustus De MorganAugustus De Morgan• Boolean ValuesBoolean Values• 16 Boolean Operators16 Boolean Operators• Boolean And, Or, Xor, NotBoolean And, Or, Xor, Not• De MorganDe Morgan’’s Theorems Theorem• Useful UrlsUseful Urls• Decimal Number SystemDecimal Number System• Binary Number SystemBinary Number System

Page 3: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

3

George Boole George BooleGeorge Boole, 1815 to 1864, was a British mathematician, , 1815 to 1864, was a British mathematician,

who developed who developed Boolean Algebra Boolean Algebra (BA). He led a relatively (BA). He led a relatively uneventful life, being a private teacher at a young age in uneventful life, being a private teacher at a young age in Doncaster, England, then math teacher in Liverpool, and in Doncaster, England, then math teacher in Liverpool, and in 1849 received an appointment as the first professor of 1849 received an appointment as the first professor of mathematics at Queen's College, Cork Ireland.mathematics at Queen's College, Cork Ireland.

He published numerous unexciting paper in the world of He published numerous unexciting paper in the world of mathematicians at the time. Of these, several are now mathematicians at the time. Of these, several are now regarded sine-qua-non standard material in Boolean regarded sine-qua-non standard material in Boolean Algebra, starting with his earliest publication on Algebra, starting with his earliest publication on Theory of Theory of Analytical TransformationsAnalytical Transformations..

Interestingly, the effective founder of the theory of Interestingly, the effective founder of the theory of computer science did not regard his specialty, BA, to be a computer science did not regard his specialty, BA, to be a branch of mathematics. His work influenced another branch of mathematics. His work influenced another contemporary founder of BA, contemporary founder of BA, Augustus De MorganAugustus De Morgan, living , living also in England from 1806 to 1871. also in England from 1806 to 1871.

Page 4: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

4

Augustus De Morgan

Augustus De Morgan was born in 1806 in Mandura, Augustus De Morgan was born in 1806 in Mandura, India. Lived in England, graduated from Trinity India. Lived in England, graduated from Trinity College, Cambridge in 1827, with well-rounded, College, Cambridge in 1827, with well-rounded, classical education. Became professor of mathematics classical education. Became professor of mathematics at University College in London. at University College in London. De Morgan was De Morgan was renowned for his methods of making math interesting renowned for his methods of making math interesting and stimulating to students. Died 1871 in London and stimulating to students. Died 1871 in London after producing a vast body of publications.after producing a vast body of publications.

Well known as BooleWell known as Boole’’s contemporary and articulator s contemporary and articulator of De Morganof De Morgan’’s theorem; but it had been known s theorem; but it had been known already in the times of Aristotle.already in the times of Aristotle.

Page 5: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

5

Boolean ValuesThe value domain of Boolean Algebra (BA) is limited to The value domain of Boolean Algebra (BA) is limited to the two values True and False, in the computer science the two values True and False, in the computer science world also known as 0 and 1, represented via a bit. In world also known as 0 and 1, represented via a bit. In the computer technology such a bit is represented by the computer technology such a bit is represented by defined voltage values. For example, a value close to 0 defined voltage values. For example, a value close to 0 V stands for False (or 0), while an electrical value of V stands for False (or 0), while an electrical value of 1.1 V stands for True (or 1). The real value of using 1.1 V stands for True (or 1). The real value of using bits comes in their combination to large sequences of bits comes in their combination to large sequences of bit strings.bit strings.

Boolean functions (operators) are logic operation on Boolean functions (operators) are logic operation on bits. There are 16 distinct operations, including the bits. There are 16 distinct operations, including the trivial identities of always trivial identities of always true true or always or always falsefalse; all 16 ; all 16 are shown below:are shown below:

Page 6: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

6

16 Boolean Operators

Page 7: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

7

Boolean And

Boolean function Boolean function a and ba and b::

true if and only if a and b are both true:true if and only if a and b are both true:

Page 8: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

8

Boolean Or --Inclusive

Boolean function Boolean function a inclusive or ba inclusive or b::

true if a or b or both are true:true if a or b or both are true:

Page 9: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

9

Boolean Or --Exclusive

Boolean function Boolean function a exclusive or b:a exclusive or b:

true if value of a differs from b:true if value of a differs from b:

Page 10: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

10

Boolean Not

Boolean function Boolean function not anot a: is true only if a is false:: is true only if a is false:

Page 11: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

11

De Morgan’s Theorem

Assuming a and b are Boolean variablesAssuming a and b are Boolean variables

. . . and . . . and ^̂ is the is the logical-andlogical-and, and , and vv is the is the logical-or oplogical-or op

Finally Finally ! ! stands for the logical-not. Then:stands for the logical-not. Then:

1.) ! ( a ^ b ) = ( ! a ) v ( ! b )1.) ! ( a ^ b ) = ( ! a ) v ( ! b )

2.) ! ( a v b ) = (! a ) ^ ( ! b )2.) ! ( a v b ) = (! a ) ^ ( ! b )

1.) if the combined 1.) if the combined a and b a and b condition is false, then at condition is false, then at least one of a and b must be falseleast one of a and b must be false

2.) if the combined 2.) if the combined a or b a or b condition is false, then both a condition is false, then both a and also b must be falseand also b must be false

Page 12: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

12

Useful URLs

Useful urls:

Boolean Functions @ Wolfram http: / /mathworld.wolf ram.com/ BooleanFunction.ht ml Boolean Functions @ Wikipedia http: / / en.wikipedia.org/w iki/ Boolean_ function Boolean Functions @ NIST http: / /www.nist.gov/dads/HTML/booleanfnctn.html Floating point standard 754: http: / / en.wikipedia.org/w iki/ IEEE_754

Page 13: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

13

Decimal NumbersThe decimal number system of our daily life uses 10 as the base to The decimal number system of our daily life uses 10 as the base to construct arbitrarily large values. Important in this system are the use construct arbitrarily large values. Important in this system are the use of the value 0, and use of a series of powers of 10 increasing from of the value 0, and use of a series of powers of 10 increasing from right to left. Each digit position moving toward the left increases the right to left. Each digit position moving toward the left increases the exponent (of the power of 10).exponent (of the power of 10).

Why we use 10 as the base is not totally obvious, yet our having 10 Why we use 10 as the base is not totally obvious, yet our having 10 fingers and 10 toes may have something to do with it. This decimal fingers and 10 toes may have something to do with it. This decimal system had been used in the Arab civilization since times before system had been used in the Arab civilization since times before Christ.Christ.

Due to its superb qualities, the decimal number system has replaced Due to its superb qualities, the decimal number system has replaced the Roman number system in the western world. The Roman system the Roman number system in the western world. The Roman system was vaguely based on 10, but lacked the notion of a zero and the was vaguely based on 10, but lacked the notion of a zero and the power series.power series.

In other ancient civilizations (Babylonian) 60 was a common numeric In other ancient civilizations (Babylonian) 60 was a common numeric base, and todaybase, and today’’s system of tracking time with 60 seconds per minute s system of tracking time with 60 seconds per minute and 60 minutes per hour still is reminiscent of that practice. Other and 60 minutes per hour still is reminiscent of that practice. Other ancient cultures used 64 as the base, yet computers today use 2 as ancient cultures used 64 as the base, yet computers today use 2 as their base. Earlier computers with base 10 were also fairly common.their base. Earlier computers with base 10 were also fairly common.

Page 14: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

14

Decimal NumbersDecimal number 00001230 has the value Decimal number 00001230 has the value one thousand, two one thousand, two hundred, and thirtyhundred, and thirty. 00,001,230 similarly, except commas are . 00,001,230 similarly, except commas are added for better readability. Leading zeros have no value and added for better readability. Leading zeros have no value and can be ignored. Thus it is efficient to skip them.can be ignored. Thus it is efficient to skip them.

Trailing zeros after other decimal digits –before the implied Trailing zeros after other decimal digits –before the implied decimal point— have enormous meaning, as they shift the decimal point— have enormous meaning, as they shift the complete range of all preceding digits to higher values.complete range of all preceding digits to higher values.

Decimal integers are generally written without decimal point. Decimal integers are generally written without decimal point. The actual, numeric value of decimal integers is constructed The actual, numeric value of decimal integers is constructed from right to left, with increasing exponent to the base 10:from right to left, with increasing exponent to the base 10:

0001230 = 0 * 100001230 = 0 * 1000 + 3 * 10 + 3 * 1011 + 2 * 10 + 2 * 1022 + 1 * 10 + 1 * 1033 + 0 * 10 + 0 * 1044 + 0 * 10 + 0 * 1055 + 0 * 10 + 0 * 1066

For general decimal numbers the computation of the For general decimal numbers the computation of the fractional fractional value progresses left to right, with decreasing exponents for value progresses left to right, with decreasing exponents for the base 10, starting with -1 (i.e. 10the base 10, starting with -1 (i.e. 10-1-1) in the first position after ) in the first position after the decimal point:the decimal point:

12.345 =12.345 = (integral) 2 * 10(integral) 2 * 1000 + 1 * 10 + 1 * 1011

(fraction) 3 * 10(fraction) 3 * 10-1-1 + 4 * 10 + 4 * 10-2-2 + 5 * 10 + 5 * 10-3-3

Page 15: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

15

Binary NumbersBinary number Binary number 01010101 has value one hundred and one in binary has value one hundred and one in binary or 5 in decimal. Like decimal numbers, leading zeros in the or 5 in decimal. Like decimal numbers, leading zeros in the binary system have no value and can be ignored.binary system have no value and can be ignored.

However, since computers use a fixed number of bits for binary However, since computers use a fixed number of bits for binary numbers, such unused bits are internally actually set to 0.numbers, such unused bits are internally actually set to 0.

Trailing zeros –before the implied binary point— have Trailing zeros –before the implied binary point— have enormous meaning, as they shift the complete range of all enormous meaning, as they shift the complete range of all preceding binary digits (bits).preceding binary digits (bits).

Binary integers are generally written without binary point. The Binary integers are generally written without binary point. The actual, numeric integer value of binary integers is constructed actual, numeric integer value of binary integers is constructed from right to left, with increasing exponent to the base 2 from from right to left, with increasing exponent to the base 2 from right to left:right to left:

0101 = 1 * 20101 = 1 * 200 + 0 * 2 + 0 * 211 + 1 * 2 + 1 * 22 2 + 0 * 2+ 0 * 23 3 = 1 + 0 + 4 + 0= 1 + 0 + 4 + 0

0101 = 1 + 0 + 4 + 00101 = 1 + 0 + 4 + 0

0101010122 = 5 = 51010 = 5 = 5 – decimal 5 is unambiguous, as no binary digit 5 exists!– decimal 5 is unambiguous, as no binary digit 5 exists!

Page 16: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

16

Binary NumbersBy convention, numbers (decimal or binary) are written By convention, numbers (decimal or binary) are written without their base when the context is clear. But if the base without their base when the context is clear. But if the base is critical to know, a subscripted base is added for clarity. is critical to know, a subscripted base is added for clarity. When the digit allows us to infer the base, then again it When the digit allows us to infer the base, then again it would be superfluous to spell out the base.would be superfluous to spell out the base.

For general binary numbers the computation of the For general binary numbers the computation of the fractionalfractional value progresses left to right, with decreasing value progresses left to right, with decreasing exponents of base 2, starting with -1 for 2exponents of base 2, starting with -1 for 2-1-1 in the first in the first position after the binary point:position after the binary point:

101.11 = (integral) 1 * 2101.11 = (integral) 1 * 200 + 0 * 2 + 0 * 211 + 1 * 2 + 1 * 222 + (fractional) 1 * 2 + (fractional) 1 * 2-1-1 + 1 * 2 + 1 * 2-2-2

101.11 = (integral) 1 + 0 + 4 +101.11 = (integral) 1 + 0 + 4 + (fractional) ½ + ¼ (fractional) ½ + ¼

101.11101.1122 = 5.75 = 5.751010 = 5.75 -– 5.75 unambiguous, no binary digits 5 or 7 exist! = 5.75 -– 5.75 unambiguous, no binary digits 5 or 7 exist!

Page 17: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

17

Binary Numbers

Page 18: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

18

Binary NumbersPossible representations of binary numbers: Possible representations of binary numbers: sign-sign-magnitude, onemagnitude, one’’s complements complement, and , and twotwo’’s complement s complement representation (tcr)representation (tcr)..

Advantage ofAdvantage of tcr tcr: computer needs no subtract unit, an : computer needs no subtract unit, an adder suffices. When subtraction is needed, just add a adder suffices. When subtraction is needed, just add a negative number. Also there is no need for signed- negative number. Also there is no need for signed- andand unsigned arithmetic; unsigned suffices. C++ uses heavily unsigned arithmetic; unsigned suffices. C++ uses heavily unsigned integers. In fact unsigned integers. In fact tcrtcr HW implementations can HW implementations can ignore the sign bit. Just need an adder and inverter.ignore the sign bit. Just need an adder and inverter.

Binary numbers in Binary numbers in tcr tcr use a sign bit and a fixed number use a sign bit and a fixed number of bits for the magnitude. For example, on a PC you may of bits for the magnitude. For example, on a PC you may have 32-bit integers, one for the have 32-bit integers, one for the signsign, 31 for the , 31 for the magnitudemagnitude. When processed in a . When processed in a tcrtcr architecture, the architecture, the most significant bit is the most significant bit is the sign bitsign bit, the other 31 bits hold , the other 31 bits hold the actual value of interest. By convention, sign bit value the actual value of interest. By convention, sign bit value of 0 stands for positive and 1 for negative numbers.of 0 stands for positive and 1 for negative numbers.

Page 19: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

19

Two’s Complement RepresentationTo create a To create a negative numbernegative number in in tcrtcr, start out with the binary , start out with the binary representation for the positive one, invert all bits, and representation for the positive one, invert all bits, and add 1add 1. Note . Note that that overflowoverflow cannot happen by inversion alone: the inverse (negative cannot happen by inversion alone: the inverse (negative value) of all representable positive numbers can always be created.value) of all representable positive numbers can always be created.

Overflow Overflow can happen during addition. can happen during addition. Overflow occurs, when the sign Overflow occurs, when the sign bitbit’’s carry-in is different from the sign bit’s carry-outs carry-in is different from the sign bit’s carry-out, i.e. when the , i.e. when the carry into the sign bit is different from the carry out of the sign bit.carry into the sign bit is different from the carry out of the sign bit.

The number of negative numbers is one more than the number of The number of negative numbers is one more than the number of positive ones (not counting 0). Naturally, there is also a 0, but no positive ones (not counting 0). Naturally, there is also a 0, but no negative 0 in negative 0 in tcrtcr, as we find in , as we find in oneone’’s complements complement (ocr)(ocr) and and sign-sign-magnitudemagnitude (smr)(smr) representations. representations.

To generate the absolute value of a negative number, invert all bits of To generate the absolute value of a negative number, invert all bits of the original negative number and the original negative number and add 1add 1. It is important to add a 1 . It is important to add a 1 again, not to subtract it. However, there will be one negative value, again, not to subtract it. However, there will be one negative value, whose positive inverse cannot be represented, causing whose positive inverse cannot be represented, causing overflowoverflow. That . That value is the smallest negative number. An 8-bit, signed value is the smallest negative number. An 8-bit, signed tcrtcr integer can integer can hold integral values in the range from -128 .. 127. See the asymmetry? hold integral values in the range from -128 .. 127. See the asymmetry? See the one negative value that cannot be inverted?See the one negative value that cannot be inverted?

Page 20: 1 CS 106 Computing Fundamentals II Chapter 9 “Binary and Decimal” Herbert G. Mayer, PSU CS status 6/17/2013

20

Decimal to Binary ConversionTake the decimal number Take the decimal number dd to be converted to binary representation, to be converted to binary representation,

and as long as and as long as dd is not yet zero, do the following repeatedly: is not yet zero, do the following repeatedly: divide d by 2; the new value is d/2

/ is integer divide, different from floating-point divide!

note the remainder, it will be 0 or 1; call this the initial string s

do repeatedly, until d is 0, each time adding one more remainder digit to s

When done, read the digits of When done, read the digits of ss backwards: that is number backwards: that is number b b in binaryin binary