230 likes | 389 Vues
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.
E N D
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
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
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
AND Function • A convenient way to represent the behavior of logical operation is the truth table • A truth table has n+1 columns and 2nrows • 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
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
AND Function • IF c=a AND b where a=0011101001101001 and b=0101100100100001, what is c? a: 0011101001101001 b: 0101100100100001 c: 0001100000100001
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?
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
AND Function • If A=01010110, A: 01010110 Bitmask: 00000011 00000010 • If A=11111100, A: 11111100 Bitmask: 00000011 00000000
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
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?
OR Function a: 0011101001101001 b: 0101100100100001 c: 0111101101101001
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
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
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
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!