Tuesday, July 28, 2009

How can we add two numbers without using "+" operator?

impelimentaion in C lag

How can we add two numbers without using "+" operator?
um maybe absolute value ( |3|4| ). I might be wrong.
Reply:get a peice of paper- write down the two numbers and do the math! its simple just make sure you carry you numbers ;)
Reply:subtract.
Reply:Z = X + Y


Z = X - (-Y)





that's it!





And BigCityPimp doesn't know what he's talking about. 2 and 4 are powers of 2 of course. That does happen to be true but 2+10=12 and 2|10 = 12 also.





looking at the bits


2 is 0010


4 is 0100





It's also true that 1+2 =3 and 1| 2 = 3





one is not a power of 2!





He really means that a number that is OR'd with another number is the same as a number + another number if and only if that in binary, the bits set for one number has no bits in common with the bit set in the other number





3 = 0011


12 = 1100


3+12 = 15 (1111 in binary)


3|12 = 15 also
Reply:If the numbers are powers of 2 you can use the bitwise | (or) operator





2+4 = 6





is the same as





2 | 4 = 6 as well.


No comments:

Post a Comment