I challenged myself to implement multiplication of signed integers using only +, -, >>, <<, &, |, ^, and conditionals, though I would like to improve my method to not use conditionals if possible in the future. Any input would be very helpful.#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <limits.h>#include <time.h>int multiply(int, int);int main() {    int a, b, c, i;    bool equal;    srand(time(NULL));    for (i = 0; i < 100; i++) {        // 0x8000 to ensure no integer overflow is possible        a = (rand() % 0x8000) * (rand() % 2 ? -1 : 1);        b = (rand() % 0x8000) * (rand() % 2 ? -1 : 1);        c = multiply(a, b);        equal = (c == a * b);        printf(%-11d * %-11d = %-11d (%s)\n, a, b, c, (equal ? Success : Failure));        if (!equal) {            return EXIT_FAILURE;        }    }    return EXIT_SUCCESS;}int multiply(int a, int b) {    struct {        unsigned char index : 5;    } bit;    int c = INT_MAX & a, // remove sign bit        d = INT_MAX & b,        sign = (INT_MIN & a) ^ (INT_MIN & b), // sign of product        mask = 0x40000000,        prod = 0;    bit.index = 30; // keeping track of mask bit index    do {        prod += (mask & c ? d << bit.index : 0);    } while ((bit.index--) && (mask >>= 1));    return sign | (prod & INT_MAX);}
Mockup in C of hardware implementation for multiplication of signed integers
c;reinventing the wheel;bitwise;integer
Sign bit manipulation unnecessaryI looked at your code and was puzzled by all the sign bit manipulation.  As far as I can tell, it's not necessary.  I rewrote your function to be like the following and it still passed all your tests:int multiply(int a, int b){    // Bit keeps track of mask bit index.    struct {        unsigned int index: 5;    } bit = {31};    int prod = 0;    do {        prod += ((a & (1 << bit.index)) ? b << bit.index : 0);    } while (bit.index--);    return prod;}Bonus answer: no conditionals inside loopint multiply(int a, int b){    // Bit keeps track of mask bit index.    struct {        unsigned int index: 5;    } bit = {31};    int prod = 0;    do {        prod += (-((a >> bit.index) & 1)) & (b << bit.index);    } while (bit.index--);    return prod;}Explanation:((a >> bit.index) & 1) is 1 or 0 depending on the masked bit.  Call this B.-B is -1 or 0.  This sets up a mask to use for the next step (-1 is a mask of all one bits).-B & (b << bit.index) is either (b << bit.index) or 0.