68 B OaishQazi Exp7
68 B OaishQazi Exp7
EXPERIMENT NO: 7
Key Concepts:
• Binary Multiplication: Booth's algorithm allows for the
multiplication of signed binary numbers represented in two's
complement.
• Two's Complement: A way to represent negative numbers
in binary by inverting all bits and adding 1. Booth’s
algorithm uses this to handle both positive and negative
operands.
• Arithmetic Shifts: The algorithm performs arithmetic shifts
to divide or multiply numbers by powers of two.
Steps of Booth's Algorithm:
1. Initialization:
o The multiplier and multiplicand are initialized in
binary form.
o The product register consists of the multiplier, an
accumulator (initialized to 0), and an additional bit
Q1Q_1Q1 (set to 0 initially).
o The algorithm uses a counter that matches the
Class: S.E Comps (Sem III) Lecturer: Sejal M Chopra
Subject: DLCA Lab
Don Bosco Institute of Technology, Kurla
Academic Year 2024-25
SOFTWARE C/C++/Java/Python
USED
STEPS TO 1. Take two decimal numbers from the user in the range of 0 to 15,both positive
EXECUTE THE and negative(M=Multiplicand & Q=Multiplier) and convert it to 4-bit binary
PROGRAM numbers.
2. Negative numbers are represented in two's complement form.
3. Initialize the counter with the count of number of bits.
4. Initialize the one bit register Q1as 0.
5. Intialise A to zero ,where A is accumulator which stores the MSB of the result.
Multiplier Q stores the LSB of the result.
6. Check LSB bit of Q and Q-1:
a. If they are 11 or 00 arithmetic shift is done for A,Q and Q-1
and decrement the counter.
b. If they are 01 add A to M and then arithmetic shift is done for A,Q and Q-1 and
decrement the counter.
c. If they are 10 sub M from A and then arithmetic shift is done for A,Q and Q-1
and decrement the counter.
7.Check the counter, if it is not 0,move to step 6,otherwise store the result in A
and Q.
CODE
def booth_multiplication(m, r, n):
def binary_representation(value, bits):
if value >= 0:
return format(value, f'0{bits}b')
else:
return format((1 << bits) + value, f'0{bits}b')
A = '0' * n
Q = binary_representation(r, n)
M = binary_representation(m, n)
M_neg = binary_representation(-m, n)
Q_1 = '0'
count = n
count -= 1
result = A + Q
print(f"\nFinal binary result: {result}")
final_result = int(result, 2)
if result[0] == '1':
final_result -= 1 << (2 * n)
booth_multiplication(m, r, n)
Class: S.E Comps (Sem III) Lecturer: Sejal M Chopra
Subject: DLCA Lab
Don Bosco Institute of Technology, Kurla
Academic Year 2024-25
OUTPUT