Skip to content

Instantly share code, notes, and snippets.

@Garmelon
Created October 21, 2017 22:25
Show Gist options
  • Save Garmelon/55ed2ca6e2b7e1dc3a55e76b661f0b60 to your computer and use it in GitHub Desktop.
Save Garmelon/55ed2ca6e2b7e1dc3a55e76b661f0b60 to your computer and use it in GitHub Desktop.
#include <stdio.h>
unsigned long add(unsigned long a, unsigned long b)
{
unsigned long xor = a ^ b;
unsigned long and = a & b;
while (and) {
a = xor;
b = and << 1;
xor = a ^ b;
and = a & b;
}
return xor;
}
unsigned long mul(unsigned long a, unsigned long b)
{
unsigned long n = 0;
unsigned long sum = 0;
while (a >> n) {
if ((a >> n) & 1) {
sum = add(sum, b << n);
}
n = add(n, 1);
}
return sum;
}
int main(int argc, char** argv)
{
int x, y;
for (x=0; x<10; ++x) {
for (y=0; y<10; ++y) {
printf("%d * %d = %d\n", x, y, mul(x, y));
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment