-3
\$\begingroup\$
  • You cannot use if,while,for, ?:, ||, && etc., or recursion!
  • Assume it is Int.32
  • Shortest answer wins
\$\endgroup\$
9
  • \$\begingroup\$ They use control flow with the ?: operators. \$\endgroup\$
    – TheKobra
    Dec 10, 2013 at 12:49
  • \$\begingroup\$ (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. \$\endgroup\$
    – Paul R
    Dec 10, 2013 at 15:43
  • \$\begingroup\$ @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. \$\endgroup\$
    – TheKobra
    Dec 10, 2013 at 16:32
  • \$\begingroup\$ 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. \$\endgroup\$
    – Paul R
    Dec 10, 2013 at 19:27
  • \$\begingroup\$ @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. \$\endgroup\$
    – MaiaVictor
    Dec 12, 2013 at 23:44

4 Answers 4

2
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

Python 3 (66 chars)

def a(i,j):v=locals();exec("i,j=i^j,(i&j)<<1;"*32,v);return v['i']
\$\endgroup\$
1
  • \$\begingroup\$ Nice workaround for the loop limitation. I wonder if I could do something similar with macros. \$\endgroup\$
    – shiona
    Dec 10, 2013 at 23:29
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – shiona
    Dec 10, 2013 at 18:33
  • 1
    \$\begingroup\$ @shiona to be fair, the title is not really a requirement. The OP should have put more in the body of the question. \$\endgroup\$
    – Cruncher
    Dec 10, 2013 at 21:50
  • \$\begingroup\$ you are right, the result of the bitop is not really used . Here's a better one... \$\endgroup\$
    – jpjacobs
    Dec 12, 2013 at 18:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.