I have a variable vBit which is an unsigned int64. I know there is exactly one bit set, and I need to figure out which one it is. Currently I do it like this (in Delphi):
vPos := -1;
repeat
vBit := vBit shr 1;
inc(vPos);
until vBit = 0;
Is there a faster way? All bit positions are equally likely, so on average the algorithm needs to iterate 32 times. I am looking for an elegant trick with ands and xors and whatnot.
Method 1 (Using Left Shift Operator) 1) Left shift given number 1 by k-1 to create a number that has only set bit as k-th bit. temp = 1 << (k-1) 2) If bitwise AND of n and temp is non-zero, then result is SET else result is NOT SET.
Setting a bitUse the bitwise OR operator ( | ) to set a bit. number |= 1UL << n; That will set the n th bit of number . n should be zero, if you want to set the 1 st bit and so on upto n-1 , if you want to set the n th bit.
It can be 18446744073709551615 . Show activity on this post. Your assumption for the maximum size of signed and unsigned integers is correct. The actual values are 9223372036854775807 for signed and 18446744073709551615 for unsigned.
A 64-bit signed integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).
Finding the first bit set is the same as counting the zero bits, so this hack might help. That's a really useful page to bookmark, by the way.
You might do an and with $FFFFFFFF00000000 and if it is non zero add 32 next you and with $FFFF0000FFFF0000 and if non zero, add 16 etc etc. In the end you have your answer and it is very fast:
Result := Ord( ( Val and $FFFFFFFF00000000 ) <> 0 ) * 32 +
Ord( ( Val and $FFFF0000FFFF0000 ) <> 0 ) * 16 +
Ord( ( Val and $FF00FF00FF00FF00 ) <> 0 ) * 8 +
Ord( ( Val and $F0F0F0F0F0F0F0F0 ) <> 0 ) * 4 +
Ord( ( Val and $CCCCCCCCCCCCCCCC ) <> 0 ) * 2 +
Ord( ( Val and $AAAAAAAAAAAAAAAA ) <> 0 );
This works only if a single bit is set!
Note: I did not test the routine shown above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With