GTU Computer Organization & Architecture(COA) - 1
Implement Booth’s Algorithm
				
					#include <stdio.h>
#include <stdlib.h>

// Function to perform Booth's algorithm
int booth(int x, int y)
{
    int n = 8; // number of bits in x and y
    int result = 0;
    int Q = 0; // Q is the sign bit of the partial product

    // loop through each bit of y
    for (int i = n - 1; i >= 0; i--)
    {
        // check if Q is different from the sign bit of y
        if (Q != (y >> i) & 1)
        {
            // if Q is different, then we need to add or subtract x from the result
            if (y >> i & 1)
            {
                result -= x;
            }
            else
            {
                result += x;
            }
        }

        // shift the result left by 1 and set Q to the sign bit of the result
        result <<= 1;
        Q = result >> n & 1;
    }

    return result;
}

int main(int argc, char *argv[])
{
    // test the booth function
    printf("%d\n", booth(-4, 5)); // should print -20
    printf("%d\n", booth(-3, 4)); // should print -12
    printf("%d\n", booth(7, -3)); // should print -21

    return 0;
}