Country: United States
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
22
of 22 vote

``````int add (int a, int b)
{
int carry, sum;
if (b == 0)
{
return a;
}

sum = a ^ b; // xor takes sum
carry = a & b; // collect carry;
carry = carry << 1;
return ( add (sum, carry) );
}``````

Like the way I learnt adding numbers when I'm a kid. Additionally, no math operator is used

Comment hidden because of low score. Click to expand.
0

Good, but honestly I'd give you points off for the variable names you've chosen. Why not just call it "sum" and "carry" or something?

Comment hidden because of low score. Click to expand.
0

Oops! Dint pay much attention to them :)
Nevertheless corrected.

Thnx.

Comment hidden because of low score. Click to expand.
0

Actually, I'm gonna post the easiest implementation of add and minus here:
int add (int a, int b)
{
do a^=b; while (b = (~a&b) << 1);
return a;
}
int minus (int a, int b)
{
do a^=b; while(b = (a&b) << 1);
return a;
}

See how subtle the difference is ~a or a

Comment hidden because of low score. Click to expand.
16
of 18 vote

return(a-(-b))

Comment hidden because of low score. Click to expand.
0

Nice one mate :) :)

Comment hidden because of low score. Click to expand.
0

The way I've seen this asked isn't like it was asked here. Implement addition of unsigned integers without any arithmetic operators.

Comment hidden because of low score. Click to expand.
0

They are unsigned integers. So, as far as I can tell, there is no way to represent negative numbers.

For signed integers, the first bit tells whether the number is positive or negative. For unsigned, the first bit is part of the number and hence, only positive numbers are allowed.

So, -b can't be represented using unsigned numbers.

Comment hidden because of low score. Click to expand.
0

cool!

Comment hidden because of low score. Click to expand.
0

oh ...

Comment hidden because of low score. Click to expand.
0

Funny, but nice. +1

Comment hidden because of low score. Click to expand.
7
of 7 vote

Well use a+b = (a^2 - b^2)/(a-b)
If a=b, just do a+b=2*a

as simple as that!

Comment hidden because of low score. Click to expand.
0

As I mentioned in the other comment, negative numbers can't be represented using unsigned numbers. In this example, if a < b, then a- b will be negative and hence, can't be assigned to an unsigned integer.

Comment hidden because of low score. Click to expand.
2
of 2 vote

Proof is shown for an unsigned byte (0xFF = 255). However the math works out the same for all unsigned types.

x + y = 255 + (-255 + x + y)
x + y = 255 - (255 - x - y)
x + y = 255 - ((255 - x) - y)
x + y = 255 - ((~x) - y)
x + y = ~((~x) - y)

To implement for an unsigned int:

``````unsigned int Add(unsigned int x, unsigned int y)
{
return ~((~x) - y);
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

bool carry(bool ci, bool a, bool b)
{
bool car = (~ci & a & b) | (ci & ~a & b) | (ci & a & ~b) | (ci & a & b);
return car;
}

bool sum(bool ci, bool a, bool b)
{
bool s = (~ci & ~a & b) | (~ci & a & ~b) | (ci & ~a & ~b) | (ci & a & b);
return s;
}

{
int a1 = 1, b1 = 1,ci = 0, output = 0, totalsum = 0;
bool c2 = 0;

for(int i =0; i < 8; i++)
{
bool a0 = a & a1;
bool b0 = b & a1;
c2 = carry(ci, a0, b0);
int s= sum(ci, a0, b0);
s = s<<(i);
totalsum = totalsum | s;
ci = c2;
a1 = a1<<1;

}
cout<<totalsum<<endl;
}

int main()
{

return 0;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````// implement 1
unsigned Add(unsigned a, unsigned b) {
if(!a)return b;
if(!b)return a;
}
//implement 2
unsigned Add(unsigned a, unsigned b, unsigned carry) {
if(!(a|b))return carry; //both a and b is zero.
if(!((a|b)&1)) return Add(a>>1,b>>1,0)<<1|carry; // the lowest bit of a and b is 0
if(!(a&b&1)) return Add(a>>1,b>>1,carry)<<1|(!carry); //the lowest bit of a and b must be 1 one and 1 zero
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

(A-(0xFFFFFFFF-B)-1) and (A+B) is modulus 2^32 congruences.

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````uint32 sum(uint32 a, uint32 b) {
if(a==b)
return 2*a;
if(a>b)
return ((a*a - b*b) / (a - b));
return ((b*b - a*a) / (b-a));``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

A solution is to consider the add operation in two passes:
* first add bits set only in one of the number (logical or, and reverse bits set by logical and)
* remaining case is when the two bits are set to 1 (spotted by the logical and), and in such case we have to add the carry, that is, the bit position shifted - multiple carries may be present, and we need to loop until no carry is still there

Iterative version:

``````unsigned int sum(unsigned int a, unsigned int b) {
for(;;) {
const unsigned int x = a & b;
a |= b;
if (x == 0)
break;
a ^= x;
b = x << 1;
}
return a;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````private static int add(int i, int j) {
if (i == 0 || j == 0) {
return i | j;
}
return add(i ^ j, (i & j) << 1);
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````u32 add_u32(u32 a, u32 b)
{
u32 tmp;

while (b) {
tmp = (a & b) << 1;
a ^= b;
b = tmp;
}

return a;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

int sum (int a,int b)
{
return ( a - ( (-1)*b ) );
}

bhooyaa!

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````function add(a, b) {
return Math.log(Math.pow(Math.E, a) * Math.pow(Math.E, b));
}``````

This isn't really useful in real life thought since the product grows too quickly.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

``````#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
long long x = atoi(argv);
long long y = atoi(argv);

long long S=0;

if(x==0 || y == 0) {
printf("%lld * %lld = %lld\n", x, y, S);
return 0;
}

long long t = x;
while(t){
S += y;
t--;
}
printf("%lld * %lld = %lld\n", x, y, S);
return S;
}``````

Comment hidden because of low score. Click to expand.
0

no + operator to be used dude!

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.