40
\$\begingroup\$

Problem:

Your goal is to add two input numbers without using any of the following math operators: +,-,*,/.

Additionally, you can't use any built-in functions that are designed to replace those math operators.

Scoring:

Smallest code (in number of bytes) wins.

Update

Most of the programs i've seen either concatenate two arrays containing their numbers, or make first number of a character, append second number characters, then count them all.

Shortest array counter: APL with 8 chars, by Tobia

Shortest array concatenation: Golfscript with 4 chars, by Doorknob

Shortest logarithmic solution: TI-89 Basic with 19 chars, by Quincunx

Integration solution: Mathematica with 45 chars, by Michael Stern

Coolest, in my opinion: bitwise operators in javascript, by dave

\$\endgroup\$
  • \$\begingroup\$ Will it have floats? \$\endgroup\$ – Ismael Miguel Feb 16 '14 at 1:06
  • 7
    \$\begingroup\$ Will it have negative numbers? (Currently, all the answers assume that the numbers will be positive, so you probably shouldn't change that) \$\endgroup\$ – Doorknob Feb 16 '14 at 3:48
  • 4
    \$\begingroup\$ What about the mathematical solutions? You forgot to list those! This integrates, and this plays with logarithms \$\endgroup\$ – Justin Feb 17 '14 at 0:54
  • 3
    \$\begingroup\$ Why did you accept one of the longer solutions? Is it because it accepts negative numbers while the shortest solutions (this and this) don't? If so, my answer supports negative numbers (it also supports floating point) and is shorter than this one. You tagged this question as code-golf, thus you are obliged to accept the shortest solution. \$\endgroup\$ – Justin Feb 20 '14 at 21:01
  • 2
    \$\begingroup\$ Define "number". Any integer? Non-negative integers? Do they have to be base-10? \$\endgroup\$ – SuperJedi224 Jan 20 '17 at 13:44

77 Answers 77

0
\$\begingroup\$

Dart - 30

s(a,b)=>a>0?s((a&b)<<1,a^b):b;

After writing this, I realized it was equivalent to the JavaScript solution already posted above - and because Dart has bignums, not just int32, it doesn't work for negative numbers. So, not a big win :(

Termination can be proven by seeing that the number of bits in a and b together reduces for each iteration until a becomes zero.

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

Microscript, 9 bytes

A variant of the array concatenation solution (although this one uses a stack.)

ics]ics]#

Technically not a valid competing entry, however; as the language is too new.

Requires that both inputs be nonnegative.


A seven-byte version using a new command added shortly after I originally posted this:

i$si$s#

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

X86 (2)

rep movb two bytes f3 A4,

adds ecx to esi (and to edi) result in esi but has side-effects if ds:esi != es:edi

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

Python 2, Python 3, 41 bytes

i,j=input(),input()
while j>0:i=-~i;j=~-j

If using the - number operator is allowed.


Python 2, Python 3, 40 bytes

i,j=input(),input()
while j>0:i=-~i;j-=1

If using the - number and literal -= number operators is allowed.


Works on positive integers. Takes a and b and prints a+b.

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

JAVA, 79 bytes

int Add(int x, int y){while (y != 0){int carry=x&y;x=x^y;y=carry<<1;}return x;}
\$\endgroup\$
0
\$\begingroup\$

PHP, 91 78 51 bytes

bit logic and shifting ... what else?

function p($x,$y){return$y?p($x^$y,($x&$y)<<1):$x;}

also works for negative integers

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

Haskell, 25 bytes

n+m=length$[1..m]++[1..n]

We can no longer use the old +? No problem, we can just define a new one. This approach doesn't work with negative numbers, I have yet to figure out something else for them.

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

R, 80 bytes

Went for a recursive function doing bit operations.

f=function(a,b){d=bitwXor(a,b);e=bitwShiftL(bitwAnd(a,b),1);ifelse(!e,d,f(d,e))}

Ungolfed:

f=function(a,b){
    d=bitwXor(a,b)            # xors a and b.(tells us bits that are 1 in end)
    f=bitwAnd(a,b)            # tells us which bits need to carry over 1
    e=bitwShiftL(e,1)         # carries those bits over
    if (e==0) d else f(d,e)   # if carried nothing, return xored bits. 
}                             # otherwise, recursively call fn on xored and
                              # carried bits.

It would have been shorter -- but R really does not intend for bit operations.

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

Turing Machine Code, 1184 bytes

A golfed version of a machine I posted to my googology wiki user blog a while ago. Supports signed ints. Try it online!

0 _ * l B
0 - * r AAA
0 * * r *
B 0 9 l *
B 9 8 r C
B 8 7 r C
B 7 6 r C
B 6 5 r C
B 5 4 r C
B 4 3 r C
B 3 2 r C
B 2 1 r C
B 1 0 r C
B _ * r AA
B * * * AA
C _ * r D
C * * r *
D _ * l E
D - * r 8
D * * r *
E 0 1 l F
E 1 2 l F
E 2 3 l F
E 3 4 l F
E 4 5 l F
E 5 6 l F
E 6 7 l F
E 7 8 l F
E 8 9 l F
E 9 0 l *
E _ * r S1
E M * r S1
F _ * l B
F * * l *
AA _ * r BB
AA * _ r *
BB M - * halt
BB * * * halt
S1 0 1 r S0
S0 0 0 r S0
S0 _ 0 * F 
AAA 0 9 l *
AAA 9 8 r BBB
AAA 8 7 r BBB
AAA 7 6 r BBB
AAA 6 5 r BBB
AAA 5 4 r BBB
AAA 4 3 r BBB
AAA 3 2 r BBB
AAA 2 1 r BBB
AAA 1 0 r BBB
AAA _ * r BB
AAA * * * BB
BBB _ * r CCC
BBB * * r *
CCC _ * l DDD
CCC - * * ZZZ
CCC * * r *
DDD 9 8 l EEE
DDD 8 7 l EEE
DDD 7 6 l EEE
DDD 6 5 l EEE
DDD 5 4 l EEE
DDD 4 3 l EEE
DDD 3 2 l EEE
DDD 2 1 l EEE
DDD 1 0 l EEE
DDD 0 9 l *
DDD _ * r FFF
EEE _ * l *
EEE * * * AAA
FFF _ * * GGG
FFF * _ r *
GGG _ * l *
GGG * * * HHH
HHH 0 1 * halt
HHH 1 2 * halt
HHH 2 3 * halt
HHH 3 4 * halt
HHH 4 5 * halt
HHH 5 6 * halt
HHH 6 7 * halt
HHH 7 8 * halt
HHH 8 9 * halt
HHH 9 0 l *
8 _ * l 9
8 * * r * 
9 9 8 l F
9 8 7 l F
9 7 6 l F
9 6 5 l F
9 5 4 l F
9 4 3 l F
9 3 2 l F
9 2 1 l F
9 1 0 l F
9 0 9 l *
9 * * * FFF
ZZZ * M r E
\$\endgroup\$
  • \$\begingroup\$ can't you use shorter state names :| \$\endgroup\$ – ASCII-only Apr 12 at 7:18
0
\$\begingroup\$

Ruby, 41 bytes

->a,b{eval([97,43,98].map(&:chr).join'')}
\$\endgroup\$
0
\$\begingroup\$

Julia, 29 bytes

a$b=next(countfrom(a,b),a)[2]

Julia has iterators that can count up by an arbitrary amount at a time. It's usually used to create arithmetic sequences, but it can be definitely pressed into service as a ersatz adder.

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

Octave, 62 39 bytes

@(a,b)round(toc(tic,pause(a),pause(b)))

Unsigned integers only. Try it online!

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

Java 8, 42 bytes

(x,y)->{for(int c;y!=0;c=x&y,x^=y,y=c<<1);return x;};

Try it online!
Based on https://www.geeksforgeeks.org/add-two-numbers-without-using-arithmetic-operators/

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

Python 2 & 3, 39 bytes (function)

lambda x,y:eval("abs(~"*x+str(y)+")"*x)

Try it online!

The * and + are string repetition and string concatenation, not mathematical operators.

Explanation

abs(~x) effectively increments x, so doing abs(~abs(~abs(...~abs(~x)))...) y times just increments n y times.

Python 2, 54 bytes (program)

x,y=input(),str(input());print eval("abs(~"*x+y+")"*x)

Try it online!

Python 3, 55 bytes (program)

x,y=int(input()),input();print(eval("abs(~"*x+y+")"*x))

Try it online!

\$\endgroup\$
  • \$\begingroup\$ while i was writing that i realized it doesn't work for negative numbers, is that a problem? \$\endgroup\$ – Sagittarius Aug 31 at 13:16
  • \$\begingroup\$ @A_ oh i was thinking about doing that but i ruled it out pretty quickly because i thought the - counted as a mathematical operator; ill edit it \$\endgroup\$ – Sagittarius Aug 31 at 22:49
  • \$\begingroup\$ The unary - is a negation operator, and I don't think that counts as a math operator. \$\endgroup\$ – A _ Aug 31 at 23:40
0
\$\begingroup\$

Python 3, 38 bytes

f=lambda a,b:b and f(a^b,(a&b)<<1)or a

Try it online!

Uses bitwise operations recursively to work out the sum.
Works with some negative numbers (works as long as both are positive, or their sum is less than zero).

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

Keg -hr, 10 bytes

(2|(¿|0))!

Try it online!

Places a whole bunch of 0's onto the stack, and then prints the length of the stack. It then uses the hr flag to print the top of the stack raw.

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

Java, 85 bytes

import java.math.*;class a{BigInteger A(BigInteger b,BigInteger B){return b.add(B);}}

BigInteger isn't located in java.lang, java.util, java.io (is it even possible for a numeric type to be related to I/O?) or their subpackages, so its add method doesn't count as a language feature. Problem?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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