ObjectScript Reference
$BITFIND


$BITFIND(bitstring,bitvalue,position,direction)
bitstring  An expression that evaluates to a bitstring. Can be a variable of any type, $FACTOR, a userdefined function, or an oref.prop, ..prop, or i%prop (instance variable) property reference. 
bitvalue  The value (0 or 1) to search for within the bitstring. 
position  Optional — The bit position from which the search begins, specified as a positive integer. Bit positions are counted from 1 from the beginning of the bit string. Search is inclusive of this position. A position value of 0 is treated as specifying position 1. 
direction  Optional — A direction flag. Available values are 1 and 1. 1 = Search forward (left to right) from the beginning of the bitstring (or from position) towards the end (this is the default). 1 = Search backward from the end of the bitstring (or from position) towards the beginning. 
// Set a to [0,0,1,1,0] SET $BIT(a,1) = 0 SET $BIT(a,2) = 0 SET $BIT(a,3) = 1 SET $BIT(a,4) = 1 SET $BIT(a,5) = 0 // Find first 1 bit within a WRITE !,$BITFIND(a,1)
// Set a to [0,0,1,1,0] SET $BIT(a,1) = 0 SET $BIT(a,2) = 0 SET $BIT(a,3) = 1 SET $BIT(a,4) = 1 SET $BIT(a,5) = 0 // Find first 1 bit from position 3 WRITE !,"found a 1 at bit position:",$BITFIND(a,1,3) // Find first 0 bit from position 3 WRITE !,"found a 0 at bit position:",$BITFIND(a,0,3)
// Set a to [0,0,1,1,0] SET $BIT(a,1) = 0 SET $BIT(a,2) = 0 SET $BIT(a,3) = 1 SET $BIT(a,4) = 1 SET $BIT(a,5) = 0 WRITE !,"found a 1 at bit position:",$BITFIND(a,1,99,1) WRITE !,"found a 0 at bit position:",$BITFIND(a,0,99,1)
// Set a to [0,0,1,1,0] SET $BIT(a,1) = 0 SET $BIT(a,2) = 0 SET $BIT(a,3) = 1 SET $BIT(a,4) = 1 SET $BIT(a,5) = 0 SET pos=0 WRITE !,"Bit positions with value 1: " FOR { SET pos=$BITFIND(a,1,pos+1) QUIT:'pos WRITE pos,", " } WRITE !,"Bit positions with value 0: " FOR { SET pos=$BITFIND(a,0,pos+1) QUIT:'pos WRITE pos,", " }
SET x=$RANDOM(65536) FOR i=1:1:16 {WRITE $BIT($FACTOR(x),i) } WRITE !,"The first 1 bit is at position ",$BITFIND($FACTOR(x),1)