Chapter 4 Caché Function Collection $BITFIND Function

Article Directory

Chapter 4 Caché Function Collection $BITFIND Function

Returns the position of the specified bit value in the bit string.

Outline

$BITFIND(bitstring,bitvalue,position,direction)

parameter

  • bitstring The calculation result is an expression of bit string. It can be any type of variable, $FACTORuser-defined function or oref.prop, .. propor i%propattribute reference.
  • bitvalue The value (0 or 1) to be searched in the bit string.
  • position Optional-the bit position to start the search, specified as a positive integer. The BIT position starts counting from the beginning of the bit string. The search includes this location. The position value 0 is regarded as the designated position 1.
  • direction Optional-direction mark. Available values ​​are 1 and -1. 1 = Search forward (from left to right) from the beginning (or position) of the bit string to the end (this is the default value). -1 = Search backward from the end (or position) of the bit string.

description

$BITFIND(bitstring,bitvalue)Returns the position where the specified bit value (0 or 1) first appears in the bit string. The bit position starts counting from 1.

$BITFIND(bitstring,bitvalue,position)Returns the position of the first match at or after the position of the specified bit value in the string.

If the required bit value cannot be found, or the position (search forward) is greater than the number of digits in the bit string, the return value is 0. If the specified bit string is an undefined variable, the return value is 0. The specified bit string is not a valid bit string, and an <INVALID BIT STRING>error will be issued .

Example

If bitstring = [0,0,1,1,0], then $BITFIND(bitstring,1)the result is 3:

/// d ##class(PHA.TEST.Function).BITFIND()
ClassMethod BITFIND()
{
    
    
	// 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)
}
DHC-APP>d ##class(PHA.TEST.Function).BITFIND()
 
3

If bitstring = [0,0,1,1,0], when searching from position 3, the first bit of value 1 is bit position 3 (because the search includes position bits), and the first bit of value 0 is bit position 5:

/// d ##class(PHA.TEST.Function).BITFIND1()
ClassMethod BITFIND1()
{
    
    
	// 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)
}
DHC-APP> d ##class(PHA.TEST.Function).BITFIND1()
 
found a 1 at bit position:3
found a 0 at bit position:5

If bitstring = [0,0,1,1,0], when searching backward from position 99, the first bit of value 1 is bit 4, and the first bit of value 0 is bit 5:

/// d ##class(PHA.TEST.Function).BITFIND2()
ClassMethod BITFIND2()
{
    
    
	// 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)
}

DHC-APP>d ##class(PHA.TEST.Function).BITFIND2()
 
found a 1 at bit position:4
found a 0 at bit position:5

The following example returns the $FACTORposition of the first 1 bit in the random 16-bit string generated by:

/// d ##class(PHA.TEST.Function).BITFIND3()
ClassMethod BITFIND3()
{
    
    
	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)
}
DHC-APP>d ##class(PHA.TEST.Function).BITFIND3()
1010000100000010
The first 1 bit is at position 1
DHC-APP>d ##class(PHA.TEST.Function).BITFIND3()
0010111110110100
The first 1 bit is at position 3
DHC-APP>d ##class(PHA.TEST.Function).BITFIND3()
0001000000111111
The first 1 bit is at position 4

Guess you like

Origin blog.csdn.net/yaoxin521123/article/details/108213337