 Bits - Maple Programming Help

Home : Support : Online Help : Programming : Bits : Bits/FirstNonzeroBit

Bits

 FirstNonzeroBit
 locate first nonzero bit from a location in a specified direction

 Calling Sequence FirstNonzeroBit(number) FirstNonzeroBit(number,dir) FirstNonzeroBit(number,dir,posn)

Parameters

 number - an integer dir - either higher or lower, indicating the direction of the search posn - bit offset to start search at

Description

 • 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.

Examples

 > $\mathrm{with}\left(\mathrm{Bits}\right):$
 > $\mathrm{FirstNonzeroBit}\left(255\right)=\mathrm{ilog2}\left(255\right)$
 ${7}{=}{7}$ (1)
 > $\mathrm{FirstNonzeroBit}\left(256\right)=\mathrm{ilog2}\left(256\right)$
 ${8}{=}{8}$ (2)
 > $\mathrm{FirstNonzeroBit}\left(0\right)\ne \mathrm{ilog2}\left(0\right)$
 ${-1}{\ne }{-}{\mathrm{\infty }}$ (3)
 > $\mathrm{FirstNonzeroBit}\left(1+2+4+256,\mathrm{higher}\right)$
 ${0}$ (4)
 > $\mathrm{FirstNonzeroBit}\left(1+2+4+256,\mathrm{higher},1\right)$
 ${1}$ (5)
 > $\mathrm{FirstNonzeroBit}\left(1+2+4+256,\mathrm{higher},2\right)$
 ${2}$ (6)
 > $\mathrm{FirstNonzeroBit}\left(1+2+4+256,\mathrm{higher},3\right)$
 ${8}$ (7)
 > $\mathrm{FirstNonzeroBit}\left(1+2+4+256,\mathrm{higher},9\right)$
 ${\mathrm{\infty }}$ (8)
 > $\mathrm{FirstNonzeroBit}\left(-1,\mathrm{lower}\right)$
 ${\mathrm{\infty }}$ (9)
 > $\mathrm{FirstNonzeroBit}\left(-1,\mathrm{lower},5\right)$
 ${5}$ (10)