Chapter 4
This presentation is the property of its rightful owner.
Sponsored Links
1 / 34

Operations on Bits PowerPoint PPT Presentation


  • 72 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 4. Operations on Bits. 主讲:李艳. 云南农业大学. Carry ------------ 1 1. Number of 1s ------------ None One Two Three. Result ------------ 0 1 0 1. 4.1 ARITHMETIC OPERATIONS. ARITHMETIC OPERATIONS ON INTEGERS Addition in Two’s Complement

Download Presentation

Operations on Bits

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Operations on bits

Chapter 4

Operations

on Bits

主讲:李艳

云南农业大学


4 1 arithmetic operations

Carry------------

1

1

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

None

One

Two

Three

Result------------

0

1

0

1

4.1 ARITHMETIC OPERATIONS

ARITHMETIC OPERATIONS ON INTEGERS

  • Addition in Two’s Complement

    Adding numbers in two’ s complement is like adding the numbers in decimal; you add column by column, and if there is a carry, it is added to the next column. When you add 2 bits, the result is 0 or 1.

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 leftmost column addition, discard it.


Operations on bits

Example 1

Solution

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

Carry 1

0 0 0 1 0 0 0 1 +0 0 0 1 0 1 1 0

-------------------------------------Result0 0 1 0 0 1 1 1  39


Operations on bits

Example 2

Solution

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

Carry 1 1 1 1 1

0 0 0 1 1 0 0 0 +1 1 1 0 1 1 1 1

-------------------------------------Result0 0 0 0 0 1 1 1  +7


Operations on bits

Example 3

Solution

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

Carry 1 1 1

1 1 0 1 1 1 0 1 +0 0 0 1 0 1 0 0

----------------------------------Result1 1 1 1 0 0 0 1  -15


Operations on bits

Example 4

Solution

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

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(Error)An overflow has occurred.


Operations on bits

Overflow

Overflow is an error that occurs when you try to store a number that is not within the range defined by the allocation. When you add numbers in two’s complement using N bits, make sure that each number and the result are in the range defined for two’s complement representation.

Range of numbers in two’s complementrepresentation

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


Operations on bits

Two’s complement numbers visualization


Operations on bits

When you do arithmetic operations onnumbers in a computer, remember thateach number and the result should bein the range defined by the bit allocation.

  • Subtraction in Two’s Complement

    One of the advantages of two’s complement representation is that there is no difference between addition and subtraction. To subtract, negate (two’s complement) the second number and add. In other words, the following two statements are the same:

    Number 1 – Number 2  Number 1 + (-Number 2)


Operations on bits

Example 5

Solution

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

Carry 1 1

0 1 1 0 0 1 0 1 +1 1 0 0 0 0 1 0

----------------------------------Result0 0 1 0 0 1 1 1  39The leftmost carry is discarded.


4 2 logical operations

4.2 LOGICAL OPERATIONS

A single bit can be either 0 or1.You can interpret 0 as the logical value false and 1 as the logical value true.

A logical operation can accept 1 or 2 bits to create only 1 bit. If the operation is applied to only one input, it is a unary operation, if it is applied to 2 bits, it is a binary operation.

We will discuss one unary operator (NOT) and three binary operators (AND, OR, XOR).


Operations on bits

TRUTH TABLES

One way to show the result of a logical operation is with a truth table. A truth table lists all the possible input combinations with the corresponding output.


Operations on bits

Example 7

Solution

UNARY OPERATOR

NOT Operator

The NOT operator has one input (a bit pattern). It inverts bits; that is, it changes 0 to1 and 1 to 0.

Use the NOT operator on the bit pattern 10011000.

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


Operations on bits

Example 8

Solution

BINARY OPERATORS

AND Operator

It takes two inputs (two bit patterns) and creates one output (bit pattern). For each pair of input bits, the result is 1 if and only if both bits are 1; otherwise, it is 0.

Use the AND operator on bit patterns 10011000 and 00110101.

Target 1 0 0 1 1 0 0 0 AND 0 0 1 1 0 1 0 1 ------------------Result 0 0 0 1 0 0 0 0


Operations on bits

Inherent Rule of the AND Operator

One interesting point about the AND operator is that if a bit in one input is 0, you do not have to check the corresponding bit in the other input, you can quickly conclude that the result is 0.


Operations on bits

Example 9

Solution

OR Operator

It takes two inputs (two bit patterns) and creates one output (bit pattern). For each pair of input bits, the result is 0 if and only if both bits are 0; otherwise, it is 1.

Use the OR operator on bit patterns 10011000 and 00110101.

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


Operations on bits

Inherent rule of the OR Operator

One interesting point about the OR operator is that if a bit in one input is 1, you do not have to check the corresponding bit in the other input; you can quickly conclude that the result is 1.


Operations on bits

Example 10

Solution

XOR Operator

It takes two inputs (two bit patterns) and creates one output (bit pattern). For each pair of input bits, the result is 0 if and only if both bits are equal; otherwise, it is 1.

Use the XOR operator on bit patterns 10011000 and 00110101.

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


Operations on bits

Inherent Rule of the XOR Operator

One interesting point about the XOR operator is that if a bit in one input is1, the result is the inverse of the corresponding bit in the other input.


Operations on bits

APPLICATIONS

The three logical binary operations can be used to modify a bit pattern. They can unset, set, or reverse specific bits. The bit pattern to be modified is ANDed, ORed, or XORed with a second bit pattern, which is called the mask. The mask is used to modify another bit pattern.


Operations on bits

Unsetting Specific Bits

One of the applications of the AND operator is to unset (force to 0) specific bits in a bit pattern. To do so, use an unsetting mask with the same bit length.

The rules for constructing an unsetting mask can be summarized as follows:

1. To unset a bit in the target bit pattern, use 0 for the corresponding bit in the mask.

2. To leave a bit in the target bit pattern unchanged, use 1 for the corresponding bit in the mask.


Operations on bits

Example 11

Solution

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

The mask is 00000111.

Target 1 0 1 0 0 1 1 0ANDMask 0 0 0 0 0 1 1 1-------------------Result 0 0 0 0 0 1 1 0


Operations on bits

Example 12

Solution

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?

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 1ANDMask1 0 1 1 1 1 1 1-------------------Result 1 0 0 0 0 1 1 1


Operations on bits

Setting Specific Bits

One of the applications of the OR operator is to set (Force to 1) specific bits in a bit pattern. To do so, use a setting mask with the same bit length.

The rules for constructing a setting mask can be summarized as follows:

1. To set a bit in the target pattern, use 1 for the corresponding bit in the mask.

2. To leave a bit in the target bit pattern unchanged, use 0 for the corresponding bit in the mask.


Operations on bits

Example 13

Solution

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

The mask is 11111000.

Target 1 0 1 0 0 1 1 0 ORMask 1 1 1 1 1 0 0 0------------------Result1 1 1 1 11 1 0


Operations on bits

Example 14

Solution

Using the power plant example, how can you use a mask to to show that pump 6 is now turned on?

Use the mask 00100000.

Target 1 0 0 0 0 1 1 1 ORMask0 0 1 0 0 0 0 0 ------------------Result 1 0 1 0 0 1 1 1


Operations on bits

Flipping Specific Bits

One of the applications of XOR is to flip bits, which means to change the value of specific bits from 0s to 1s, and vice versa.

The rules for constructing an XOR mask can be summarized as follows:

1. To flip a bit in the target bit pattern, use 1 for the corresponding bit in the mask.

2.To leave a bit in the target bit pattern unchanged, use 0 for the corresponding bit in the mask.


Operations on bits

Example 15

Solution

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

Target 1 0 1 0 0 1 1 0XORMask 1 1 1 1 1 0 0 0 -------------------Result 0 1 0 1 1 1 1 0


4 3 shift operations

4.3 SHIFT OPERATIONS

Another common operation on bit patterns is the shift operation. A bit pattern can be shifted to the right or to the left. The right-shift operation discards the rightmost bit, shift every bit to the right, and inserts 0 as the leftmost bit. The left-shift operation discards the leftmost bit, shifts every bit to the left, and inserts 0 as the rightmost bit.

Shift operations can only be used when a pattern represents an unsigned number.


Operations on bits

Example 16

Solution

Show how you can divide or multiply a number by 2 using shift operations.

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. If you shift the original number to the left, you get 01110110, which is 118.


Operations on bits

Example 17

Solution

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

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

Target a b c d e f g hANDMask 0 0 0 0 1 0 0 0------------------Result 0 0 0 0e 0 0 0

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


Operations on bits

SUMMARY

  • You can perform arithmetic or logical operations on bits.

  • Most computers 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 allocation.

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


Operations on bits

SUMMARY (continued)

  • The result of the binary AND operation is true only if both inputs are true.

  • The result of the binary OR operation is false only if both inputs are false.

  • The result of the binary XOR operation is false only if both inputs are the same.

  • 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 operator.

  • To set a bit in a target bit pattern, set the corresponding mask bit to 1 and use the OR operator.

  • To flip a bit in a target bit pattern, set the corresponding mask bit to 1 and use the XOR operator.


Operations on bits

Thanks!


  • Login