美文网首页
第八章 操作位和位串(四)

第八章 操作位和位串(四)

作者: Cache技术分享 | 来源:发表于2022-06-24 09:27 被阅读0次

第八章 操作位和位串(四)

操作以整数形式实现的位串

设置位

要创建一个存储为整数的新位串,请对每个位求和 2 的幂:

set bitint = (2**2) + (2**5) + (2**10)
 
write bitint
1060

要将现有位串中的位设置为 1,请使用 $zboolean 函数(逻辑 OR)的选项7 (arg1 ! arg2)

set bitint = $zboolean(bitint, 2**4, 7)

write bitint
1076

要将现有位串中的位设置为 0,请使用 $zboolean 函数的选项 2 (arg1 & ~arg2)


set bitint = $zboolean(bitint, 2**4, 2)

write bitint
1060

要在现有位串中切换位,请使用 $zboolean 函数(逻辑 XOR)的选项 6 (arg1 ^ arg2)

set bitint = $zboolean(bitint, 2**4, 6)

write bitint
1076
set bitint = $zboolean(bitint, 2**4, 6)

write bitint
1060

测试位是否已设置

要将位字符串显示为整数,可以使用如下方法,该方法循环位并使用 $zboolean 函数:

Class Util.BitUtil Extends %RegisteredObject
{

/// w ##class(Util.BitUtil).LogicalToDisplay(1)
ClassMethod LogicalToDisplay(bitint As %Integer)
{
    s str = ""
    for i = 0 : 1 { 
        q:((2 ** i) > bitint)
        if $zboolean(bitint, 2 ** i, 1) {
            s str = str _ 1
        } else {
            s str = str _ 0
        }
    }
    q str
}

}

DHC-APP>w ##class(Util.BitUtil).LogicalToDisplay(101000)
00010001010100011

查找设置位

此方法使用$Zlog函数将位字符串中的哪些位设置为整数,该函数返回以10为底的对数值。该方法删除越来越小的位串块,直到没有剩余:

/// w ##class(Util.BitUtil).FindSetBits(2)
ClassMethod FindSetBits(bitint As %Integer)
{
    s bits = "" 
    while (bitint '= 0) { 
        s bit = $zlog(bitint) \ $zlog(2)
        s bits = bit _ " " _ bits
        s bitint = bitint - (2 ** bit) 
    } 
    q bits
}

DHC-APP>w ##class(Util.BitUtil).FindSetBits(3)
0 1

执行按位算术

使用 $zboolean 函数对存储为整数的位串执行按位逻辑运算。

对于此示例,假设有两个位串 ab,存储为整数,以及一个 LogicalToDisplay() 方法,如 Display Bits 中定义的,用于显示这些位。

do ##class(User.BitInt).LogicalToDisplay(a)
100110111
do ##class(User.BitInt).LogicalToDisplay(b)
001000101

使用 $zboolean 函数的选项 7 对位执行逻辑 OR

set c = $zboolean(a, b, 7)

do ##class(User.BitInt).LogicalToDisplay(c)
101110111

使用 $zboolean 函数的选项 1 对位执行逻辑与:

set d = $zboolean(a, b, 1)

do ##class(User.BitInt).LogicalToDisplay(d)
000000101

转换为常规位串

要将存储为整数的位串转换为常规位串,请使用 $factor 函数。对于此示例,假设有一个位串为整数的 bitint 和一个 FindSetBits() 方法,如 Find Set Bits 中所定义,以显示设置了哪些位。

do ##class(User.BitInt).FindSetBits(bitint)
2 5 10
set bitstring = $factor(bitint)

zwrite bitstring 
bitstring=$zwc(128,4)_$c(36,4,0,0)/*$bit(3,6,11)*/

请注意,常规位串中的位似乎向右移动了一位,因为位串没有位 0。位串中的第一位是位 1

相关文章

  • 第八章 操作位和位串(四)

    第八章 操作位和位串(四) 操作以整数形式实现的位串 设置位 要创建一个存储为整数的新位串,请对每个位求和 2 的...

  • 第七章 操作位和位串(三)

    第七章 操作位和位串(三) 操作位串 要创建新的位串,请使用 $bit 函数将所需位设置为 1: 使用 $bit ...

  • 第五章 操作位和位串

    第五章 操作位和位串 有时可能希望在基于数据平台的应用程序中存储一系列相关的布尔值。可以创建许多布尔变量,也可以将...

  • 第六章 操作位和位串(二)

    第六章 操作位和位串(二) 将位序列存储为整数 如果要将一系列布尔参数传递给方法,一种常见的方法是将它们作为编码为...

  • 在编程中使用集合与按位运算符

    目录 一、引言二、离散数学中的集合三、位串和按位运算符四、在编程中使用位串和按位运算符五、练习题六、拓展阅读 本文...

  • 位操作

    位操作乘、除、求余,需要乘以或除以2的n次方,都可以用移位的方法代替1、乘a=a*4 <=> a=a<<2 2、...

  • 位操作

    位操作详解 位运算的操作符有:&、|、^、~、>>、<<,六种,分别对应与,或,异或,按位取反,右位移,左位移 1...

  • 位操作

    c++中位操作操作符 这些位操作符只能用于整形的操作,其他会编译报错。位操作符的运算优先级比较低,因为尽量使用括号...

  • 位操作

    1. 把一个数上调成另一个数的倍数 2. 取某些位 取最低位, 可由数字得到相应的二进制字符串 3. 判断str与...

  • 位操作

    1.概念: ​ In digital computer programming, a bitwise ope...

网友评论

      本文标题:第八章 操作位和位串(四)

      本文链接:https://www.haomeiwen.com/subject/elevvrtx.html