LectureNotes 10
LectureNotes 10
Bit Manipulations
1 Multiplication Algorithm
With the important capability of decision making in our repertoire we move on to the discussion of
an algorithm, which will help us uncover an important set of instructions in our processor used for bit
manipulations.
Multiplication is a common process that we use, and we were trained to do in early schooling. Remember
multiplying by a digit and then putting a cross and then multiplying with the next digit and putting two
crosses and so on and summing the intermediate results in the end. Very familiar process but we never
saw the process as an algorithm, and we need to see it as an algorithm to convey it to the processor.
To highlight the important thing in the algorithm we revise it on two 4bit binary numbers. The numbers
are 1101 i.e. 13 and 0101 i.e. 5. The answer should be 65 or in binary 01000001. Observe that the answer
is twice as long as the multiplier and the multiplicand. The multiplication is shown in the following
figure.
We take the first digit of the multiplier and multiply it with the multiplicand. As the digit is one the
answer is the multiplicand itself. So we place the multiplicand below the bar. Before multiplying with
1
the next digit a cross is placed at the right most place on the next line and the result is placed shifted
one digit left. However since the digit is zero, the result is zero. Next digit is one, multiplying with
which, the answer is 1101. We put two crosses on the next line at the right most positions and place
the result there shifted two places to the left. The fourth digit is zero, so the answer 0000 is placed with
three crosses to its right.
Observe the beauty of binary base, as no real multiplication is needed at the digit level. If the digit is 0
the answer is 0 and if the digit is 1 the answer is the multiplicand itself. Also observe that for every next
digit in the multiplier the answer is written shifted one more place to the left. No shifting for the first
digit, once for the second, twice for the third and thrice for the fourth one. Adding all the intermediate
answers the result is 01000001=65 as desired. Crosses are treated as zero in this addition.
Before formulating the algorithm for this problem, we need some more instructions that can shift a
number so that we use this instruction for our multiplicand shifting and also some way to check the bits
of the multiplier one by one.
The set of shifting and rotation instructions is one of the most useful set in any processor’s instruction
set. They simplify really complex tasks to a very neat and concise algorithm. The following shifting and
rotation operations are available in our processor.
The shift logical right operation inserts a zero from the left and moves every bit one position to the right
and copies the rightmost bit in the carry flag. Imagine that there is a pipe filled to capacity with eight
balls. The pipe is open from both ends and there is a basket at the right end to hold anything dropping
from there. The operation of shift logical right is to force a white ball from the left end. The operation
is depicted in the following illustration.
White balls represent zero bits while black balls represent one bits. Sixteen bit shifting is done the same
way with a pipe of double capacity.
2
2.2 Shift Logical Left (SHL) / Shift Arithmetic Left (SAL)
The shift logical left operation is the exact opposite of shift logical right. In this operation the zero bit is
inserted from the right and every bit moves one position to its left with the most significant bit dropping
into the carry flag. Shift arithmetic left is just another name for shift logical left. The operation is again
exemplified with the following illustration of ball and pipes.
A signed number holds the sign in its most significant bit. If this bit was one a logical right shifting will
change the sign of this number because of insertion of a zero from the left. The sign of a signed number
should not change because of shifting. The operation of shift arithmetic right is therefore to shift every
bit one place to the right with a copy of the most significant bit left at the most significant place. The
bit dropped from the right is caught in the carry basket. The sign bit is retained in this operation. The
operation is further illustrated below.
The left shifting operation is basically multiplication by 2 while the right shifting operation is division
by two. However for signed numbers division by two can be accomplished by using shift arithmetic
right and not shift logical right. The left shift operation is equivalent to multiplication except when an
important bit is dropped from the left. The overflow flag will signal this condition if it occurs and can
be checked with JO. For division by 2 of a signed number logical right shifting will give a wrong answer
for a negative number as the zero inserted from the left will change its sign. To retain the sign flag and
still effectively divide by two the shift arithmetic right instruction must be used on signed numbers.
In the rotate right operation every bit moves one position to the right and the bit dropped from the
right is inserted at the left. This bit is also copied into the carry flag. The operation can be understood
by imagining that the pipe used for shifting has been molded such that both ends coincide. Now when
3
the first ball is forced to move forward, every ball moves one step forward with the last ball entering the
pipe from its other end occupying the first ball’s old position. The carry basket takes a snapshot of this
ball leaving one end of the pipe and entering from the other.
In the operation of rotate left instruction, the most significant bit is copied to the carry flag and is
inserted from the right, causing every bit to move one position to the left. It is the reverse of the rotate
right instruction. Rotation can be of eight or sixteen bits. The following illustration will make the
concept clear using the same pipe and balls example.
In the rotate through carry right instruction, the carry flag is inserted from the left, every bit moves one
position to the right, and the right most bit is dropped in the carry flag. Effectively this is a nine bit or
a seventeen bit rotation instead of the eight or sixteen bit rotation as in the case of simple rotations.
Imagine the circular molded pipe as used in the simple rotations but this time the carry position is
part of the circle between the two ends of the pipe. Pushing the carry ball from the left causes every
ball to move one step to its right and the right most bit occupying the carry place. The idea is further
illustrated below.
4
2.7 Rotate Through Carry Left (RCL)
The exact opposite of rotate through carry right instruction is the rotate through carry left instruction.
In its operation the carry flag is inserted from the right causing every bit to move one location to its
left and the most significant bit occupying the carry flag. The concept is illustrated below in the same
manner as in the last example.