Algorithms-Unit 5 CO
Algorithms-Unit 5 CO
algorithms and methods. Here’s a basic overview of a common approach: the binary addition
algorithm.
Binary Addition Algorithm
1. Binary Representation: First, the numbers to be added are converted into binary
form. For example, the decimal numbers 3 and 5 are represented as 0011 and 0101 in
4-bit binary.
2. Addition Process: The addition is performed bit by bit from the least significant bit
(LSB) to the most significant bit (MSB), using the following rules:
o 0 + 0 = 0 (no carry)
o 0 + 1 = 1 (no carry)
o 1 + 0 = 1 (no carry)
o 1 + 1 = 10 (0 with a carry of 1)
3. Carry Handling: If a carry is generated from a sum (e.g., 1 + 1), it is added to the
next higher bit in the next column.
4. Final Result: The final result is obtained by concatenating the sums and any
remaining carry.
Example
Let’s add the binary numbers 0011 (3) and 0101 (5):
yaml
Copy code
0011
+ 0101
------
Column 1 (LSB): 1 + 1 = 10 → Write down 0, carry 1
Column 2: 1 + 0 + carry(1) = 10 → Write down 0, carry 1
Column 3: 0 + 1 + carry(1) = 10 → Write down 0, carry 1
Column 4 (MSB): 0 + 0 + carry(1) = 01 → Write down 1
So, the result is 1000, which is 8 in decimal.
Hardware Implementation
In hardware, binary addition is typically implemented using:
Full Adders: These components perform the addition of three bits (two significant
bits and a carry from the previous column). They can be combined to form an adder
circuit.
Ripple Carry Adder: A series of full adders connected in a way that the carry output
of one adder is the carry input of the next. While simple, it can be slow for large
numbers due to carry propagation.
Carry Lookahead Adder: A more advanced circuit that reduces delay by calculating
carries in advance rather than waiting for each bit to propagate.
In computer organization, subtraction can be performed using various algorithms and
techniques. One common approach is to use the method of complement arithmetic. Here's a
breakdown of how subtraction can be implemented:
1. Binary Subtraction Using Borrowing
Similar to decimal subtraction, binary subtraction can use a borrowing method. Here's the
basic process:
Align the binary numbers.
Subtract each bit starting from the least significant bit (LSB).
If a bit in the minuend (the number from which another is subtracted) is smaller than
the corresponding bit in the subtrahend (the number being subtracted), borrow from
the next higher bit.
2. Two's Complement Method
This is a widely used method for performing subtraction in binary:
1. Find the Two's Complement of the Subtrahend:
o Invert all bits of the subtrahend (this is called the one's complement).
o Add 1 to the least significant bit (LSB) of the inverted number to get the two's
complement.
2. Add the Two's Complement to the Minuend:
o Perform binary addition of the minuend and the two's complement of the
subtrahend.
o If there is a carry out of the most significant bit (MSB), it can be ignored in the
case of fixed-width binary numbers.
3. Result Interpretation:
o If the result is negative (in a signed binary format), it is represented in two's
complement format.
Example
To subtract 5 (101 in binary) from 12 (1100 in binary):
1. Find the two's complement of 5:
o One's complement of 5 (101) → 010
o Add 1 → 011 (which is 3 in decimal)
2. Add this to 12:
o 1100 (12)
o
0011 (two's complement of 5)
o