Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Operations on Bits

Similar presentations


Presentation on theme: "Chapter 4 Operations on Bits"— Presentation transcript:

1 Chapter 4 Operations on Bits
Operations on Data on Version 2

2 Objective 這一章節中,我們要看看儲存於電腦中的資料如何做運算。有兩大類的運算:算數運算 (arithmetic operation) 與邏輯運算 (logic operation)。 After reading this chapter, the reader should be able to: Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits. Understand the applications of logical operations using masks. Understand the shift operations on numbers and how a number can be multiplied or divided by powers of two using shift operations.

3 Outlines Arithmetic Operations Logical Operations Shift Operations
Key Terms Summary Homework

4 Section 4.1 Arithmetic Operations
Section 4.3 in Version 2

5 Data Operations (1/2) Operations on data can be divided into two broad categories: Arithmetic operations Logic operations Shift operations Data Operations Arithmetic Operations Logic Operations Shift Operations

6 Data Operations (2/2) Logic operations refer to those operations that apply the same basic operation on individual bits of a pattern, or on two corresponding bits in two patterns. Arithmetic operations refer to those operations to integers and floating-point numbers. Shift operations move the bits in a pattern, changing he positions of the bits. They move bits to the left or to the right.

7 Arithmetic Operations
Arithmetic operations involve Add Subtract Multiply Divide Operations on Integer Floating-point number

8 Arithmetic Operations on Integers
Two’s complement is the only method used to store integers in computers today. We will introduce addition and subtraction operated on the numbers in two’s complement representation. The multiplication and division operation can be implemented in software or in hardware. 任何的 integer representation 都可以各自發展自己加減法的技術, 但由於現在的 computer 都是使用 two’s complement, 所以在這裡只介紹 two’s complement 的加減. 你可以在 “computer architecture” 的書上看到許多關於 implement 乘法與除法的技術.

9 Binary Addition Add 2 bits and propagate the carry to the next column. carry in 1 1 + 1 當 bit 相加超過 bit allocation, 就會產生進位, 稱為 carry. 如果記憶體空間只有一個bit, 當然儲存時, carry 部份就超過可儲存的空間, 會被 discard. carry sum 1 1 carry out

10 Example 1 Question: Add two numbers 17+22=39 in two’s complement representation. Solution: Result is = 39 Correct! Carry 1 + 39

11 Example 2 Question: Add two numbers 24+(-17)=7 in two’s complement representation. Solution: Add two bit strings: If there is a final carry after the leftmost column addition, discard it. Result is = 7 Correct! Carry to leftmost bit  discard + 7 Carry

12 Example 3 Question: Add two numbers =-15 in two’s complement representation. Solution: Result is = -15 Correct! No Carry + -15

13 Example 4 Question: Add two numbers (-1)+(-2)=(-3) in two’s complement representation. Solution: Result is = -3 Correct! Carry to leftmost bit  discard + -3 Carry

14 Example 5 Question: Add two numbers 127+3=130 in two’s complement representation. Solution: Result is = -126  It’s wrong! Computer never know whether the answer is correct or not. 電腦不會知道最後的結果是對或錯, 只是按照規則來運作, 所以判斷對錯的工作, 就落在 programmer 上. 在這個例子中, 只有 D8 進位, 所以是 overflow. + -126

15 Example 6 Question: Add two numbers (-3) + (-127) = (-130) in two’s complement representation. Solution: Result is = 126  It’s wrong! Computer never know whether the answer is correct or not. 電腦不會知道最後的結果是對或錯, 只是按照規則來運作, 所以判斷對錯的工作, 就落在 programmer 上. 在這個例子中, 只有 D8 進位, 所以是 overflow. + 126

16 Overflow Overflow is an error that occurs when you try to store a number that is not within the range defined by the allocation. Two’s complement by using N bits  range: -2N-1 〜 2N-1-1 When you do arithmetic operations on numbers in a computer, remember that each number and the result should be in the range defined by the bit allocation.

17 Addition Overflow When an overflow occurs?
Let the bits of an integer be D7 D6 …… D0 . Overflow occurs: If either (D7 carries out of the integer) or (D6 carries to D7), but not both, then the answer is incorrect, else, the answer is correct. 其原理可以用數學式表示數字的 2’s complement, 再做相加來驗證. 一個數 k>0, 其 2’s complement 表示法為 binary 的 k. 一個數 k<0, 其 2’s complement 表示法為 binary 的 2^N - k. 所以當兩個負數 (-m, -n) 相加時, (-m)+(-n) = 2^N – m + 2 ^N –n= 2^N + [2^N –(m+n)]. 如果沒有 carry to D4, 一定是 error. 如果沒有 carry to D3, 不是負數, 也是 error. 所以當兩個正數 (m, n) 相加時, m+n. 不會有 carry to D4. 但是如果 carry to D3, 變成負數, 也是 error. 既沒有 carry to D4, 也沒有 carry to D3, 一定是兩個很小的正數相加. 有 carry to D4 也有 carry to D3, 一定是兩個很小的負數相加. 或者是一正數與一負數相加, 都不會超出範圍.

18 Two’s Complements Format
To store an negative integer -K in N bits of two’s complement format: 1. Find the one’s complement of -K. 2. Add 1 to its one’s complement. The above operation equals to the arithmetic operation 2N-K Decimal:  K=6:  1’s complement  add 1 to it  2’s complement 從另一個角度看, 當一個數值 (-k <0 )時, 其 2’s complement 表示法為 2^(N) -k , 其原因是做 complement, 就好像用 (=255)減去 k 的 binary, 最後再加上 1. 1 28 6 -6

19 Idea of Overflow (1/3) Add two numbers n and m in 2’s complement in 8-bit allocation –27  n, m  27 -1 Two positive numbers plus together m+n m>0, n>0  D7 of m = D7 of n=0 No D7 carry out. m+n≧27  a overflow occurs  D6 carries to D7. m+n<27  the sum is correct  D6 does not carry to D7.

20 Idea of Overflow (2/3) Two negative numbers plus together
(-m)+(-n) = (28 – m) + (2 8 –n)= 28 + [28 –(m+n)] m>0, n>0  D7 of m = D7 of n =1 D7 must carry out and the carry is ignored. m+n>27  -(m+n)<-27  a overflow occurs  D6 does not carry to D7 (28 –(m+n)<27) m+n27  0>-(m+n)>-27  the sum is correct  D6 carries to D7 (28 –(m+n) > 27)

21 Idea of Overflow (3/3) A positive number m plus a negative number –n equal to m-n = -(n-m) If 128>m>n>0  128>m+(-n)>0  the sum is correct  128> m+(28 –n)= 28 + (m-n) >0 D7 must carry out and the carry is ignored. D6 must carry to D7 (such that D7 can carry out). If 128>n>m>0  0>-(n-m)>-128  the sum is correct  -128<m+(-n) = m+(28 –n)= 28-(n-m) <0 No D7 carry out (28 -(n-m) < 28). D6 does not carry to D7(otherwise, D7 will carry out since D7 of n =1).

22 Question 1 8-bit 2’s complement representation
Tell us whether it is overflow or not Add two numbers =-15 in two’s complement representation. Add two numbers =35 in two’s complement representation. Add two numbers -105+(-30)=-135 in two’s complement representation. Add two numbers =145 in two’s complement representation. Answer: 1, 2 are correct, 3,5 are incorrect. -45= , 30= , 80= -105= , -30= , 45= , 100= -45+30= (no carry) -45+80= (both carry) -105+(-30)= (only D7 carries to D8) 45+100= (only D6 carries to D7)

23 Subtraction - + A - B = A + (-B) Find the 2’s complement of -B
Add A and (-B) 100 (4) (1) 11 (4) (-1) 0011 - + 3

24 Example 7 Question: Subtract 62 from 101 in two’s complement representation. Solution: 101-62= (-62) Result is = 39 Carry to leftmost bit  discard + 39 Carry

25 Arithmetic Operations on Floating-point Numbers
Check the signs. The same: add the number and assign the sign to the result. Different: compare the absolute values, subtract the smaller from the larger, and use the sign of the larger for the result. Move the decimal points to make the exponents the same. Add or subtract the mantissas. Normalize the result. Check for any overflow. Floating-point 的加減法原理與 2’s complement 相同, 但要做較多個工作. 先要比較兩個數的正負號, 相同則將數值相加, 並使用相同的 sign. 若不相同, 則以絕對值大者的 sign 決定最後的正負號, 數值則是絕對值大者減去絕對值小者. 相加或相減時, 先要將兩個數調到擁有相同的 exponent. 再將 mantissa 相加或相減. 做 normalization. 檢查是否有 overflow.

26 Question 2 Format: Add two numbers in floating point representation. Add two numbers in floating point representation. -5.5 = = 1.75 = 1.11 = 1.75+(-5.5) = = 6.5 = = -0.25 = = 6.5+(-0.25) = 6.25 =

27 Example 8 +25  Question: Add two floats: Solution: Make the exponents the same. (+25 x )+ (+25 x )  +25 x Normalization: +26 x It is stored as: +23 

28 Example 9 (1/2) Question: Show how the computer finds the result of (+5.75) + ( ) = ( ). Solution:

29 Example 9 (2/2) The result is ( )2 × 2134−127 = ( )2 =

30 Example 10 (1/2) Question: Show how the computer finds the result of (+5.75)+(− ) = − Solution:

31 Example 10 (2/2) The result is R=-2 127−127 ×1.0100011 = -1.2734375.
The result is R=-2 127−127 × =

32 Section 4.2 Logical Operations
Section 4.1 in Version 2 Boolean Algebra in Logic Circuit Design Appendix E

33 Logical Operations (1/2)
A bit can be represent a logic value 0: false 1: true You can provide logic operations at bit level (a bit) or at pattern level (a byte). Unary Operator Input 1 Binary Operator 由於每個 bit 有 0 / 1 兩個狀態, 所以可以用於表示 logic 的真假. 通常我們會令 0 代表 false, 1 代表 true. 在現實生活的運用上, 如果我們想要表示一個電燈的開關狀態, 通常會用 0 代表 off, 1 代表 on. 如此就可以由看一個 bit 得到電燈是否亮著, 可以用電腦設定一個 bit=1, 再接上一些控制電路, 去打開電燈. 對於這些代表邏輯值的 bits, 我們可以在上面做運算, 產生新的邏輯值. 這樣我們就可以把較複雜的計算(如要開 這幾盞燈就好, 或由外界天色的狀況決定要開幾盞燈)交給電腦運算, 在令控制電路來進行. 也可以平時做的推理搬到電腦上讓電腦去做. 例如我們已知 Mary watches TV if today is Sunday and she has finished her homework. 那麼想知道今天不是星期天, Mary 已完成功課, Mary 有沒有看電視? P:Mary watches TV. Q:Today is Sunday. R:Mary has finished her homework 答案是 P=Q AND R. 因 Q=0, R=1, 所以 P=0, Mary 沒有看電視. Logic operation 分成兩種: unary operator: 只有 one input, one output; binary operator: 有 two inputs, one output. Input Output Output Input 2 a. Unary operations b. Binary operations

34 Logical Operations (2/2)
Unary Binary NOT AND OR XOR

35 NOT NOT (A) means that We use notation A or A to denote NOT (A)
Truth Table A NOT (A) NOT (A) means that NOT inverts bits. NOT changes 0 to 1, and 1 to 0. We use notation A or A to denote NOT (A) Truth table is one way to show the result of a logical operations. Truth table lists all the possible input combinations with the corresponding output. Boolean variable can be 0 or 1 True table 是用來表示 logic operation 結果的一種方式

36 Example 7 Question: Use the NOT operator on the bit pattern 10011000
Solution: NOT Result: Truth Table A NOT (A)

37 AND AND (A,B) means that AND(A,B) = A•B = A∧B Inherent rule of AND:
Truth Table A B AND (A,B) AND (A,B) means that The result is 1 iff both bits are 1. AND(A,B) = A•B = A∧B Inherent rule of AND: X is any logic value. 0 AND X = 0 1 AND X = X

38 Example 8 Question: Use the AND operator on the bit patterns and Solution: AND Result: Truth Table A B AND (A,B)

39 OR OR (A,B) means that OR(A,B) = AB = A∨B Inherent rule of OR:
Truth Table A B OR(A,B) OR (A,B) means that The result is 0 iff both bits are 0. OR(A,B) = AB = A∨B Inherent rule of OR: X is any logic value. 1 OR X = 1 0 OR X = X

40 Example 9 Question: Use the OR operator on the bit patterns and Solution: OR Result: Truth Table A B OR(A,B)

41 XOR XOR (A,B) means that XOR(A,B) = AB Inherent rule of XOR:
Truth Table A B XOR(A,B) XOR (A,B) means that The result is 0 iff both bits are equal. XOR(A,B) = AB Inherent rule of XOR: X is any logic value. 1 XOR X = NOT (X) 0 XOR X = X X XOR Y = [ X AND (NOT Y) ] OR [ Y AND (NOT X) ] Today is Sunday or Mary has finished her homework => Mary does not watch TV Today is Monday or Mary has finished her homework => Mary watches TV Today is Sunday or Mary has not finished her homework => Mary watches TV Today is Monday or Mary has not finished her homework => Mary does not watch TV

42 Example 10 Question: Use the XOR operator on the bit patterns and Solution: XOR Result: Truth Table A B XOR(A,B)

43 Question 3 A=0010 0100, B=1010 0101. Show that AND(A,B), NOT(A)
A= , B= Show that OR(A,B), XOR(A,B) AND(A,B)= Not(A)= OR(A,B)= XOR(A,B)=

44 When Use Logic Operation
We use a bit to represent a state of a device. Ex: a light is on (bit=1) vs. a light is off (bit=0) A byte has 8 bits and represents the states of 8 devices. The logic operation usually is performed on a byte, i.e., at pattern level. How to set a bit to 1 among a byte? How to clear a bit to 0 among a byte? How to reverse a bit among a byte?

45 Applications of Logical Operations
Logical operations can be used to modify a bit pattern: Unset (clear) : force to 0  AND Set: force to 1  OR Reverse specific bits (flip)  XOR The bit pattern used to modify the target is called mask. AND 可以讓特定的一些 bit 清為 0 (clear, unset, force to 0). OR 可以讓特定的一些 bit 設為 0 (set, force to 1). XOR 可以讓特定的一些 bit 做 reverse, 1 變 0, 0 變 1 (reverse, flip). Target Binary Operator Output Mask

46 Example 11 Question: Use a mask to unset (clear) the 5 leftmost bits of a pattern. Test the mask with the pattern Solution: AND Result: X X AND AND 1 X

47 Example 12 Question: Imagine a power plant that pumps water to a city using eight pumps. The state of the pumps (on or off) can be represented by an 8-bit pattern. For example, the pattern shows that pumps 1 to 3 (from the right), 7 and 8 are on while pumps 4, 5, and 6 are off. Now assume pump 7 shuts down. How can a mask show this situation? Solution: AND Result 想像發電機使用 8 個幫浦抽水到城市中. 這 8 個幫浦的狀態 (1:on or 0:off) 由右而左用 8 個 bits 來表示. 例如 表示幫浦 1,2,3,7,8 正在使用, 幫浦 5,6 關起來. 現在要將第 7 個幫浦關掉, 應如何用 mask 做 AND operation, 使得 7 號關閉卻不會影響到其他幫浦的運作?

48 Example 13 Question: Use a mask to set the 5 leftmost bits of a pattern. Test the mask with the pattern Solution: OR Result: X X OR OR 1 X

49 Example 14 Question: Using the power plant example, how can you use a mask to show that pump 6 is now turned on? Solution: OR Result

50 Example 15 Question: Use a mask to flip the 5 leftmost bits of a pattern. Test the mask with the pattern Solution: XOR Result: X X XOR XOR 1 X X

51 Logic Gates (1/2) How the logical operation is performed on the computer? Answer is logic gates. We use specially graphic notation to represent these logic gates. Input Output 我們會用一些圖來表示 logical operators. 好處是當我們想用 logical operators 建構比較大的系統時, 圖像會比數學式容易明瞭各個 operators 間的關連, 進而了解系統的運作. A NOT (A)

52 Logic Gates (2/2)

53 Half Adder Two input variables: x, y
Two output variables: C (carry), S (sum)

54 Full Adder The third input z: carry from the last stage

55 Section 4.3 Shift Operations
Section 4.2 in Version 2 以下要探討不同型態資料如何用 bit patterns 來表示.

56 Shift Right Operations
The right-shift operation discards the rightmost bit, shifts every bit to the right, and inserts 0 as the leftmost bit. Action: Divide by 2 h is discarded. 由於某些表示法 leftmost bit 代表 sign, 所以要注意 shift right 與 shift left 若要用於數學運算, 都只能用在 unsigned number.

57 Shift Left Operations The left-shift operation discards the leftmost bit, shifts every bit to the left, and inserts 0 as the rightmost bit. Action: Multiple by 2 (Note: it is not always valid.) a is usually pushed into a carry bit CY. a

58 Rotate Operations Circular shift operations shifts, but no bit is lost or added.

59 Example 16 Question: Show how you can divide or multiply a number by 2 using shift operations. Solution: A bit pattern represents an unsigned number. 59 =  =29 shift to the right 59 =  =118 shift to the left 重點是不要 overflow, otherwise, 不管是 unsigned integer, 還是 two’s complement, 都會有乘以 2 或除以 2 的效果.

60 Example 17 Question: Use a circular left shift operation on the bit pattern Solution: The leftmost bit is circulated and becomes the rightmost bit.

61 Example 18 Question: Use an arithmetic left shift operation on the bit pattern (a) (b) The pattern is an integer in two’s complement format. Solution: (a) -39 =  = -78 shift to the left (multiple of 2) (b) 127 =  = -2 shift to the left (invalid)

62 Example 19 Question: Use a combination of logical and shift operations to find the value (0 or 1) of the fourth bit (from the right). Solution: 1. Target abcd efgh AND 0000 e000 2. Shift to right: 0000e00000000e00000000e0 e 3. Check it is 0 or 1. 先用 AND 保留下第 4 個 bit. 再做 3 次 shift to right. 再檢查其值為 0 或 1, 就可得到結果. 答案不只有一種!

63 Section 4.4 Key Terms

64 Key Terms (1/3) How many terms can you describe? XOR operator
AND operator NOT operator OR operator Arithmetic operation Logical operation Binary operation Binary operator

65 Key Terms (2/3) How many terms can you describe? Carry
Floating-point number Mantissa Mask Overflow Truth table Flip Clear Force to 0

66 Key Terms (3/3) How many terms can you describe? Set Force to 1
Two’s complement Unary Operation Unary operator Unset

67 Section 4.5 Summary

68 Summary (1/3) You can perform arithmetic or logical operations on bits. Most computer use the two’s complement method of integer representation. If there is a carry after addition of the leftmost digits, the carry is discarded. To subtract in two’s complement, just negate the number to be subtracted and add. Numbers to be added must be within the range defined by the bit allocated.

69 Summary (2/3) The term overflow describes a condition in which a number is not within the range defined by the bit allocation. Logical operation on bits can be unary (one input) or binary (two inputs). The unary NOT operator inverts its input. The result of the binary AND operation is true only if both input are true. The result of the binary OR operation is false only if both input are false. The result of the binary XOR operation is false only if both input are the same.

70 Summary (3/3) A mask is a bit pattern that is applied to a target bit pattern to achieve a specific result. To unset (clear) a bit in a target bit pattern, set the corresponding mask bit to 0 and use the AND operation. To set a bit in a target bit pattern, set the corresponding mask bit to 1 and use the OR operation. To flip a bit in a target bit pattern, set the corresponding mask bit to 1 and use the XOR operation.

71 Homework Review Questions: 4,5 Multiple-Choice Questions: 20-39
Exercises: 40 (b), 44 (d), 56, 59, 45 (single precision, ) Why and when does an overflow occur for the addition of two integers in 2’s complement representation?


Download ppt "Chapter 4 Operations on Bits"

Similar presentations


Ads by Google