-6

I need some help answering the above question Any twiddling idea?

KBE
  • 379
  • 3
  • 4
  • 15

2 Answers2

4

You'll get a lot more here: Bit Twiddling Hacks

Azodious
  • 13,752
  • 1
  • 36
  • 71
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