当前位置:网站首页>第八章 操作位和位串(四)
第八章 操作位和位串(四)
2022-06-24 13:03:00 【yaoxin521123】
第八章 操作位和位串(四)
操作以整数形式实现的位串
设置位
要创建一个存储为整数的新位串,请对每个位求和 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 函数对存储为整数的位串执行按位逻辑运算。
对于此示例,假设有两个位串 a 和 b,存储为整数,以及一个 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。
边栏推荐
- 【无标题】
- The project manager needs to look at six characteristics to build a team
- P2PDB 白皮书
- pgsql查询分组中某个字段最大或者最小的一条数据
- 数学之英文写作——基本中英文词汇(几何与三角的常用词汇)
- ssh-keygen 配置无需每次输入密码
- js去除字符串空格
- Jerry has opened a variety of decoding formats, and the waiting time from card insertion to playback is long [chapter]
- Can a team do both projects and products?
- 谷歌WayMo提出R4D: 采用参考目标做远程距离估计
猜你喜欢

快手实时数仓保障体系研发实践

Go语言三个高效编程的技巧

Mit-6.824-lab4a-2022 (ten thousand words explanation - code construction)

Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire
![NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)](/img/b0/85ac6274b239e42c9543fa296df456.png)
NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)

21set classic case

Win10 system problems

专精特新“小巨人”再启动,“企业上云”数字赋能

【无标题】

AntD checkbox,限制选中数量
随机推荐
谷歌WayMo提出R4D: 采用参考目标做远程距离估计
A review of text contrastive learning
10_那些格调很高的个性签名
P2PDB 白皮书
数据库一些基本操作(提供了原数据库信息)
Zhiyuan community weekly 86: Gary Marcus talks about three linguistic factors that can be used for reference in large model research; Google puts forward the Wensheng graph model parti which is compar
MES在流程和离散制造企业的15个差别(下)
Second, the examinee must see | consolidate the preferred question bank to help the examinee make the final dash
Promotion of Project Manager
MIT-6.824-lab4A-2022(万字讲解-代码构建)
从谭浩强《C程序设计》上摘录的ASCII码表(常用字符与ASCII代码对照表)
Halcon draw area into picture
#21Set经典案例
10_那些格調很高的個性簽名
杰理之TIMER0 用默认的 PA13 来检测脉宽【篇】
[untitled]
Jupiter notebook operation
10 Ces autographes très stylisés.
初识云原生安全:云时代的最佳保障
Kunpeng arm server compilation and installation paddlepaddle