_BitScanReverse, _BitScanReverse64
Microsoft Specific
Search the mask data from most significant bit (MSB) to least significant bit (LSB) for a set bit (1).
unsigned char _BitScanReverse( unsigned long * Index, unsigned long Mask ); unsigned char _BitScanReverse64( unsigned long * Index, unsigned __int64 Mask );
// BitScanReverse.cpp
// compile with: /EHsc
#include <iostream>
#include <intrin.h>
using namespace std;
#pragma intrinsic(_BitScanReverse)
int main()
{
unsigned long mask = 0x1000;
unsigned long index;
unsigned char isNonzero;
cout << "Enter a positive integer as the mask: " << flush;
cin >> mask;
isNonzero = _BitScanReverse(&index, mask);
if (isNonzero)
{
cout << "Mask: " << mask << " Index: " << index << endl;
}
else
{
cout << "No set bits found. Mask is zero." << endl;
}
}