I have setup this macro that returns the bit number of a given flag mask...
0x00000001 will result in 0
0x00001000 will result in 12
0x00008000 will result in 15
0x00010000 will result in 16
0x80000000 will result in 31
When a mask exceeds 32 bit or or any combination of flags are OR-ed together the result is 32.
The macro works fine, and after experimenting I thought it was the best solution;
A seeking loop with shifts and compares...
(a)Use 2-3 times more processing
(b)Cannot result in a value but must set an operand because it has more than one statement.
Still I wonder if a macro exist, that does the same, but in a smarter, shorter, more efficient and elegant way?
0x00000001 will result in 0
0x00001000 will result in 12
0x00008000 will result in 15
0x00010000 will result in 16
0x80000000 will result in 31
When a mask exceeds 32 bit or or any combination of flags are OR-ed together the result is 32.
Code:
//Macro that results in a bitmask
#define BITNMASK(bitn) ((DWORD) (((DWORD) 1) << (bitn)) )
//Macro that results in a bit number
#define MASKBITN(mask) ((((DWORD)(mask)) == 0x00000001) ? ( 0 ) : ( (((DWORD)(mask)) == 0x00000002) ? ( 1 ) : ( (((DWORD)(mask)) == 0x00000004) ? ( 2 ) : ( (((DWORD)(mask)) == 0x00000008) ? ( 3 ) : ( (((DWORD)(mask)) == 0x00000010) ? ( 4 ) : ( (((DWORD)(mask)) == 0x00000020) ? ( 5 ) : ( (((DWORD)(mask)) == 0x00000040) ? ( 6 ) : ( (((DWORD)(mask)) == 0x00000080) ? ( 7 ) : ( (((DWORD)(mask)) == 0x00000100) ? ( 8 ) : ( (((DWORD)(mask)) == 0x00000200) ? ( 9 ) : ( (((DWORD)(mask)) == 0x00000400) ? ( 10 ) : ( (((DWORD)(mask)) == 0x00000800) ? ( 11 ) : ( (((DWORD)(mask)) == 0x00001000) ? ( 12 ) : ( (((DWORD)(mask)) == 0x00002000) ? ( 13 ) : ( (((DWORD)(mask)) == 0x00004000) ? ( 14 ) : ( (((DWORD)(mask)) == 0x00008000) ? ( 15 ) : ( (((DWORD)(mask)) == 0x00010000) ? ( 16 ) : ( (((DWORD)(mask)) == 0x00020000) ? ( 17 ) : ( (((DWORD)(mask)) == 0x00040000) ? ( 18 ) : ( (((DWORD)(mask)) == 0x00080000) ? ( 19 ) : ( (((DWORD)(mask)) == 0x00100000) ? ( 20 ) : ( (((DWORD)(mask)) == 0x00200000) ? ( 21 ) : ( (((DWORD)(mask)) == 0x00400000) ? ( 22 ) : ( (((DWORD)(mask)) == 0x00800000) ? ( 23 ) : ( (((DWORD)(mask)) == 0x01000000) ? ( 24 ) : ( (((DWORD)(mask)) == 0x02000000) ? ( 25 ) : ( (((DWORD)(mask)) == 0x04000000) ? ( 26 ) : ( (((DWORD)(mask)) == 0x08000000) ? ( 27 ) : ( (((DWORD)(mask)) == 0x10000000) ? ( 28 ) : ( (((DWORD)(mask)) == 0x20000000) ? ( 29 ) : ( (((DWORD)(mask)) == 0x40000000) ? ( 30 ) : ( (((DWORD)(mask)) == 0x80000000) ? ( 31 ) : ( 32 ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ))
A seeking loop with shifts and compares...
(a)Use 2-3 times more processing
(b)Cannot result in a value but must set an operand because it has more than one statement.
Still I wonder if a macro exist, that does the same, but in a smarter, shorter, more efficient and elegant way?