当前位置:网站首页>第八章 操作位和位串(四)
第八章 操作位和位串(四)
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。
边栏推荐
- Rasa 3.x 学习系列-非常荣幸成为 Rasa contributors 源码贡献者,和全世界的Rasa源码贡献者共建共享Rasa社区!
- 杰理之TIMER0 用默认的 PA13 来检测脉宽【篇】
- In the era of knowledge economy, it will teach you to do well in knowledge management
- The real project managers are all closed-loop masters!
- 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
- Three efficient programming skills of go language
- Halcon 绘制区域 到图片中
- One click to generate University, major and even admission probability. Is it so magical for AI to fill in volunteer cards?
- 记录一次Mongotemplate的And和Or的各种套
- HarmonyOS.2
猜你喜欢

Three efficient programming skills of go language
![[R language data science] (XIV): random variables and basic statistics](/img/87/3606041a588ecc615eb8013cdf9fb1.png)
[R language data science] (XIV): random variables and basic statistics
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

智慧园区SaaS管理系统解决方案:赋能园区实现信息化、数字化管理

P2PDB 白皮书

Télétravail: Camping à la maison gadgets de bureau | rédaction communautaire

A review of text contrastive learning

2022 Quality Officer - Equipment direction - post skills (Quality Officer) recurrent training question bank and online simulation examination

unity 等高线创建方法

MySQL复合索引探究
随机推荐
杰理之开了多种解码格式插卡到播放等待的时间长【篇】
Jerrys timer0 uses the default pa13 to detect the pulse width [chapter]
Jericho may have some chips with fast music playing speed [chapter]
10_那些格調很高的個性簽名
[R language data science] (XIV): random variables and basic statistics
杰理之无缝循环播放【篇】
二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]
Digital business cloud: strengthen supplier management and promote efficient collaboration between air transport enterprises and suppliers
Rongyun communication has "hacked" into the heart of the bank
[deep learning] storage form of nchw, nhwc and chwn format data
HarmonyOS.2
AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)
Rasa 3. X learning series - it is a great honor to be a source code contributor of Rasa contributors, and to build and share the rasa community with rasa source code contributors all over the world!
Gatling performance test
markdown/LaTeX中在字母下方输入圆点的方法
90% of the project managers have skipped the pit. Are you still in the pit?
Baidu map API drawing points and tips
One click to generate University, major and even admission probability. Is it so magical for AI to fill in volunteer cards?
greendao使用问题
根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]