psu cs 106 computing fundamentals ii bits, binary numbers & binary arithmetic hm 1/3/2009

26
PSU CS 106 Computing Fundamentals II Bits, Binary Numbers & Binary Arithmetic HM 1/3/2009

Upload: basil-arnold

Post on 26-Dec-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

PSU CS 106Computing Fundamentals II

Bits, Binary Numbers&

Binary Arithmetic

HM 1/3/2009

2© Dr. Herbert G. Mayer

Agenda

• Decimal Number System• Binary Number System• Signed Binary Integers• Two’s Complement Representation tcr• Decimal to Binary Conversion• Sample 8-bit Binary Numbers• Hexadecimal Numbers• Adding Hex• Other Bases, and Bit Strings• Floating-Point Numbers• Characters

3© Dr. Herbert G. Mayer

Decimal Number System• The decimal number system of our daily life uses 10 as the base to

construct arbitrarily large values. Important in this number system are the use of the value 0, and the use of a series of powers of 10 increasing from right to left. Each digit position moving toward the left increases the exponent (of the power of 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 system had been used in the Arab civilization since times way before Christ.

• Due to superb qualities, the decimal number system has replaced 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 power series.

• In other ancient civilizations (Babylonian) 60 was a common numeric base, and today’s system of tracking time with 60 seconds per minute 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 their base. Earlier computers with base 10 were also fairly common.

4© Dr. Herbert G. Mayer

Decimal Number System, Samples• Decimal number 00001230 has the value one thousand, two

hundred, and thirty. 00,001,230 similarly, except commas are added for better readability. Yet leading zeros have no value and can be ignored. Thus it is efficient to skip them.

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

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

0001230 = 0 * 100 + 3 * 101 + 2 * 102 + 1 * 103 + 0 * 104 + 0 * 105 + 0 * 106

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

12.345 = (integral) 2 * 100 + 1 * 101

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

5© Dr. Herbert G. Mayer

Binary Number System• Binary number 0101 has the value one hundred and one, binary

or 5 decimal. Like decimal numbers, leading zeros in the binary system have no value and are ignored.

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

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

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

0101 = 1 * 20 + 0 * 21 + 1 * 22 + 0 * 23 = 1 + 0 + 4 + 0

0101 = 1 + 0 + 4 + 0

01012 = 510 = 5 -– 5 is unambiguous, as no matching binary digit exists!

6© Dr. Herbert G. Mayer

Binary Number System, Samples• By convention, numbers (decimal or binary) are written without

their base when the context is clear. But if the base is critical to know, a subscripted base is added for clarity. When the digit allows us to infer the base, then again it would be superfluous to spell out the base.

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

101.11 = (integral) 1 * 20 + 0 * 21 + 1 * 22 + (fractional) 1 * 2-1 + 1 * 2-2

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

101.112 = 5.7510 = 5.75 -– 5.75 unambiguous, as no binary digits 5 or 7 exist!

7© Dr. Herbert G. Mayer

Binary Number System, Samples

Exponent Power of 2 Name

0 20 = 1

1 21 = 2

2 2* 2 = 22 = 4

3 2* 2* 2 = 22* 2 = 23 = 8

4 2* 2* 2* 2 = 22* 22 = 24 = 16

5 32

6 64

7 128

8 256 Size of byte

9 512

10 210 = 1024 One K

20 210 * 210 = 220 = 1024* 1024 = 1,048,576 One Meg

30 220 * 210 = 230 = 1,073,741,824 One Gig

32 232 = 4 * 1,073,741,824 = 4,294,967,296 4 Gig

8© Dr. Herbert G. Mayer

Signed Binary Integers• Possible representations of binary numbers: sign-magnitude, one’s

complement, and two’s complement representation (tcr). Advantage of tcr: the machine needs no subtract unit, a single adder suffices. When subtraction is needed, just add a negative number. Also there is no need for signed- and unsigned arithmetic; unsigned suffices. C++ uses heavily unsigned integers. In fact tcr HW implementations can ignore the sign bit. Such architectures just need an adder and inverter.

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

9© Dr. Herbert G. Mayer

Two’s Complement Representation tcr• To create a negative number in tcr, start out with the binary representation

for the positive one, invert all bits, and add 1. Note that overflow cannot happen by inversion alone: the inverse (negative value) of all representable positive numbers can always be created.

• Overflow can happen during addition. Overflow occurs, when the sign bit’s carry-in is different from the carry-out, i.e. when the carry into the sign bit is different from the carry out of the sign bit, i.e. the carry created by adding the two sign bits.

• 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 negative 0 in tcr, as we find in one’s complement (ocr) and sign-magnitude (smr) representations.

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

10© Dr. Herbert G. Mayer

Decimal to Binary Conversion• Take the decimal number d to be converted to binary representation,

and as long as d 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!– write the remainder, which 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 s backwards: that is number b in binary

Decimal d Quotient Remainder

500 / 2 = 250 0

250 / 2 = 125 0

125 / 2 = 62 1

62 / 2 = 31 0

31 / 2 = 15 1

15 / 2 = 7 1

7 / 2 = 3 1

3 / 2 = 1 1

1 / 2 = 0 1

11© Dr. Herbert G. Mayer

Binary Conversion: You Exercise

Decimal Quotient Remainder

678 / 2 = 339 0

When d is finally 0, after repeated integer division by 2, reverse the order of the remainder digits, which are the bits in s. In the above example we used decimal 50010 = 1111101002.

This works for any base.

Now convert decimal 678 to binary:

12© Dr. Herbert G. Mayer

Sample 8-bit Binary Numbers

Decimal Value S Binary Digits (Bits)

2 0 0 0 0 0 0 1 0

16 0 0 0 1 0 0 0 0

21 0 0 0 1 0 1 0 1

31 0 0 0 1 1 1 1 1

64 0 1 0 0 0 0 0 0

100 0 1 1 0 0 1 0 0

Largest positive: 127 0 1 1 1 1 1 1 1

-1 1 1 1 1 1 1 1 1

-2 1 1 1 1 1 1 1 0

-16 1 1 1 1 0 0 0 0

-21 1 1 1 0 1 0 1 1

-31 1 1 1 0 0 0 0 1

-64 1 1 0 0 0 0 0 0

-100 1 0 0 1 1 1 0 0

Most negative -128 1 0 0 0 0 0 0 0

almost smallest -127 1 0 0 0 0 0 0 1

13© Dr. Herbert G. Mayer

8-Bit Two’s Complement of -10010

Decimal Value S Binary Digits (Bits)

100 0 1 1 0 0 1 0 0

~100 1 0 0 1 1 0 1 1

+1 1

-100 1 0 0 1 1 1 0 0

Creating a negative number in Two’s Complement binary representation (TCR): Use the bit string for the positive integer, invert all bits, and add a 1, ignore overflow if it occurs:

14© Dr. Herbert G. Mayer

8-Bit Two’s Complement of 9910+1910

S Binary Digits (Bits)

99 0 1 1 0 0 0 1 1

+19 0 0 0 1 0 0 1 1

99+19 = 118 0 1 1 1 0 1 1 0

• 8-bit integer with 1 bit for sign, leaves 7 bits for magnitude• Range from -128 to +127.• Note asymmetry of numeric range.• Sample of 99 + 19 will fit, i.e. does not cause overflow:

15© Dr. Herbert G. Mayer

8-bit Two’s Complement of 9+-7

9+7 = 16 S Binary Digits 9-7 = 9+(-7) S Binary Digits

9 0 0 0 0 1 0 0 1 7 0 0 0 0 0 1 1 1

+7 0 0 0 0 0 1 1 1 ~7 1 1 1 1 1 0 0 0

9+7 = 16 0 0 0 1 0 0 0 0 +1 1

~7 + 1 = -7 1 1 1 1 1 0 0 1

+9 0 0 0 0 1 0 0 1

9+(-7) = 2 0 0 0 0 0 0 1 0

• Left sample: 9 + 7 = 16, fits into 8-bit signed TCR• Right side: 9 – 7 = 9 + (-7), no need for binary subtract unit• Just need an inverter, and use existing adder to add binary value 1

16© Dr. Herbert G. Mayer

8-bit Two’s Complement of -9+-7

7-9 = 7+(-9) S Binary Digits -9-7 = -9-7 S Binary Digits

9 0 0 0 0 1 0 0 1 7 0 0 0 0 0 1 1 1

~9 1 1 1 1 0 1 1 0 ~7 1 1 1 1 1 0 0 0

+1 1 +1 1

-9 1 1 1 1 0 1 1 1 ~7 + 1 = -7 1 1 1 1 1 0 0 1

+7 0 0 0 0 0 1 1 1 +(-9) 1 1 1 1 0 1 1 1

7+(-9) = -2 1 1 1 1 1 1 1 0 -9-7 = -16 1 1 1 1 0 0 0 0

• Left sample: +7-9 = -2, in TCR system is = 11111110• Right side: -9–7 = -16, in TCR system is = 11110000

17© Dr. Herbert G. Mayer

8-bit Two’s Complement of +-100+-64

100+64=164?

S Binary Digits -100-64=-164?

S Binary Digits

100 0 1 1 0 0 1 0 0 -100 1 0 0 1 1 1 0 0

+ 64 0 1 0 0 0 0 0 0 + (-64) 1 1 0 0 0 0 0 0

164?? 1 0 1 0 0 1 0 0 -164?? 0 1 0 1 1 1 0 0

• Left sample: 100 + 64 = 164 will not fit in 8 bits: Overflow (OVFL)• Since carry-in to S = 1, carry-out of S = 0: different; hence OVFL• Right sample: -100 – 64 = -164, also won’t fit: OVFL• Since carry-in to S = 0, carry-out of S = 1: different; hence OVFL

18© Dr. Herbert G. Mayer

Exercise TCR: 40+80,40-80

80 + 40 = S Binary Digits 40 - 80 = S Binary Digits

• The above 2 samples will fit into 8-bit TCR!• Will the subsequent one fit? Prove your answer!

19© Dr. Herbert G. Mayer

Exercise TCR: 112+16, -112-16

• One sample will barely fit into 8-bit TCR. Which one?• The other will barely not fit, prove OVFL occurs!

112 + 16 = S Binary Digits -112 - 16 = S Binary Digits

20© Dr. Herbert G. Mayer

Hexadecimal NumbersHexadecimal (AKA hex) numbers are simply numbers with base 16, not 10, or 2. They have 16 different digits, 0..9 and a..f. The symbol a stands for hex digit 1010 , while f stands for the hex digit 1510. Here a few hex numbers and their equivalent decimal values:

Decimal Hexadecimal Decimal Hexadecimal

0 0 257 101

1 1 258 102

9 9 300 12c

10 A 16,383 3fff

11 B 16,384 4000

15 F 16,385 4001

16 10 32,767 7fff

33 21 32,768 8000

127 7f 32,769 8001

128 80 65,535 ffff

129 81 65,536 1,0000

255 Ff 65,537 1,0001

256 100 4,294,967,295 ffff,ffff

21© Dr. Herbert G. Mayer

Adding Hex: af16+6516, 10a4216+5be16

a f 1 0 a 4 2

+ 6 5 + 5 b e

1 1 4 1 1 0 0 0

Adding Hex numbers proceeds like addition in base 10: Starting at the lowest (rightmost) digit, add 1 digit for both rows at a time, thus the max value cannot reach or exceed 32. Store the digit and record the possible carry for the next digit; note that this can be 1 through f.

22© Dr. Herbert G. Mayer

Other BasesIf you manipulate numbers with a different, larger base than 16, invent suitable new digits, analogous to the convention for hex numbers. For example, if you select a base of 2010, extend the digit range to the digits 1010, 11, 12 .. 19, represented as a, b, c, .. i, j. Here i20 stands for the digit 1810 and j20 for 1910. For example, the decimal number 10010 is 5020 in base 20 notation.

Base 10, 0..9 Base 20, 0..9, a..j

1 1 10 a 15 f 19 j 20 10 21 11 40 20 52 2c 81 41 90 4a

111 5b 1200 300

23© Dr. Herbert G. Mayer

Other Bit Strings• Other Bit String Interpretations: On many computers a

sequence of 8 bits is called a Byte, if such a sequence is uniquely addressable as a whole, complete unit.

• Sequences of bits can be used (i.e. be interpreted) as integer numbers of varying precision, as floating point numbers, decimal numbers, printable ASCII characters of which about 93 are printable, EBCDIC characters, or Unicode with about 100,000 different symbols.

• When looking at any string of bits it is important to keep in mind: “What is the current interpretation of these bits? Are they numbers, printable characters, or combinations of various things?”

24© Dr. Herbert G. Mayer

Integer Numbers• Integer Numbers, AKA ints, represented on a computer come in

various precisions, based on the number of bits allotted to them. See past samples of 8-bit numbers using tcr.

• They are used as signed or unsigned ints, the latter heavily used in early days of computing, since each bit in a restrictedly short word was precious.

• Typical integer precisions include 8, 16, 32, or 64 bits. In unsigned integers, the bit otherwise used for the sign is used to double the representable range of distinct numbers.

25© Dr. Herbert G. Mayer

Floating-Point Numbers• Floating-Point Numbers: AKA floats are commonly packaged into

32 bits, with 1 bit for the sign, 8 bits for the exponent of an assumed base 16, and 23 bits for the mantissa. Double-precision floating point numbers use 64 bits, 1 for the sign, 11 to represent the exponent to the implied base 16, and 52 bits for the much more accurate mantissa.

• The IEEE standard 754 defines a commonly used format for floats, available on many contemporary computers. In the past, different formats and definitions of floats caused dangerous incompatibilities during the porting of data from one computer from another.

26© Dr. Herbert G. Mayer

Characters• Characters: The American Standard Code for Information Interchange

(ASCII) defines certain strings of bits in an 8-bit sequence (AKA byte) to be printable characters. These include 26 printable, common lowercase letters, also uppercase letters, 10 digits, numerous “special characters”, a small range of letter used in some foreign alphabets such as ä and œ, plus some unprintable characters. Only 128 combinations of the 8-bit sequences were used in the original ASCII definition. The leftmost bit in the 8-bit sequence was left unused.

• The Extended Binary Coded, Decimal Interchange Code (EBCDIC) is a different standard for defining character values on a computer; this standard was developed in the early days of electronic computing by IBM and used heavily on the 360-series of computers manufactured by IBM, Honeywell, Siemens and a few other companies. EBCDIC does not define the lowercase letters ‘a’ through ‘z’ and the corresponding uppercase correspondents ‘A’ through ‘Z’ contiguously. This poses an interesting, though only minor, challenge to compiler writers, wishing to devise portable compilers across computers from various manufacturers, and thus across multiple character standards. However, EBCDIC does map all 256 combinations in an 8-bit byte to a character value.