Chapter 4 Operations on Bits

Slides:



Advertisements
Similar presentations
EET 1131 Unit 7 Arithmetic Operations and Circuits
Advertisements

©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, C++ Version, Third Edition.
Digital Fundamentals Floyd Chapter 2 Tenth Edition
Assembly Language and Computer Architecture Using C++ and Java
Signed Numbers.
Assembly Language and Computer Architecture Using C++ and Java
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
CSCE 211: Digital Logic Design Chin-Tser Huang University of South Carolina.
Lecture 3. Boolean Algebra, Logic Gates
Chapter 3 Number Representation
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
Mantıksal Tasarım – BBM231 M. Önder Efe
Operations on data CHAPTER 4.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
Chapter 3 Data Representation part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Lecture 3. Boolean Algebra, Logic Gates Prof. Sin-Min Lee Department of Computer Science 2x.
Chapter3 Fixed Point Representation Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
© 2009 Pearson Education, Upper Saddle River, NJ All Rights ReservedFloyd, Digital Fundamentals, 10 th ed Digital Fundamentals Tenth Edition Floyd.
ES 244: Digital Logic Design Chapter 1 Chapter 1: Introduction Uchechukwu Ofoegbu Temple University.
Computer Arithmetic Nizamettin AYDIN
1 CHAPTER 4: PART I ARITHMETIC FOR COMPUTERS. 2 The MIPS ALU We’ll be working with the MIPS instruction set architecture –similar to other architectures.
3-1 Chapter 3 - Arithmetic Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles of Computer Architecture.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Today’s Topics How information.
Basic Arithmetic (adding and subtracting)
Logic and Digital System Design - CS 303
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
IT253: Computer Organization
Basic Arithmetic (adding and subtracting)
Foundations of Computer Science Chapter 4 Operations on Bits.
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
1 EENG 2710 Chapter 1 Number Systems and Codes. 2 Chapter 1 Homework 1.1c, 1.2c, 1.3c, 1.4e, 1.5e, 1.6c, 1.7e, 1.8a, 1.9a, 1.10b, 1.13a, 1.19.
CSC 221 Computer Organization and Assembly Language
Operations on Bits Arithmetic Operations Logic Operations
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
Half Adder & Full Adder Patrick Marshall. Intro Adding binary digits Half adder Full adder Parallel adder (ripple carry) Arithmetic overflow.
Tutorial: ITI1100 Dewan Tanvir Ahmed SITE, UofO
Data Representation in Computer Systems. 2 Signed Integer Representation The conversions we have so far presented have involved only positive numbers.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
Outline Binary Addition 2’s complement Binary Subtraction Half Adder
1. Computing Systems Lecture 3 Binary Representation & Boolean Logic Binary and Logic 2.
1 Fundamentals of Computer Science Combinational Circuits.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
Number Representation and Arithmetic Circuits
©Brooks/Cole, 2003 Chapter 3 Number Representation.
1 Ethics of Computing MONT 113G, Spring 2012 Session 4 Binary Addition.
ECE DIGITAL LOGIC LECTURE 15: COMBINATIONAL CIRCUITS Assistant Prof. Fareena Saqib Florida Institute of Technology Fall 2015, 10/20/2015.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
BINARY SYSTEMS ENGR. KASHIF SHAHZAD 1. BINARY NUMBERS 1/2 Internally, information in digital systems is of binary form groups of bits (i.e. binary numbers)
Computing Systems Lecture 3 Binary Representation & Boolean Logic Binary and Logic 1.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
1 Integer Representations V1.0 (22/10/2005). 2 Integer Representations  Unsigned integer  Signed integer  Sign and magnitude  Complements  One’s.
Arithmetic Circuits I. 2 Iterative Combinational Circuits Like a hierachy, except functional blocks per bit.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Chapter 4 Operations on Bits.
Chapter 4 Register Transfer and Microoperations
Subtraction The arithmetic we did so far was limited to unsigned (positive) integers. Today we’ll consider negative numbers and subtraction. The main problem.
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Chapter3 Fixed Point Representation
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
1.6) Storing Integer: 1.7) storing fraction:
Presentation transcript:

Chapter 4 Operations on Bits Operations on Data on Version 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.

Outlines Arithmetic Operations Logical Operations Shift Operations Key Terms Summary Homework

Section 4.1 Arithmetic Operations Section 4.3 in Version 2

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

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.

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

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 乘法與除法的技術.

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

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

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 00000111 = 7 Correct! Carry to leftmost bit  discard 00011000 11101111 00000111 + 7 Carry

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

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

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

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

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.

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, 一定是兩個很小的負數相加. 或者是一正數與一負數相加, 都不會超出範圍.

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: -6  K=6: 0000 0110  1’s complement 1111 1001  add 1 to it  2’s complement 1111 1010 從另一個角度看, 當一個數值 (-k <0 )時, 其 2’s complement 表示法為 2^(N) -k , 其原因是做 complement, 就好像用 1111 1111 (=255)減去 k 的 binary, 最後再加上 1. 00000000 00000110 11111010 - 1 28 6 -6

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.

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)

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).

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

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

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

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.

Question 2 Format: 0 000 0000 Add two numbers 0011 1100 + 1101 0110 in floating point representation. Add two numbers 0101 1010 + 1001 0000 in floating point representation. -5.5 = -101.1 = 1 101 0110 1.75 = 1.11 = 0 011 1100 1.75+(-5.5) = -3.75 = 1 100 1110 6.5 = 110.1 = 0 101 1010 -0.25 = -0.01 = 1 001 0000 6.5+(-0.25) = 6.25 = 0 101 1001

Example 8 +25  1.1011 Question: Add two floats: 0 10000100 10110000000000000000000 0 10000010 01100000000000000000000 Solution: Make the exponents the same. (+25 x 1.1011)+ (+25 x 0.01011)  +25 x 10.00001 Normalization: +26 x 1.000001 It is stored as: 0 10000101 000001000000000000000000 +23  1.011

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

Example 9 (2/2) 10000111 0000010 111000000000000000 The result is (1.0100111101)2 × 2134−127 = (10100111.101)2 = 167.625.

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

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

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

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, 我們可以在上面做運算, 產生新的邏輯值. 這樣我們就可以把較複雜的計算(如要開 1 3 5 這幾盞燈就好, 或由外界天色的狀況決定要開幾盞燈)交給電腦運算, 在令控制電路來進行. 也可以平時做的推理搬到電腦上讓電腦去做. 例如我們已知 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

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

NOT NOT (A) means that We use notation A or A to denote NOT (A) Truth Table A NOT (A) 0 1 1 0 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 結果的一種方式

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

AND AND (A,B) means that AND(A,B) = A•B = A∧B Inherent rule of AND: Truth Table A B AND (A,B) 0 0 0 0 1 0 1 0 0 1 1 1 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

Example 8 Question: Use the AND operator on the bit patterns 10011000 and 00110101. Solution: 1001 1000 AND 0011 0101 0001 0000 Result: 00010000 Truth Table A B AND (A,B) 0 0 0 0 1 0 1 0 0 1 1 1

OR OR (A,B) means that OR(A,B) = AB = A∨B Inherent rule of OR: Truth Table A B OR(A,B) 0 0 0 0 1 1 1 0 1 1 1 1 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

Example 9 Question: Use the OR operator on the bit patterns 10011000 and 00110101. Solution: 1001 1000 OR 0011 0101 1011 1101 Result: 1011 1101 Truth Table A B OR(A,B) 0 0 0 0 1 1 1 0 1 1 1 1

XOR XOR (A,B) means that XOR(A,B) = AB Inherent rule of XOR: Truth Table A B XOR(A,B) 0 0 0 0 1 1 1 0 1 1 1 0 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

Example 10 Question: Use the XOR operator on the bit patterns 10011000 and 00110101. Solution: 1001 1000 XOR 0011 0101 1010 1101 Result: 1010 1101 Truth Table A B XOR(A,B) 0 0 0 0 1 1 1 0 1 1 1 0

Question 3 A=0010 0100, B=1010 0101. Show that AND(A,B), NOT(A) A=1011 1110, B=0111 1101. Show that OR(A,B), XOR(A,B) AND(A,B)=0010 0100 Not(A)= 1101 1011 OR(A,B)=1111 1111 XOR(A,B)=1100 0011

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?

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

Example 11 Question: Use a mask to unset (clear) the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution: 1010 0110 AND 0000 0111 0000 0110 Result: 00000110 X X AND 0 AND 1 0 X

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 11000111 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: 1100 0111 AND 1011 1111 Result 1000 0111 想像發電機使用 8 個幫浦抽水到城市中. 這 8 個幫浦的狀態 (1:on or 0:off) 由右而左用 8 個 bits 來表示. 例如 11000111 表示幫浦 1,2,3,7,8 正在使用, 幫浦 5,6 關起來. 現在要將第 7 個幫浦關掉, 應如何用 mask 做 AND operation, 使得 7 號關閉卻不會影響到其他幫浦的運作?

Example 13 Question: Use a mask to set the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution: 1010 0110 OR 1111 1000 1111 1110 Result: 1111 1110 X X OR 0 OR 1 X 1

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

Example 15 Question: Use a mask to flip the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution: 1010 0110 XOR 1111 1000 0101 1110 Result: 0101 1110 X X XOR 0 XOR 1 X X

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)

Logic Gates (2/2)

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

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

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

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.

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

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

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 = 00111011  00011101=29 shift to the right 59 = 00111011  01110110=118 shift to the left 重點是不要 overflow, otherwise, 不管是 unsigned integer, 還是 two’s complement, 都會有乘以 2 或除以 2 的效果.

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

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

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 1000 0000 e000 2. Shift to right: 0000e00000000e00000000e00000000e 3. Check it is 0 or 1. 先用 AND 保留下第 4 個 bit. 再做 3 次 shift to right. 再檢查其值為 0 或 1, 就可得到結果. 答案不只有一種!

Section 4.4 Key Terms

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

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

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

Section 4.5 Summary

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.

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.

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.

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