Bits
FirstNonzeroBit
locate first nonzero bit from a location in a specified direction
Calling Sequence
Parameters
Description
Examples
FirstNonzeroBit(number)
FirstNonzeroBit(number,dir)
FirstNonzeroBit(number,dir,posn)
number
-
an integer
dir
either higher or lower, indicating the direction of the search
posn
bit offset to start search at
The FirstNonzeroBit command locates the first nonzero bit in a number in a specified direction from a specified location.
By default, the command searches in the lower direction starting at the most significant bit in the number. In this way, calling it with no additional arguments gives an identical result as the ilog2 command.
Note that for negative numbers, the most significant bit does not exist, so infinity is returned.
If dir is specified as lower, then the number will be searched in the direction of the most significant bit to the least significant bit.
If posn is not specified, it defaults to the most significant bit in the number.
If there are no bits lower than posn, -1 is returned.
If dir is specified as higher, then the number will be searched in the direction of the least significant bit to the most significant bit.
If posn is not specified, it defaults to 0.
If there are no bits higher than posn, infinity is returned.
with⁡Bits:
FirstNonzeroBit⁡255=ilog2⁡255
7=7
FirstNonzeroBit⁡256=ilog2⁡256
8=8
FirstNonzeroBit⁡0≠ilog2⁡0
−1≠−∞
FirstNonzeroBit⁡1+2+4+256,higher
0
FirstNonzeroBit⁡1+2+4+256,higher,1
1
FirstNonzeroBit⁡1+2+4+256,higher,2
2
FirstNonzeroBit⁡1+2+4+256,higher,3
8
FirstNonzeroBit⁡1+2+4+256,higher,9
∞
FirstNonzeroBit⁡−1,lower
FirstNonzeroBit⁡−1,lower,5
5
See Also
ilog2
Download Help Document