6

Is there a way to perform addition(or arithmetic operations) by using ONLY bitwise operators?

6
  • 10
    Yes.​​​​​​​​​​​​ Commented Nov 8, 2012 at 6:17
  • 1
    yes! do dump assembly code for multiplication and division and you can see it for your self. eg: int a = 2; int twice_a = a << 1; // left shift is multiplication by two and like wise I would recommend "Hacker's Delight" book for other good stuff that you can do with bitwise operations Commented Nov 8, 2012 at 6:18
  • 3
    Possible duplicate of stackoverflow.com/questions/4068033/…. Answer to that question is given in C. Commented Nov 8, 2012 at 6:18
  • 1
    Not a duplicate. This is a superset question, whereas the other concerns only addition. Commented Nov 8, 2012 at 6:20
  • 1
    Hardware is all bitwise operators, and eventually ALL operations are done in hardware, so yes. Even log10 is done bitwise. Commented Nov 8, 2012 at 9:41

3 Answers 3

4

You can search for some hardware designs for arithmetic operators. For addition example, you can find full adder, half adder, then ripple carry adder, carry save adder, carry lookahead adder. Then you can generate lots of code to use bit-wise operators to do those arithmetic operations.

Sign up to request clarification or add additional context in comments.

6 Comments

I would make it more clear in this answer that the only way that hardware performs addition and other arithmetic operations is with bitwise logical operations :)
@ktodisco But from that it doesn't follow that C/C++ bitwise operators can be used to describe these operations. There is a difference between bitwise operations in general, and the bitwise operators of C/C++.
@jogojapan There is? Yes the right shift is annoyingly unspecified, but you can fix that rather easily with a bit extra code and it's not as if implementing right shifts with a bit of looping and the more elemental bitwise operators was especially complicated.
@Voo What I mean is that for example the processor instruction ADD is of course a bitwise operation. When it is applied to two registers, it will perform addition by manipulating the bits in of the target register as necessary. But who says that this operation can be expressed using no more than &, |, ~? (We know it can, but this doesn't follow trivially from the fact that ADD is applied in a bitwise manner.)
Hmm no, I don't see how from "Add is implemented using only bitwise operations" it doesn't follow that "We can implement add using only bitwise operations". Whether I do the operations on paper, in my head, in a high level language like c++ or using 32nm CMOS transistors is rather immaterial really.
|
3

Addition of bits a,b and c

carry_out = b&c|a&b|a&c;  // there's a carry, if at least 2 bits are set
sum = a^b^c;              // the sum is the number of set bits modulo 2

One has to perform this for all bits in the word - first for bit 0 using carry_in = c = 0, and iterating to carry_in(next_bit) = carry_out(previous_result).

Subtraction happens with bit inverting b from (a-b) and setting the initial carry to 1.

However, if one has to add e.g 32 numbers in parallel, one can fit 'a' with 32 lsbs of all those numbers and perform the binary operations in parallel. This is a technique called bit slicing.

For multiplication CSA (carry save adder is definitely the best software approach -- it has the smallest "area")

As an exercise, here's an algorithm that calculates a+b(+c) in parallel:

int a = 13113;
int b = 43334;
int c =     1;

int main()
{
   int sum=a^b^c,c2;
   c=((c&a)|(a&b)|(c&b))<<1;
   while (c) {
     c2=(c&sum)<<1;
     sum^=c;
    c=c2;
   }
   printf("%d\n",sum);
}

Comments

0

Note that bitwise operators in C is only applicable to integral operands not on arithmetic operands

And yes you can do it using bitwise for all operations(+,-,*,/,%)

for e.g.

int a =10;
a<<1; // it's multiplication : a*2
a>>1; //it's division : a/2

I have shown this for 2 only. You can do for addition of two integers using the FULL ADDER (SUM and CARRY functios) for any number of bits

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.