foundations of computer science chapter 4 operations on bits

53
Foundations of Computer Science Chapter 4 Operations on Bits

Upload: clarence-arnold

Post on 31-Dec-2015

229 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Foundations of Computer Science Chapter 4 Operations on Bits

Foundations of Computer Science

Chapter 4

Operations on Bits

Page 2: Foundations of Computer Science Chapter 4 Operations on Bits

2

Outline

4.1 Arithmetic Operations4.1 Arithmetic Operations

4.2 Logical Operations4.2 Logical Operations

4.3 Shift Operations4.3 Shift Operations

Page 3: Foundations of Computer Science Chapter 4 Operations on Bits

3

Operations on Bits

You can perform arithmetic or logical operations on bits

Bit operations(位元運算 )

Arithmetic(算數 )

Logical(邏輯 )

Page 4: Foundations of Computer Science Chapter 4 Operations on Bits

4

4.1ArithmeticOperations

4.1ArithmeticOperations

Page 5: Foundations of Computer Science Chapter 4 Operations on Bits

5

Adding Bits

• Most computer use the two’s complement method of integer representation

• Adding numbers in two’s complement is like adding the number in decimal, if there is a carry, it is added to the next column

• If there is a carry after addition of the leftmost digits, the carry is discarded

Number of 1sNumber of 1s------------

NoneOneTwo

Three

ResultResult------------

0101

CarryCarry------------

11

Page 6: Foundations of Computer Science Chapter 4 Operations on Bits

6

Rule of Adding Integers in Two's Complement

• Add 2 bits and propagate the carry to the next column. If there is a final carry after the left most column addition, discard it.

Page 7: Foundations of Computer Science Chapter 4 Operations on Bits

7

Example

Add two numbers in two’s complement representation: (+17) + (+22) (+39)

SolutionSolution

Carry 10 0 0 1 0 0 0 1 ++0 0 0 1 0 1 1 0----------------------------------

Result 0 0 1 0 0 1 1 1 39

Page 8: Foundations of Computer Science Chapter 4 Operations on Bits

8

Example

Add two numbers in two’s complement representation: (+24) + (-17) (+7)

SolutionSolution

Carry 1 1 1 1 10 0 0 1 1 0 0 0 ++1 1 1 0 1 1 1 1----------------------------------

Result 0 0 0 0 0 1 1 1 +7

Page 9: Foundations of Computer Science Chapter 4 Operations on Bits

9

Example

Add two numbers in two’s complement representation: (-35) + (+20) (-15)

SolutionSolution

Carry 1 1 11 1 0 1 1 1 0 1 ++0 0 0 1 0 1 0 0----------------------------------

Result 1 1 1 1 0 0 0 1 -15

Page 10: Foundations of Computer Science Chapter 4 Operations on Bits

10

Example

Add two numbers in two’s complement representation: (+127) + (+3) (+130)

SolutionSolution

Carry 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 ++ 0 0 0 0 0 0 1 1 ----------------------------------

Result 1 0 0 0 0 0 1 0 -126-126 (Error)(Error) An overflow has occurred. An overflow has occurred.

Page 11: Foundations of Computer Science Chapter 4 Operations on Bits

11

Overflow (溢位 )

• The term overflow describes a condition in which a number is not within the range defined by the bit allocation

• For example4, the range is -28-1 to +28-1-1, which is -128 to 127. The result of the addition (130) is not in this range

Page 12: Foundations of Computer Science Chapter 4 Operations on Bits

12

Note

Range of numbers in two’s complementrepresentation

- (2N-1) ---------- 0 ----------- +(2N-1 –1)

Page 13: Foundations of Computer Science Chapter 4 Operations on Bits

13

Two’s Complement Numbers Visualization

Page 14: Foundations of Computer Science Chapter 4 Operations on Bits

14

Note

• 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 (配置 )

Page 15: Foundations of Computer Science Chapter 4 Operations on Bits

15

Subtraction in Two's Complement

• To subtract in two‘s complement, just negate (two’s complement, 指使用二的補數對數值進行變更正負號 ) the second number to be subtracted and add

X – Y = X + (-Y)

Page 16: Foundations of Computer Science Chapter 4 Operations on Bits

16

Example

Subtract 62 from 101 in two’s complement: (+101) - (+62) (+101) + (-62)

SolutionSolution

Carry 1 10 1 1 0 0 1 0 1 ++1 1 0 0 0 0 1 0----------------------------------

Result 0 0 1 0 0 1 1 1 39The leftmost carry is discarded.

Page 17: Foundations of Computer Science Chapter 4 Operations on Bits

17

Arithmetic on Floating-Point Number

• The steps are as follows:– Check the signs

• If the signs are the same, add the numbers and assign (分配 ) the sign to the result

• If the signs are 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 before storing in memory– Check for any overflow

Page 18: Foundations of Computer Science Chapter 4 Operations on Bits

18

Example

Add two floats:0 10000100 101100000000000000000000 10000010 01100000000000000000000

SolutionSolution

The exponents are 5 and 3. The numbers are:+25 * 1.1011 and +23 * 1.011Make the exponents the same.(+25 * 1.1011) + (+25 * 0.01011) +25 * 10.00001After normalization +26 * 1.000001, which is stored as:0 10000101 000001000000000000000000

Page 19: Foundations of Computer Science Chapter 4 Operations on Bits

19

4.2LogicalOperations

4.2LogicalOperations

Page 20: Foundations of Computer Science Chapter 4 Operations on Bits

20

Unary and Binary Operations

• Logical operation on bits can be unary (one input, 單一 ) or binary (two inputs)

Page 21: Foundations of Computer Science Chapter 4 Operations on Bits

21

Logical Operations

Page 22: Foundations of Computer Science Chapter 4 Operations on Bits

22

Truth Tables (真值表 )

Page 23: Foundations of Computer Science Chapter 4 Operations on Bits

23

NOT Operator

The unary NOT operator inverts (反轉 ) its input.

Page 24: Foundations of Computer Science Chapter 4 Operations on Bits

24

NOT Operator

x NOT x

Page 25: Foundations of Computer Science Chapter 4 Operations on Bits

25

Example

Use the NOT operator on the bit pattern 10011000

SolutionSolution

Target 1 0 0 1 1 0 0 0 NOT------------------

Result 0 1 1 0 0 1 1 1

Page 26: Foundations of Computer Science Chapter 4 Operations on Bits

26

AND Operator

The result of the binary AND operation is true only ifboth inputs are true.

Page 27: Foundations of Computer Science Chapter 4 Operations on Bits

27

AND Operator

x

yx AND y

Page 28: Foundations of Computer Science Chapter 4 Operations on Bits

28

Example

Use the AND operator on bit patterns 10011000 and 00110101

SolutionSolution

Target 1 0 0 1 1 0 0 0 AND0 0 1 1 0 1 0 1------------------

Result 0 0 0 1 0 0 0 0

Page 29: Foundations of Computer Science Chapter 4 Operations on Bits

29

Inherent Rule of the AND Operator

If a bit in one input is 0, you do not have to check thecorresponding bit in the other input → the result is 0

Page 30: Foundations of Computer Science Chapter 4 Operations on Bits

30

OR Operator

The result of the binary OR operation is false only ifboth inputs are false.

Page 31: Foundations of Computer Science Chapter 4 Operations on Bits

31

OR Operator

x

yx OR y

Page 32: Foundations of Computer Science Chapter 4 Operations on Bits

32

Example

Use the OR operator on bit patterns 10011000 and 00110101

SolutionSolution

Target 1 0 0 1 1 0 0 0 OR0 0 1 1 0 1 0 1------------------

Result 1 0 1 1 1 1 0 1

Page 33: Foundations of Computer Science Chapter 4 Operations on Bits

33

Inherent Rule of the OR Operator

If a bit in one input is 1, you do not have to check thecorresponding bit in the other input → the result is 1

Page 34: Foundations of Computer Science Chapter 4 Operations on Bits

34

XOR Operator

The result of the binary XOR operation is false only ifboth input are the same.XOR: eXclusive (獨占的 ) OR

Page 35: Foundations of Computer Science Chapter 4 Operations on Bits

35

XOR Operator

x

yx XOR y

Page 36: Foundations of Computer Science Chapter 4 Operations on Bits

36

Example

Use the XOR operator on bit patterns 10011000 and 00110101

SolutionSolution

Target 1 0 0 1 1 0 0 0 XOR0 0 1 1 0 1 0 1------------------

Result 1 0 1 0 1 1 0 1

Page 37: Foundations of Computer Science Chapter 4 Operations on Bits

37

Inherent Rule of the XOR Operator

If a bit in one input is 1, the result is the inverse of thecorresponding bit in the other input

Page 38: Foundations of Computer Science Chapter 4 Operations on Bits

38

Mask (遮罩 )

A mask is a bit pattern that is applied to a target bit pattern to achieve a specific result.

Page 39: Foundations of Computer Science Chapter 4 Operations on Bits

39

Unsetting Specific Bits

Unset (清除 ) : force (強迫 ) to 0

Page 40: Foundations of Computer Science Chapter 4 Operations on Bits

40

Example

Use a mask to unset (clear, 清除 ) the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110

SolutionSolution

The mask is 00000111.Target 1 0 1 0 0 1 1 0 ANDMask 0 0 0 0 0 1 1 1

------------------Result 0 0 0 0 0 1 1 0

Page 41: Foundations of Computer Science Chapter 4 Operations on Bits

41

Example

Imagine a power plant (發電廠 ) that pumps (幫浦 ) water to a city using eight (8) 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?

Page 42: Foundations of Computer Science Chapter 4 Operations on Bits

42

Solution

Use the mask 10111111 to AND with the target pattern. The only 0 bit (bit 7) in the mask turns off the seventh bit in the target.Target 1 1 0 0 0 1 1 1 ANDMask 1 0 1 1 1 1 1 1

------------------Result 1 0 0 0 0 1 1 1

Page 43: Foundations of Computer Science Chapter 4 Operations on Bits

43

Setting Specific Bits

Set (設定 ) : force (強迫 ) to 1

Page 44: Foundations of Computer Science Chapter 4 Operations on Bits

44

Example

Use a mask to set the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110

SolutionSolution

The mask is 11111000.Target 1 0 1 0 0 1 1 0 ORMask 1 1 1 1 1 0 0 0

------------------Result 1 1 1 1 1 1 1 0

Page 45: Foundations of Computer Science Chapter 4 Operations on Bits

45

Example

Using the power plant (發電廠 ) example, how can you use a mask to to show that pump 6 is now turned on (pump’s status 11000111)?

SolutionSolution

Use the mask 00100000.Target 1 0 0 0 0 1 1 1 ORMask 0 0 1 0 0 0 0 0

------------------Result 1 0 1 0 0 1 1 1

Page 46: Foundations of Computer Science Chapter 4 Operations on Bits

46

Flipping Specific Bits

To change the value of specific bits from 0s to 1s,and vice versa (反之亦然 , 1s to 0s)

Flip (翻轉 ) : 0 to 1, and 1 to 0

Page 47: Foundations of Computer Science Chapter 4 Operations on Bits

47

Example

Use a mask to flip the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110

SolutionSolution

Target 1 0 1 0 0 1 1 0 XORMask 1 1 1 1 1 0 0 0

------------------Result 0 1 0 1 1 1 1 0

Page 48: Foundations of Computer Science Chapter 4 Operations on Bits

48

4.3Shift Operations

4.3Shift Operations

Page 49: Foundations of Computer Science Chapter 4 Operations on Bits

49

Shift Operations

• A bit pattern can be shifted (位移 ) to the right or to the left.

• The right-shift operation discards the rightmost bit, shifts every bit to the right, and inserts 0 as the leftmost bit.

Page 50: Foundations of Computer Science Chapter 4 Operations on Bits

50

Example

Show how you can divide ( 除 ) or multiply ( 乘 ) a number by 2 using shift operations

SolutionSolution

If a bit pattern represents an unsigned number, a right-shift operation divides ( 除 ) the number by two. The pattern 00111011 represents 59. When you shift the number to the right, you get 00011101, which is 29. A left-shift operation multiply ( 乘 ) the number by two. If you shift the original number to the left, you get 01110110, which is 118.

Page 51: Foundations of Computer Science Chapter 4 Operations on Bits

51

Example

Use a combination ( 結合 ) of logical and shift operations to find the value (0 or 1) of the fourth bit (from the right)

SolutionSolution

Use the mask 00001000 to AND with the target to keep the fourth bit and clear the rest of the bits.

Continued on the next slide

Page 52: Foundations of Computer Science Chapter 4 Operations on Bits

52

Solution (Continued)

Target a b c d e f g h ANDMask 0 0 0 0 1 0 0 0

------------------Result 0 0 0 0 e 0 0 0Shift the new pattern three times to the right

0000e000 00000e00 000000e0 0000000e

Now it is easy to test the value of the new pattern as an unsigned integer. If the value is 1, the original bit was 1; otherwise the original bit was 0.

Page 53: Foundations of Computer Science Chapter 4 Operations on Bits

53

The End