r/dailyprogrammer • • Aug 05 '12

[8/3/2012] Challenge #85 [difficult] (Bitwise arithmetic)

While basic arithmetic operations like addition, subtraction, multiplication, etc. seem 'natural' to us, computers think on a very different level: under the hood, computations work with bitwise operations, using operators like ~, &, |, ^, and <<. Your task is to implement functions for (integer) addition, subtraction, negation, multiplication, division, modulo, and exponentiation, without using any "high-level" operators like + and *. Other statements like "if" and "while", or recursion for functional languages, are fine.

As a hint, you could start with a helper function that increments a binary number, and work from there. Your functions should take signed integer arguments and return signed integer values, rounding down (e.g. binary_div(14, 4) == 3, not 3.5).

Use your set of functions to implement this function:

f(a, b, c, d, e) = (a % e) * (b / (c - a) + exp(d * (-b), e))

What is the result of f(50, -40, 300, 2, 3)?

12 Upvotes

14 comments sorted by

View all comments

3

u/Ledrug 0 2 Aug 05 '12

Crude implementation, using only &, |, ~ and "equal to zero" comparisons. The division function is braindead, but it's simpler this way.

#include <stdio.h>
#include <stdlib.h>

typedef unsigned int bit_t;
bit_t HIGHEST_BIT = ~0 & ~(~0U >> 1);
#define NEG(a) add(~(a), 1)
#define sub(a, b) add(a, NEG(b))

void die(char *msg) {
	fprintf(stderr, "fatal: %s\n", msg);
	exit(1);
}

int cmp(int a, int b)
{
	bit_t x = HIGHEST_BIT;

	int _cmp(int a, int b, bit_t x) {
		if (x == 0) return 0; // have to assume x == 0 is allowed

		return	((a & x) && !(b & x)) ?  1 :
			((b & x) && !(a & x)) ? -1 :
			_cmp(a, b, x >> 1);
	}

	return	(x & (a ^ b))
			? ((a & x) ? -1 : 1)
			: (x & a)
				? _cmp(~b, ~a, x >> 1)
				: _cmp(a, b, x >> 1);
}

int eq(int a, int b) { return cmp(a, b) == 0; }
int is_neg(int a) { return eq(cmp(0, a), 1); }

int ge(int a, int b) { // a >= b
	int r = cmp(a, b);
	return (!r) | eq(r, 1);
}

int add(int a, int b)
{
	bit_t x, c;

	int _add_bit() {
		int l =	((a & x) ? 4 : 0) | ((b & x) ? 2 : 0) | (c ? 1 : 0);
		if (l == 0) return 0;
		if (eq(l, 1) | eq(l, 2) | eq(l, 4)) {
			c = 0;
			return 1;
		}
		if (eq(l, 3) | eq(l, 5) | eq(l, 6)) {
			c = 1;
			return 0;
		}
		c = 1;
		return 1;
	}

	int res = 0;
	for (x = 1, c = 0; x; x <<= 1, c = c ? x : 0)
		res |= _add_bit() ? x : 0;

	return res;
}

int mul(int a, int b)
{
	bit_t x = 1;
	int res = 0;
	for (x = 1; x; x <<= 1, a <<= 1)
		if (b & x) res = add(res, a);

	return res;
}

int expo(int a, int e)
{
	if (eq(a, 1)) return 1;
	if (is_neg(e)) return 0; //probably should abort

	bit_t x;
	int res = 1;
	for (x = 1; x; x <<= 1, a = mul(a, a))
		if (e & x) res = mul(res, a);

	return res;
}

int divide(int a, int m)
{
	if (!m) die("division by zero");

	// some overflows are not considered
	int neg = 0, res = 0;
	if (is_neg(a)) neg = !neg, a = NEG(a);
	if (is_neg(m)) neg = !neg, m = NEG(m);

	while (ge(a, m)) // well
		a = sub(a, m), res = add(res, 1);
	return neg ? NEG(res) : res;
}

int mod(int a, int m)
{
	int r = divide(a, m);
	return sub(a, mul(r, m));
}

int main(void)
{
	int a = 50, b = -40, c = 300, d = 2, e = 3;

	printf("%d\n", mul(
			add(mod(a, e), divide(b, sub(c, a))),
			expo(mul(d, NEG(b)), e)));

	//printf("%d\n", (a % e) * (b / (c - a) + (int)pow(d * (-b), e)));
	return 0;
}