当前位置:网站首页>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
边栏推荐
- [Niuke] convert string to integer
- 【Redis实现秒杀业务①】秒杀流程概述|基本业务实现
- [Niuke] length of the last word of HJ1 string
- Pytoch read data set (two modes: typical data set and user-defined data set)
- 学习太极创客 — ESP8226 (十三)OTA
- 嵌入式 | 硬件转软件的几条建议
- Yolox backbone -- implementation of cspparknet
- Data middle office: overview of data governance
- 数云发布2022美妆行业全域消费者数字化经营白皮书:全域增长破解营销难题
- MySQL - SQL statement
猜你喜欢
随机推荐
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development
陆奇:我现在最看好这四大技术趋势
【E325: ATTENTION】vim编辑时报错
PM2 deploy nuxt3 JS project
linux(centos7.9)安装部署mysql-cluster 7.6
"Unusual proxy initial value setting is not supported", causes and Solutions
MySQL | 视图《康师傅MySQL从入门到高级》笔记
Kaformer personal notes
十二、所有功能实现效果演示
[Niuke] length of the last word of HJ1 string
Threejs glow channel 01 (unrealbroompass & layers)
Ebanb B1 Bracelet brush firmware abnormal interrupt handling
Webrtc series - network transmission 5: select the optimal connection switching
The printed object is [object object]. Solution
[use picgo+ Tencent cloud object to store cos as a map bed]
MySQL | view notes on Master Kong MySQL from introduction to advanced
Data midrange: analysis of full stack technical architecture of data midrange, with industry solutions
[pytoch basic tutorial 31] youtubednn model analysis
MySQL | store notes of Master Kong MySQL from introduction to advanced









