News for hackers. Stuff that matters.

Calculate n + 1 without using + or - or * or /

As the title asks! My buddy came across this while preparing for an interview.

All I could think of is implementing a full adder in software but I'm thinking there has to be a better solution...help!

( 17 comments | 18 points | on HN )
Re:Calculate n + 1 without using + or - or * or / by KeithMajhor on Saturday July 02, 2011 @03:02AM (#2721211)
Arbitrary addition, subtraction and multiplication without arithmetic signs.

int add( int a, int b ) { while ( a ) { int c = a & b; b ^= a; a = c << 1; } return b; }

int sub( int a, int b ) { return add( a, add( ~b, 1 ) ); }

int mul( int a, int b ) { int c = 0; while ( a ) { if ( a & 1 ) c = add( c, b ); a >>= 1; b <<= 1; } return c; }

Re:Calculate n + 1 without using + or - or * or / by icebraining on Saturday July 02, 2011 @11:41PM (#2723017)
I have the proper Enterprise version:

    def increment(a):
        import urllib2
        url = "http://www.html2xml.nl/Services/Calculator/Version1/Calculator.asmx/Add?a=%d&b=1" % a       
        result = urllib2.urlopen(url).read() 
        from xml.dom.minidom import parseString
        result = parseString(result)
        return int(result.getElementsByTagName('int')[0].childNodes[0].data)
Re:Calculate n + 1 without using + or - or * or / by namank on Sunday July 03, 2011 @11:02AM (#2723792)
I'm pretty sure that's not a valid solution. Its the same as using Math.sum(). While it evaluates your resourcefulness, the point of the question, I think, is to figure out your knowledge and creative thinking
Re:Calculate n + 1 without using + or - or * or / by alexl1988 on Saturday July 02, 2011 @04:20AM (#2721298)

  int inc(int a) {
    int mask = ~a;
    /* Assuming a is 32-bit. If 64-bit, add 32-bit shift. */
    mask |= mask << 1;
    mask |= mask << 2;
    mask |= mask << 4;
    mask |= mask << 8;
    mask |= mask << 16;
    /* Take the longest run of 1s from the right of a.
     * mask has 0s there and 1s everywhere else. We want
     * to set the next bit to 1 and set the ones after
     * to 0. */
    return (a | ~(mask << 1)) & mask;
  }
Re:Calculate n + 1 without using + or - or * or / by LarryA on Saturday July 02, 2011 @10:27AM (#2721655)
Could do lookup tables like the old CADET system did. http://www.websters-online-dictionary.org/definitions/IBM+16...

In some situations you may be data rich but function poor. Like embedded devices where you have a limited instruction set, 128bytes of RAM, but access to megs of ROM for program/data.

Re:Calculate n + 1 without using + or - or * or / by veyron on Monday July 04, 2011 @06:48PM (#2728107)
Basically replicate the carry process in the form of bit ops:

    def inc(n):
        x = 1
        while n & 1 == 1:
            n = n >> 1
            x = x << 1
        n = n | 1
        while not x == 1:
            x = x >> 1
            n = n << 1
        return n
Re:Calculate n + 1 without using + or - or * or / by anujkk on Monday July 04, 2011 @03:41AM (#2725740)
If it is just about not using +,-,* or / operators, we can do it by using "++" operator.

x=n++;

Re:Calculate n + 1 without using + or - or * or / by awaage on Saturday July 02, 2011 @01:42AM (#2721097)
how about this (example in ruby): def plus_1(x); arr = []; arr[x] = true; return arr.size; end
Re:Calculate n + 1 without using + or - or * or / by kirillzubovsky on Saturday July 02, 2011 @01:52AM (#2721109)
Funny, this is the first thing I thought of, but then realized it would only work if x>0
Re:Calculate n + 1 without using + or - or * or / by awaage on Saturday July 02, 2011 @03:28AM (#2721243)
oops! good catch!
Re:Calculate n + 1 without using + or - or * or / by custominstall on Sunday July 03, 2011 @11:59PM (#2725326)
lol .. but also wont work for decimals
Re:Calculate n + 1 without using + or - or * or / by kirillzubovsky on Saturday July 02, 2011 @01:39AM (#2721089)
Re:Calculate n + 1 without using + or - or * or / by hasenj on Saturday July 02, 2011 @06:28AM (#2721409)
I don't think this counts; it is using the sum operation, even if the operator doesn't look like +
Re:Calculate n + 1 without using + or - or * or / by custominstall on Monday July 04, 2011 @12:44AM (#2725399)
php solution provided number is a positive int :

round($n.'.9')

Re:Calculate n + 1 without using + or - or * or / by code4pizza on Saturday July 02, 2011 @08:31AM (#2721525)
data Nat = Zero | Succ n

plus1 = Succ

Probably not the answer they were looking for, but it was the first thing that came into my head.

Re:Calculate n + 1 without using + or - or * or / by bobwebb on Saturday July 02, 2011 @06:30PM (#2722582)
def add_1(x): return sum([1,x])

Python.

Too simple? I didn't use any of the signs, and it demonstrates knowledge of the python standard library!

EDIT: Whoops, seems the idea of using the sum() function (or operator) has been dismissed elsewhere. Time to study some more languages, I think.

Re:Calculate n + 1 without using + or - or * or / by alecbenzer on Monday July 04, 2011 @05:52PM (#2727988)
if n is an integer, I believe this works:

https://gist.github.com/1063990

uses only bit shits, bitwise XOR, and bitwise AND

I would imagine they meant not to use the operations, not just the symbols.