# Write a function that results the sum of two integers using bitwise operations, w\o conditional operation [duplicate]

• You cannot use if,while,for, ?:, ||, && etc., or recursion!
• Assume it is Int.32
• They use control flow with the ?: operators. Dec 10, 2013 at 12:49
• (i) ?: is not "control flow", it's just an operator and it may well be branchless; (ii) if you don't want to allow the ternary conditional operator then you should state this in the question. Dec 10, 2013 at 15:43
• @PaulR - Unless I am misunderstanding, ?: equates to an if-then statement. If it is not considered a part of control-flow, then what is it considered? I was going based off of this Control flow wikipedia entry. Dec 10, 2013 at 16:32
• No - it doesn't equate to if-else (in C and related languages at least) - it's just an operator, which in some limited cases you can use to replace if-else, but it's still just an operator nonetheless, and its use does not necessarily imply branching. Anyway, I see you've changed the rules now to exclude ?: so I guess that makes its use moot. Dec 10, 2013 at 19:27
• @PaulR hmm yes, it does imply branching. Code inside the ?: operator is not executed unless the condition is met. And that is what a branching is. Dec 12, 2013 at 23:44

## C - 91

Based on Kogge-Stone, there is probably ways to cut parts of this off, but it seems good enough to start the race.

g,p;t(h){g|=p&g<<h;p&=p<<h;}d(a,b){g=a&b,p=a^b;t(1);t(2);t(4);t(8);t(16);return a^b^g<<1;}


An ungolfed version and possibly some explanation can be found from http://chessprogramming.wikispaces.com/Parallel+Prefix+Algorithms

# Python 3 (66 chars)

def a(i,j):v=locals();exec("i,j=i^j,(i&j)<<1;"*32,v);return v['i']

• Nice workaround for the loop limitation. I wonder if I could do something similar with macros. Dec 10, 2013 at 23:29

# Golfscript, 3 (or 7, if it needs to be assigned to a function)

Since you never said + was forbidden

0|+


0| basically does nothing (if the top of the stack is a number), because n [bitwise or] 0 is n.

+ adds them.

If it needs to be a function:

{0|+}:s


Assigned to function s.

## J - 814

A better version

+&.(1&(32 b.))


Which does: sum under rotate bits which means (for people who don't speak J): do rotate on both arguments, sum them, inverse the rotation.

This way the bit operation is really used in the result.

• First of all: I have no idea what your code does. My comment is written as if you just summed two numbers in a completely normal manner. I think "write -- sum -- using bitwise operations" does include both of the requirements. Usually I like cheaters in code-golf, but if we continue on this path the questions will have to be written in formal mathematical definitions. I don't want to go there. Dec 10, 2013 at 18:33
• @shiona to be fair, the title is not really a requirement. The OP should have put more in the body of the question. Dec 10, 2013 at 21:50
• you are right, the result of the bitop is not really used . Here's a better one... Dec 12, 2013 at 18:15