mulitiplication using bitwise operators

S

sandy_pt_in

Hi C guru's
can you please tell me how to do multiplication of 2 numbers using
bitwize operators?? expecting reply.
 
M

Morris Dovey

can you please tell me how to do multiplication of 2 numbers using
bitwize operators?? expecting reply.

To multiply:

[1] Initialize product to zero.
[2] While multiplicand is greater than zero, add (see below)
multiplier to product and decrement multiplicand.

To add:

[1] Initialize sum to augend
[2] Exclusive OR sum and addend
[3] AND addend and sum, shift this result left one position
[4] Result from [2] replaces sum
[5] Result from [3] replaces addend
[6] If addend isn't zero, continue from [2]
 
K

Keith Thompson

Hi C guru's
can you please tell me how to do multiplication of 2 numbers using
bitwize operators?? expecting reply.

Why would you want to do that?

If this is homework, we won't do it for you. If you have some other
reason to want to do this, you need to (1) explain why you can't just
use the "*" operator, and (2) show us some C code so we can help you
with it.

Off the top of my head, I'm sure it's possible, but it's probably
pretty difficult.
 
D

Dave Thompson

To multiply:
For nonnegative (or unsigned) integers only. Otherwise, add more
complexity. (Well, not add, that's not bitwise. Shift in?)
[1] Initialize product to zero.
[2] While multiplicand is greater than zero, add (see below)
multiplier to product and decrement multiplicand.
Decrement is not bitwise. Change that to add ~0U.
Or to the specialized algorithm: loop changing all trailing zero bits
to one and (then) the last one to zero.

Or, even better, use "Russian peasant":
[2] While multiplicand is greater than zero,
[2A] if low bit of multiplicand is set, add multiplier to product.
[2B] (always) shift multiplier left and multiplicand right.
To add:

[1] Initialize sum to augend
[2] Exclusive OR sum and addend
[3] AND addend and sum, shift this result left one position
[4] Result from [2] replaces sum
[5] Result from [3] replaces addend
[6] If addend isn't zero, continue from [2]

You could do full-adder logic with carry-save, and only complete the
ripples at the end. But I don't think it's feasible to do look-ahead.
Or at least not worth the trouble.

drHTHtOP :)

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top