当前位置:网站首页>Chapter 7 operation bit and bit string (III)
Chapter 7 operation bit and bit string (III)
2022-06-24 09:08:00 【yaoxin521123】
List of articles
Chapter vii. Operation bits and bit strings ( 3、 ... and )
Operation bit string
To create a new bit string , Please use $bit The function sets the desired bit to 1:
kill bitstring
set $bit(bitstring, 3) = 1
set $bit(bitstring, 6) = 1
set $bit(bitstring, 11) = 1
Use $bit Set the bits in the existing bit string to 1:
set $bit(bitstring, 5) = 1
Use $bit Set the bits in the existing bit string to 0:
set $bit(bitstring, 5) = 0
Because the first bit in the bit string is bit 1, So try to set the bit 0 Will return an error :
set $bit(bitstring, 0) = 1
SET $BIT(bitstring, 0) = 1
^
<VALUE OUT OF RANGE>
Test whether the bit is set
To test whether a bit is set in an existing bit string , You can also use $bit function :
write $bit(bitstring, 6)
1
write $bit(bitstring, 5)
0
If the test does not explicitly set the bit , be $bit return 0:
write $bit(bitstring, 4)
0
write $bit(bitstring, 55)
0
Display bit
To display the bits in the bit string , Please use $bitcount Function to get the count of bits in a bit string , Then traverse the bit :
for i=1:1:$bitcount(bitstring) {
write $bit(bitstring, i)}
00100100001
You can also use $bitcount To calculate... In a bit string 1 or 0 The number of :
write $bitcount(bitstring, 1)
3
write $bitcount(bitstring, 0)
8
Find the setting bit
To find which bits are set in the bit string , Please use $bitfind function , This function returns the position of the next bit of the specified value , Start at a given position in the bit string :
Class User.BitStr
{
ClassMethod FindSetBits(bitstring As %String)
{
set bit = 0
for {
set bit = $bitfind(bitstring, 1, bit)
quit:'bit
write bit, " "
set bit = bit + 1
}
}
}
This method searches for a string and displays it in $bitfind return 0 Exit from time , Indicates that no more matches were found .
do ##class(User.BitStr).FindSetBits(bitstring)
3 6 11
Be very careful when testing bit string comparisons .
for example , Can have two bit strings b1 and b2, They have the same bit set :
do ##class(User.BitStr).FindSetBits(b1)
3 6 11
do ##class(User.BitStr).FindSetBits(b2)
3 6 11
However , If you compare them , You will find that they are not actually equal :
write b1 = b2
0
If you use zwrite, You can see that the internal representations of the two bit rings are different :
zwrite b1
b1=$zwc(405,2,2,5,10)/*$bit(3,6,11)*/
zwrite b2
b2=$zwc(404,2,2,5,10)/*$bit(3,6,11)*/
under these circumstances ,b2 Will be the first 12 Bit is set to 0:
for i=1:1:$bitcount(b1) {
write $bit(b1, i)}
00100100001
USER>for i=1:1:$bitcount(b2) {
write $bit(b2, i)}
001001000010
Besides , There may also be other internal representations , For example, by $factor Created representation , It converts an integer to a bit string :
set b3 = $factor(1060)
zwrite b3
b3=$zwc(128,4)_$c(36,4,0,0)/*$bit(3,6,11)*/
for i=1:1:$bitcount(b3) {
write $bit(b3, i)}
00100100001000000000000000000000
To compare two bit strings that may have different internal representations , have access to $bitlogic Function to directly compare the set bits , As described in performing bitwise arithmetic .
Perform bitwise arithmetic
Use $bitlogic Function performs bitwise logical operations on bit strings .
In this example , There are two bit strings a and b.
for i=1:1:$bitcount(a) {
write $bit(a, i)}
100110111
for i=1:1:$bitcount(b) {
write $bit(b, i)}
001000101
Use $bitlogic Functions perform logical or... On bits :
set c = $bitlogic(a|b)
for i=1:1:$bitcount(c) {
write $bit(c, i)}
101110111
Use $bitlogic The function performs logical and :
set d = $bitlogic(a&b)
for i=1:1:$bitcount(d) {
write $bit(d, i)}
000000101
This example shows how to use $bitlogic Functions perform logic XOR To test two bit strings b1 and b3 Whether it has the same set bit , Regardless of the internal representation :
zwrite b1
b1=$zwc(405,2,2,5,10)/*$bit(3,6,11)*/
zwrite b3
b3=$zwc(128,4)_$c(36,4,0,0)/*$bit(3,6,11)*/
write $bitcount($bitlogic(b1^b3),1)
0
Logical XOR can quickly indicate that there is no difference between the set bits of two bit strings .
Convert to bit string integer
To convert a regular bit string to a bit string stored as an integer , Please use $bitfind The function finds the set bits and converts their 2 Power addition . Remember to divide the result by 2 To move the bit to the left , Because the bits in the regular bit string 1 Corresponds to the bit in the bit string 0.
ClassMethod BitstringToInt(bitstring As %String)
{
set bitint = 0
set bit = 0
for {
set bit = $bitfind(bitstring, 1, bit)
quit:'bit
set bitint = bitint + (2**bit)
set bit = bit + 1
}
return bitint/2
}
Converts a bit string to an integer :
for i=1:1:$bitcount(bitstring) {
write $bit(bitstring, i)}
00100100001
set bitint = ##class(User.BitStr).BitstringToInt(bitstring)
write bitint
1060
边栏推荐
- Double pointer analog
- Pytorch读入据集(典型数据集及自定义数据集两种模式)
- KaFormer个人笔记整理
- GradScaler MaxClipGradScaler
- The printed object is [object object]. Solution
- Data midrange: analysis of full stack technical architecture of data midrange, with industry solutions
- 【LeetCode】387. First unique character in string
- Matlab camera calibrator camera calibration
- 【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
- 快慢指针系列
猜你喜欢

Kaformer personal notes

学习太极创客 — ESP8226 (十二)ESP8266 多任务处理

阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍

2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。

Squid代理服务器应用

China chip Unicorn Corporation

什么是图神经网络?图神经网络有什么用?
![[redis implements seckill business ①] seckill process overview | basic business implementation](/img/a3/9a50e83ece43904a3a622dcdb05b3c.png)
[redis implements seckill business ①] seckill process overview | basic business implementation
![[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle](/img/db/e581087e550a2e460f12047685c48f.png)
[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle

Transplantation of xuantie e906 -- fanwai 0: Construction of xuantie c906 simulation environment
随机推荐
tcpdump抓包实现过程
【E325: ATTENTION】vim编辑时报错
从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”
学习太极创客 — ESP8226 (十三)OTA
One article explains in detail | those things about growth
Become an IEEE student member
Determination of monocular and binocular 3D coordinates
What is SRE? A detailed explanation of SRE operation and maintenance system
RISC-V架构下 FPU Context 的动态保存和恢复
Matlab camera calibrator camera calibration
Scheme of alcohol concentration tester based on single chip microcomputer
Floating error waiting for changelog lock
linux(centos7.9)安装部署mysql-cluster 7.6
MySQL - SQL statement
阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
Unable to change the virtual machine power status and report an error solution
Change SSH port number
【牛客】HJ1 字符串最后一个单词的长度
【LeetCode】387. First unique character in string