How can I implement division using bit-wise operators (not just division by powers of 2)?
Describe it in detail.
The standard way to do division is by implementing binary long-division. This involves subtraction, so as long as you don't discount this as not a bit-wise operation, then this is what you should do. (Note that you can of course implement subtraction, very tediously, using bitwise logical operations.)
In essence, if you're doing Q = N/D
:
N
and D
.t = (N - D);
.(t >= 0)
, then set the least significant bit of Q
to 1, and set N = t
.N
by 1.Q
by 1.Loop for as many output bits (including fractional) as you require, then apply a final shift to undo what you did in Step 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With