0
c code
C program for binary multiplication/division using bitwise operator If anyone who knows just give me some idea.
2 Réponses
+ 2
simple one is
x<<1 // x*2
and
x>>1 // x/2
and its only work with 2. if you want more complex one it's same as normal multiplication
0010 //2
0110 //6
______
0000
0010
0010
0000
_________
0001100 //12
0
Supposing that:
- You talking about operations between integers (and not floats)
- You take cure of use correct representation of negative integers (like popular complement two)
- you know how implement an adder in binary
try to think about how you learned multiplication to school.. Starting with multiplication (base 10):
22 x 13 = 22 x (10+3)
= (22x10) + (22x3) = 220 + 66 = 286
and in base 2:
10110 x 1101 = 10110 x (1000 + 100 + 1)
= 10110 x (1<<3 + 1<<2 + 1)
= (10110 x 1)<<3 + (10110 x 1) <<2 + 10110
= (10110<<3) + (10110<<2) + 10110
= 10110000 + 1011000 + 10110
= 1011 0000+
101 1000 + = 1 0000 1000+
1 0110 = 1 0110=
--------------------------------------
1 0001 1110
That in decimal is 286 like wanted. Take note that using left shift x<<n mean effectivly x*2^n.
For division you can use repeated subtractions method like:
Operation: 19 / 3
res = 0
19 - 3 = 16 (res=1)
16-3= 13 (res=2)
13- 3= 10 (res=3)
10 - 3= 7 (res=4)
7 - 3= 4 (res=5)
4 - 3= 1 (res=6)
at this point 1 < 3 then you have to stop. Result is obtained by using a var (res in example) that count how many division you made
Obliviously these are VERY BASIC algorithms (exist much more complex ones) but i think that is enough for you