Ch-04 Operation On Data (Compatibility Mode)
Ch-04 Operation On Data (Compatibility Mode)
4
Operations
On Data
QF 7.01/REV01
Previous Lecture…..
• There are 5 data types stored in a computer (number, text, audio, image
and video), all these data types are transformed into bit pattern
• Data (integer numbers) can be transformed into unsigned number, sign &
magnitude, and 2’s complement number representation
• Real numbers can be transformed using floating point representation (Sign,
Exponent, Mantissa)
• The most common code used to store text data is using ASCII code ( 1
character = 7-bit representation)
• Audio data is an analog data and must be converted into digital data
(sampling, quantization, encode) then it can be stored in computer.
• An image data is stored into computer using 2 different technique, raster
graphics and vector graphics.
• A video data is a representation of series of images displayed as a function
of time.
QF 7.01/REV01
Objectives
After studying this chapter, the student should be able to:
List the three categories of operations performed on data.
Perform unary and binary logic operations on bit patterns.
Distinguish between logic shift operations and arithmetic shift operations.
Perform addition and subtraction on integers when they are stored in
two’s complement format.
❑ Perform addition and subtraction on integers when stored in sign-and-
magnitude format.
❑ Perform addition and subtraction operations on reals stored in floating-
point format.
QF 7.01/REV01
QF 7.01/REV01
NOT
The NOT operator is a unary operator: it takes only one
input. The output bit is the complement of the input.
AND
The AND operator is a binary operator: it takes two inputs.
The output bit is 1 if both inputs are 1s and the output is 0
in the other three cases.
i
For x = 0 or 1 x AND 0 → 0 0 AND x → 0
QF 7.01/REV01
OR
The OR operator is a binary operator: it takes two inputs. The output
bit is 0 if both inputs are 0s and the output is 1 in other three cases.
i
For x = 0 or 1 x OR 1 → 1 1 OR x → 1
XOR
The XOR operator is a binary operator like the OR operator, with only
one difference: the output is 0 if both inputs are 1s.
i
For x = 0 or 1
1 XOR x → NOT x x XOR 1 → NOT x
QF 7.01/REV01
Example 4.1
In English we use the conjunction “or” sometimes to mean an
inclusive--or, and sometimes to means an exclusive
inclusive exclusive--or
or..
QF 7.01/REV01
Example 4.2
QF 7.01/REV01
QF 7.01/REV01
Example 4.3
Solution
The solution is shown below
below.. Note that the NOT operator
changes every 0 to 1 and every 1 to 0.
QF 7.01/REV01
Example 4.4
Use the AND operator on the bit patterns 10011000 and
00101010..
00101010
Solution
The solution is shown below
below.. Note that only one bit in the output
is 1, where both corresponding inputs are 1s.
QF 7.01/REV01
Example 4.5
Solution
The solution is shown below
below.. Note that only one bit in the output
is 0, where both corresponding inputs are 0s.
QF 7.01/REV01
Example 4.6
Use the XOR operator on the bit patterns 10011001 and
00101110..
00101110
Solution
The solution is shown below
below.. Compare the output in this example
with the one in Example 4.5. The only difference is that when the
two inputs are 1s, the result is 0 (the effect of exclusion)
exclusion)..
QF 7.01/REV01
Applications
Unsetting (AND)
Setting (OR)
Flipping (XOR)
QF 7.01/REV01
Example 4.7
Use a mask to unset (clear) the five leftmost bits of a pattern
pattern.. Test
the mask with the pattern 10100110
10100110..
Solution
The mask is 00000111
00000111.. The result of applying the mask is
is::
QF 7.01/REV01
Example 4.8
Use a mask to set the five leftmost bits of a pattern
pattern.. Test the mask
with the pattern 10100110
10100110..
Solution
The mask is 11111000
11111000.. The result of applying the mask is
is::
QF 7.01/REV01
Example 4.9
Use a mask to flip the five leftmost bits of a pattern
pattern.. Test the
mask with the pattern 10100110
10100110..
Solution
The mask is 11111000
11111000.. The result of applying the mask is
is::
QF 7.01/REV01
QF 7.01/REV01
Logical shift
QF 7.01/REV01
QF 7.01/REV01
Example 4.10
Solution
The solution is shown below
below.. The leftmost bit is lost and a 0 is
inserted as the rightmost bit
bit..
Discarded
Added
QF 7.01/REV01
QF 7.01/REV01
Example 4.11
Solution
The solution is shown below
below.. The leftmost bit is circulated and
becomes the rightmost bit
bit..
QF 7.01/REV01
QF 7.01/REV01
Example 4.12
Use an arithmetic right shift operation on the bit pattern
10011001.. The pattern is an integer in two’s complement format
10011001 format..
Solution
The solution is shown below
below.. The leftmost bit is retained and also
copied to its right neighbor bit
bit..
The original number was −103 and the new number is −52
52,, which
is the result of dividing −103 by 2 truncated to the smaller
integer..
integer
QF 7.01/REV01
Example 4.13
Use an arithmetic left shift operation on the bit pattern 11011001
11011001..
The pattern is an integer in two’s complement format
format..
Solution
The solution is shown below
below.. The leftmost bit is lost and a 0 is
inserted as the rightmost bit
bit..
The original number was −39 and the new number is −7878.. The
original number is multiplied by two
two.. The operation is valid
because no underflow occurred
occurred.. QF 7.01/REV01
Example 4.14
Use an arithmetic left shift operation on the bit pattern 01111111
01111111..
The pattern is an integer in two’s complement format
format..
Solution
The solution is shown below
below.. The leftmost bit is lost and a 0 is
inserted as the rightmost bit
bit..
The original number was 127 and the new number is −2. Here the
result is not valid because an overflow has occurred
occurred.. The expected
answer 127 × 2 = 254 cannot be represented by an 8-bit pattern
pattern..
QF 7.01/REV01
Example 4.15
Combining logic operations and logical shift operations give us some tools
for manipulating bit patterns
patterns.. Assume that we have a pattern and we need
to use the third bit (from the right) of this pattern in a decision
decision--making
process.. We want to know if this particular bit is 0 or 1. The following
process
shows how we can find outout..
QF 7.01/REV01
QF 7.01/REV01
A − B ↔ A + (B + 1)
Where B is the one’s complement of B and
(B + 1) means the two’s complement of B
QF 7.01/REV01
QF 7.01/REV01
QF 7.01/REV01
Example 4.16
A = (00010001
00010001))2 B = (00010110
00010110))2
Solution
The operation is adding
adding.. A is added to B and the result is stored in
R. (+
(+17
17)) + (+
(+22
22)) = (+
(+39
39)).
QF 7.01/REV01
A = (00011000
00011000))2 B = (11101111
11101111))2
Solution
The operation is adding
adding.. A is added to B and the result is stored in
R. (+24)) + (-17
(+24 17)) = (+
(+77).
QF 7.01/REV01
Example 4.18
Two integers A and B are stored in two’s complement format
format..
Show how B is subtracted from A.
A = (00011000
00011000))2 B = (11101111
11101111))2
Solution
The operation is subtracting
subtracting.. A is added to (B + 1) and the result
is stored in R. (+24)) - (-17
(+24 17)) = (+
(+41
41)).
QF 7.01/REV01
Example 4.19
Two integers A and B are stored in two’s complement format
format..
Show how B is subtracted from A.
A = (11011101
11011101))2 B = (00010100
00010100))2
Solution
The operation is subtracting
subtracting.. A is added to (B + 1) and the result
is stored in R. (−
(−35
35)) − (+20)) = (−
(+20 (−55
55)).
QF 7.01/REV01
Example 4.20
Two integers A and B are stored in two’s complement format
format.. Show
how B is added to A.
A = (01111111
01111111))2 B = (00000011
00000011))2
Solution
The operation is adding
adding.. A is added to B and the result is stored in R.
i
When we do arithmetic operations on numbers in a
computer, we should remember that each number
and the result should be in the range defined by
the bit allocation.
QF 7.01/REV01
sign-and-magnitude integers
Addition and subtraction for integers in sign-and-magnitude
representation looks very complex. We have four different
combination of signs (two signs, each of two values) for addition
and four different conditions for subtraction. This means that we
need to consider eight different situations. However, if we first check
the signs, we can reduce these cases, as shown in Figure 4.7.
QF 7.01/REV01
Example 4.22
Two integers A and B are stored in sign
sign--and
and--magnitude format
format..
Show how B is added to A.
A = (0 0010001
0010001))2 B = (1 0010110
0010110))2
Solution
The operation is adding
adding:: the sign of B is not changed
changed.. S = AS XOR BS = 1; RM =
AM + (BM +1). Since there is no overflow, we need to take the two’s
complement of RM. The sign of R is the sign of B. (+ 17)) + ( −22
(+17 22)) = (−
(−55 ).
QF 7.01/REV01
Example 4.23
Two integers A and B are stored in sign
sign--and
and--magnitude format
format..
Show how B is subtracted from A.
A = (1 1010001
1010001))2 B = (1 0010110
0010110))2
Solution
The operation is subtracting
subtracting:: SB = SB. S = AS XOR BS = 1, RM = AM + (BM
+1). Since there is an overflow, the value of RM is final
final.. The sign of R is the
sign of A. (−
(−81
81)) − (−
(−22
22)) = (−
(−59
59)).
QF 7.01/REV01
QF 7.01/REV01
QF 7.01/REV01
Example 4.24
Show how the computer finds the result of (+
(+55.75
75)) + (+
(+161
161..875
875))
= (+
(+167
167..625
625)).
Solution
As we saw in Chapter 3, these two numbers are stored in floating
floating--
point format, as shown below, but we need to remember that each
number has a hidden 1 (which is not stored, but assumed)
assumed)..
QF 7.01/REV01
The first few steps in the UML diagram (Figure 4.8) are not
needed.. We de
needed de--normalize the numbers by adding the hidden 1s to
the mantissa and incrementing the exponent
exponent.. Now both de de--
normalized mantissas are 24 bits and include the hidden 1s. They
should be stored in a location that can hold all 24 bits
bits.. Each
exponent is incremented
incremented..
QF 7.01/REV01
Example 4.25
Show how the computer finds the result of (+
(+55.75
75)) +
(−77.0234375
(− 0234375)) = − 1.2734375
2734375..
Solution
These two numbers can be stored in floating
floating--point format, as
shown below
below::
De--normalization results in
De in::
QF 7.01/REV01
QF 7.01/REV01
QF 7.01/REV01