I need some help answering the above question Any twiddling idea?
Asked
Active
Viewed 118 times
-6
-
Are both numbers just any numbers unknown in advance? The question *could* make sense for the opposite case, e.g. if one of the numbers is constant. – Anton Kovalenko Feb 12 '13 at 12:33
-
[Been asked](http://stackoverflow.com/q/12697523/968261). – Alexey Frunze Feb 12 '13 at 12:34
-
Just 2 simple integers unsigned. – KBE Feb 12 '13 at 12:41
-
for example multiply 2 unsiged integers using only shift right and shift left. – KBE Feb 12 '13 at 13:52
2 Answers
3
Treat one of the inputs as a bitmask. For each bit in it that is set, you want to add the other input, shifted that many spaces left, to your result. This assumes unsigned inputs: non-2's-complement signed inputs require special treatment of the sign bit.
I think I can safely predict that this will be less efficient than the CPU's built-in multiply operation.

Steve Jessop
- 273,490
- 39
- 460
- 699