第四章 Caché 函数大全 $BITFIND 函数

2020-08-25  本文已影响0人  Cache技术分享

第四章 Caché 函数大全 $BITFIND 函数

返回指定位值在位串中的位置。

大纲

$BITFIND(bitstring,bitvalue,position,direction)

参数

描述

$BITFIND(bitstring,bitvalue)返回指定位值(0或1)在位串位串中首次出现的位置。位位置从1开始计数。

$BITFIND(bitstring,bitvalue,position)返回在字符串中指定位值的位置处或之后的第一个匹配项的位置。

如果找不到所需的位值,或者位置(向前搜索)大于位串中的位数,则返回值为0。如果指定的位串是未定义的变量,则返回值为0。指定的位串不是有效的位串,将发出<INVALID BIT STRING>错误。

示例

如果bitstring = [0,0,1,1,0],则$BITFIND(bitstring,1)的结果为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

如果bitstring = [0,0,1,1,0],则从位置3搜索时,值1的第一位是位位置3(因为搜索包括位置位),而值0的第一位是bit位置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

如果bitstring = [0,0,1,1,0],则从位置99向后搜索时,值1的第一位是位4,而值0的第一位是位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

以下示例返回由$FACTOR生成的随机16位位串中前1位的位置:

/// 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
上一篇 下一篇

猜你喜欢

热点阅读