bits, data types, and operations: chapter 2

23
Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP 2610 1

Upload: kiona-burke

Post on 02-Jan-2016

51 views

Category:

Documents


2 download

DESCRIPTION

1. Bits, Data types, and Operations: Chapter 2. COMP 2610. Dr. James Money COMP 2610. Operations on Bits. So far, we have seen we can perform addition and subtraction on binary patterns Recall the meaning of ALU – arithmetic and logic unit The other set of operations is logical operations. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Bits, Data types, and Operations: Chapter 2

Bits, Data types, and Operations: Chapter 2

COMP 2610Dr. James Money

COMP 2610

1

Page 2: Bits, Data types, and Operations: Chapter 2

Operations on Bits

So far, we have seen we can perform

addition and subtraction on binary patterns

Recall the meaning of ALU – arithmetic and

logic unit

The other set of operations is logical

operations

Page 3: Bits, Data types, and Operations: Chapter 2

Operations on Bits

Recall that the name logical is historical in origin

It refers to the fact that a bit has two values 0

and 1

These refer to false and true, respectively

We consider several basic logical functions of

the ALU

Page 4: Bits, Data types, and Operations: Chapter 2

AND Function

AND is a binary logical function

It takes two source operands, and produces one

result

Each source is a logical values, either 0 or 1

The output of AND is 1 only if both the source

values are 1

Otherwise the output is 0

Page 5: Bits, Data types, and Operations: Chapter 2

AND Function

A convenient way to represent the behavior of

logical operation is the truth table

A truth table has n+1 columns and 2n rows

The n columns refer to the source operands and

the +1 refers to the output

Each value has two possible values, so there

are 2n choices

Page 6: Bits, Data types, and Operations: Chapter 2

AND Function

A B AND

0 0 0

0 1 0

1 0 0

1 1 1

Page 7: Bits, Data types, and Operations: Chapter 2

AND Function

AND 1 0

1 1 0

0 0 0

AND True False

True True False

False False False

Page 8: Bits, Data types, and Operations: Chapter 2

AND Function

We can also apply the AND operation to two

bits patterns of m bits each

We apply the AND function to each pair of

bits in the two source operands

This operation is called a bitwise AND

Page 9: Bits, Data types, and Operations: Chapter 2

AND Function

IF c=a AND b where a=0011101001101001

and b=0101100100100001, what is c?

a: 0011101001101001

b: 0101100100100001

c: 0001100000100001

Page 10: Bits, Data types, and Operations: Chapter 2

AND Function

Suppose we have an 8 bit pattern called A in

which only the two right-most bits are

significant

The computer will do one of four tasks

depending on the value of these two bits

How do we isolate these two bits?

Page 11: Bits, Data types, and Operations: Chapter 2

AND Function

We can use a bitmask to get this value

The bitmask should be 1 for the bits you are

interested in and 0 elsewhere

So we would use the bitmask 00000011

Then we apply the A AND bitmask

Page 12: Bits, Data types, and Operations: Chapter 2

AND Function

If A=01010110,A: 01010110

Bitmask: 00000011

00000010

If A=11111100,A: 11111100

Bitmask: 00000011

00000000

Page 13: Bits, Data types, and Operations: Chapter 2

OR Function

OR is also a binary logical function

It requires two source operand and produces one

output

The output of OR is only 0 if both inputs are 0

Otherwise, it is 1

We can apply the OR operation to m bits the

same as the AND function

Page 14: Bits, Data types, and Operations: Chapter 2

OR Function

A B OR

0 0 0

0 1 1

1 0 1

1 1 1

Page 15: Bits, Data types, and Operations: Chapter 2

OR Function

OR 1 0

1 1 1

0 1 0

AND True False

True True True

False True False

Page 16: Bits, Data types, and Operations: Chapter 2

OR Function

Some times this is called the inclusive-OR

function to differentiate it from the exclusive

OR operator

Let a=0011101001101001 and

b=0101100100100001

What is c=a OR b?

Page 17: Bits, Data types, and Operations: Chapter 2

OR Function

a: 0011101001101001

b: 0101100100100001

c: 0111101101101001

Page 18: Bits, Data types, and Operations: Chapter 2

NOT Function

NOT is a unary logical function

That is, it only takes one source operand, and

outputs one result

This is also known as the complement operation

We says the output is formed by inverting the

bits

Page 19: Bits, Data types, and Operations: Chapter 2

NOT Function

A NOT

0 1

1 0

Page 20: Bits, Data types, and Operations: Chapter 2

NOT Function

We can apply the NOT function to a single m

bit pattern the same way we apply it to two m

bit patterns for AND and OR

Let c=NOT a and a=0011101001101001

a: 0011101001101001

c: 1100010110010110

Page 21: Bits, Data types, and Operations: Chapter 2

XOR Function

The Exclusive-OR or XOR function is a binary

logical function with two source operands and

one result

The output of XOR is 1 two sources are different

Otherwise, the output is 0

We can apply this to m bit patterns as well

Page 22: Bits, Data types, and Operations: Chapter 2

XOR Function

A B XOR

0 0 0

0 1 1

1 0 1

1 1 0

Page 23: Bits, Data types, and Operations: Chapter 2

XOR Function

Let a=0011101001101001,

b=01011001001000001, and find c=a XOR b

a: 0011101001101001

b: 0101100100100001

c: 0110001101001000

We can use XOR to determine if two bit patterns

are identical!