471,831 Members | 1,197 Online

# how to divide or multiply two numbers with using bitwise operator

please can anyone tell me how to multiply or divide,or add or subtracted two
numbers
Oct 8 '06 #1
3 24233
D_C
293 100+
To multiply and divide by powers of two, use the bitwise shift operator.

dividend / 2^n = (dividend >> n)
factor * 2^n = (factor << n).

bitwise addition and subtraction is done using xor.
Expand|Select|Wrap|Line Numbers
1.  1101
2. ±0110
3. =====
4.  1011
5. +c(b)
6. =====
7.  xxxx
Where c(b) is the carry in terms of bits. The carry bits depend on whether it's addition or subtraction that's being performed, and maybe whether the numbers are signed or unsigned.
Oct 8 '06 #2
that was nice of u.

But how to multply 4*3=12
Oct 9 '06 #3
D_C
293 100+
3 << 2, since 2^2 = 4.

If you mean 3*5, that's more trouble. You would have to do
Expand|Select|Wrap|Line Numbers
1. 011   // 100
2.  000  //  00
3.   011 //   1
4. ===== // ===
5. 01111 // 101
Why don't you look at how multipliers are implemented in hardware. The idea is to put it into binary, and if the bit is a 1, shift it appropriately, and add it. Do that for all bits. Just like we learned in grade school, but with base two instead of base ten.
Oct 9 '06 #4