thejohndonson
thejohndonson

Reputation: 169

Finding most significant and least significant bit set in a 64-bit integer?

I'm trying to get this done in a C++ program on Windows, using visual C++. I only need to support 64-bit targets. I know about hacks that use division or multiplication to get the info, but I'd like to know if there's a faster non-generic way to do this... I would even consider inline assembly but you can't do that in VS for 64-bit.

Upvotes: 2

Views: 437

Answers (1)

martona
martona

Reputation: 5914

If code portability is not an issue you should try _BitScanForward64 and _BitScanReverse64. They're compiler intrinsics and map to a single, efficient assembler instruction.

Upvotes: 1

Related Questions