当前位置:网站首页>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
边栏推荐
- How does the tunnel mobile inspection track robot monitor 24 hours to ensure the safety of tunnel construction?
- linux(centos7.9)安装部署mysql-cluster 7.6
- Pytoch read data set (two modes: typical data set and user-defined data set)
- A tip to read on Medium for free
- 解决:jmeter5.5在win11下界面上的字特别小
- 1528. rearrange strings
- [noi Simulation Competition] geiguo and time chicken (structure)
- [pytoch basic tutorial 31] youtubednn model analysis
- The printed object is [object object]. Solution
- Data middle office: middle office practice and summary
猜你喜欢

华为路由器:ipsec技术
![[pytorch basic tutorial 30] code analysis of DSSM twin tower model](/img/4a/1f290990b39c517efb2b9cef0b5fcb.png)
[pytorch basic tutorial 30] code analysis of DSSM twin tower model

leetcode——错误的集合
![[noi Simulation Competition] send (tree DP)](/img/5b/3beb9f5fdad00b6d5dc789e88c6e98.png)
[noi Simulation Competition] send (tree DP)

Solution: the word of jmeter5.5 on the win11 lower interface is very small

The native applet uses canvas to make posters, which are scaled to the same scale. It is similar to the uniapp, but the writing method is a little different

Telnet port login method with user name for liunx server

当程序员被问会不会修电脑时… | 每日趣闻

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

【E325: ATTENTION】vim编辑时报错
随机推荐
Jincang KFS replicator installation (oracle-kes)
数云发布2022美妆行业全域消费者数字化经营白皮书:全域增长破解营销难题
Alibaba Senior Software Testing Engineer recommends testers to learn -- Introduction to security testing
4274. 后缀表达式
The border problem after the focus of input
China chip Unicorn Corporation
[noi Simulation Competition] send (tree DP)
Idea another line shortcut
学习太极创客 — ESP8226 (十三)OTA
Matlab camera calibrator camera calibration
Ebanb B1 Bracelet brush firmware abnormal interrupt handling
Solution: Nan occurs in loss during model training
什么是图神经网络?图神经网络有什么用?
【Pytorch基础教程31】YoutubeDNN模型解析
Qingcloud based "real estate integration" cloud solution
Digital cloud released the 2022 white paper on digital operation of global consumers in the beauty industry: global growth solves marketing problems
EasyExcel单sheet页与多sheet页写出
What is SRE? A detailed explanation of SRE operation and maintenance system
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)
Remote connection of raspberry pie without display by VNC viewer