Arithmetic operators using bitwise operators

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

+6
source share
3 answers

Adding bits a, b, and c

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

You need to do this for all bits in the word - first for bits 0 using carry_in = c = 0, and iterating to carry_in (next_bit) = carry_out (previous_result).

Subtraction occurs with bit inverting b from (ab) and setting the initial transfer to 1.

However, if you need to add, for example, 32 numbers in parallel, you can put "a" with 32 pounds of all these numbers and perform binary operations in parallel. This is a method called bit slicing.

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

As an exercise, here is an algorithm that computes 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); } 
+3
source

You can search for some hardware circuits for arithmetic operators. For an example of adding, you can find the full adder, half of the summation, then the ripples summing the bag, transferring the saving adder, summing the adder. You can then generate a lot of code to use bitwise operators to perform these arithmetic operations.

+4
source

Note that bitwise statements in C apply only to integral operands not on arithmetic operands

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

eg,

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

I showed it for only 2 . You can do to add two integers using FULL ADDER (SUM and CARRY functios) for any number of bits

0
source

All Articles