0
How by using bitwise operators can you add numbers?
Title ^
5 ответов
+ 1
In general by using bitwise OR operator. But, considering bitwise operator works at bit level, if the bit was already set, addition isn't happening.
For example;
BIN DEC
0001 1
0010 2
------------ | (OR)
0011 3
0011 3
0010 2
------------ | (OR)
0011 3
# no addition, bit (2) is already set
P.S. I'm not too sure this was what you asked for, so perhaps you could elaborate further what it is in thread's Description.
+ 1
Yahel,
This is in regards to C# but I guess it works pretty much the same in common languages.
https://stackoverflow.com/questions/4068033/add-two-integers-using-only-bitwise-operators
0
Ipang you added 1 and 2 and you got 3 - as it should.
When you added 3 and 2 - you got 3.
I want to make it that I doesn't matter what 2 numbers I give as input, the output will be the sum of the numbers.
0
You can do it similar to how the CPU does it:
a = a ^ b
b = (a & b) << 1
repeat until b is 0 or for the number of bits that you have