当前位置:网站首页>Chapter V operation bit and bit string
Chapter V operation bit and bit string
2022-06-21 17:38:00 【yaoxin521123】
List of articles
The fifth chapter Operation bits and bit strings
Sometimes you might want to store a series of related Boolean values in a data platform based application . You can create many Boolean variables , You can also store them in an array or list . Or it can be called “ Bit string ” The concept of , It can be defined as a sequence of bits , First, render the least significant bit . Bit strings allow you to store such data in a very efficient way , Whether in terms of storage space or processing speed .
Bit strings can be stored in one of two ways , As a packed string or integer . If you hear a term without context “ Bit string ”, It means that the bit sequence is stored as a compressed string . This article introduces these two types of bit strings , Then it introduces some techniques that can be used to operate them .
Store the bit sequence as a bit string
The most common way to store a sequence of bits is in a bit string , This is a special kind of compressed string . In addition to saving storage space , You can also use ObjectScript System functions effectively manipulate bit strings .
Such a system function is $factor, It converts an integer to a bit string . We can convert integers into integers by executing the following statement 11744 Convert to bit string :
set bitstring = $factor(11744)
To view the representation of the bit string contents , have access to zwrite command :
zwrite bitstring
bitstring=$zwc(128,4)_$c(224,45,0,0)/*$bit(6..9,11,12,14)*/
At first it looked mysterious , But at the end of the output , You will see a comment , It shows a list of the actual bits that have been set :6、7、8、9、11、12 and 14. Bits in the bit string 1 Express 2^0, position 2 Express 2^1, And so on . Add all the bits together , We get 2^5 +2^6 + 2^7+ 2^8 + 2^10 + 2^11 + 2^13 = 11744.
To get a more pleasant visual representation , You can use another system function $bit:
for i=1:1:14 {
write $bit(bitstring, i)}
00000111101101
In this example ,$bit(bitstring, i) Returns the bits of a bit string i Value .
Be careful : To learn more about how this bit sequence is stored internally , Please take a close look at zwrite Output of command :
bitstring=$zwc(128,4)_$c(224,45,0,0)/*$bit(6..9,11,12,14)*/
The bit string is stored in four blocks , Each piece 8 position . Calculating each block separately will give you 224、45、0、0.
If it helps to treat a bit string as a string , You can think of each block as a 8 Bit character .
A common application of bit strings is the storage of bitmap indexes . Bitmap index is a special type of index , It uses a series of bit strings to represent a set of objects corresponding to a given value of a particular attribute . Each bit in the bitmap represents an object in the class .
for example , Suppose you create a database of animals and their characteristics , And define a class as follows :
Class User.Animal Extends %RegisteredObject
{
Property Name As %String [ Required ];
Property Classification As %String;
Property Diet As %String;
Property Swims As %Boolean;
Index ClassificationIDX On Classification [ Type = bitmap ];
Index DietIDX On Diet [ Type = bitmap ];
Index SwimsIDX On Swims [ Type = bitmap ];
}
After filling the database with some animals , It might look like :
| ID | Name | Classification | Diet | Swims |
|---|---|---|---|---|
| 1 | Penguin | Bird | Carnivore | 1 |
| 2 | Giraffe | Mammal | Herbivore | 0 |
| 3 | Cheetah | Mammal | Carnivore | 0 |
| 4 | Bullfrog | Amphibian | Carnivore | 1 |
| 5 | Shark | Fish | Carnivore | 1 |
| 6 | Fruit Bat | Mammal | Herbivore | 0 |
| 7 | Snapping Turtle | Reptile | Carnivore | 1 |
| 8 | Manatee | Mammal | Herbivore | 1 |
| 9 | Ant | Insect | Omnivore | 0 |
| 10 | Rattlesnake | Reptile | Carnivore | 0 |
Bitmap index DietIDX Track animals with specific dietary attribute values . Each row of the index can store up to one representative 64,000 A piece of an animal .
Diet Property
| Diet | Chunk | Bitmap |
|---|---|---|
| Carnivore | 1 | 1011101001 |
| Herbivore | 1 | 0100010100 |
| Omnivore | 1 | 0000000010 |
In the internal , Bitmap indexes are stored globally ^User.AnimalI In the following nodes of :
^User.AnimalI("DietIDX"," CARNIVORE",1)
^User.AnimalI("DietIDX"," HERBIVORE",1)
^User.AnimalI("DietIDX"," OMNIVORE",1)
The first subscript is the name of the index (DietIDX), The second subscript is the value of the indexed attribute ( for example ,CARNIVORE), The third subscript is the block number ( In this case 1).
Again , Bitmap index SwimsIDX Tracing has a specific Swims Attribute value of the animal .
Swims Property
| Swims | Chunk | Bitmap |
|---|---|---|
| True | 1 | 1001101100 |
| False | 1 | 0110010011 |
This bitmap index is stored in ^User.AnimalI In the following nodes of :
^User.AnimalI("SwimsIDX",1,1)
^User.AnimalI("SwimsIDX",0,1)
To understand the power of bit strings , You can calculate the CARNIVORE The number of carnivores is very easy to calculate the number of carnivores in the database , Without checking the actual data . Just use the system functions $bitcount:
set c = ^User.AnimalI("DietIDX"," CARNIVORE",1)
write $bitcount(c,1)
6
Again , You can calculate swim Number of animals :
set s = ^User.AnimalI("SwimsIDX",1,1)
write $bitcount(s,1)
5
To count the number of carnivores swimming , Please use $bitlogic Function to find the intersection of two sets :
set cs = $bitlogic(c&s)
write $bitcount(cs,1)
4
Be careful : Again using zwrite Check how bitmaps of predators are stored internally :
zwrite ^User.AnimalI("DietIDX"," CARNIVORE",1)
^User.AnimalI("DietIDX"," CARNIVORE",1)=$zwc(413,2,0,2,6,8,9)/*$bit(2,4..6,8,11)*/
ad locum , You can see that the diet attribute is CARNIVORE Of all animals . As you know , The bitmap index is divided into 64,000 Block of bits . For a given ID The bits of the animal store are stored in the block (ID\64000) + 1, Location (ID#64000) + 1 in . therefore , Indicates having ID 1 The bits of the animals are stored in the block 1, Location 2 in . therefore , In this bit string , position 2 For penguins , Not giraffes .
SQL The engine includes many special optimizations that can take advantage of bitmap indexing , So you can write SQL Get benefits when querying .
边栏推荐
- Summary of the 16th week
- RTMP webrtc protocol OpenSSL installation
- Uni app framework learning notes
- Accelerate the implementation of cloud native applications, and Yanrong yrcloudfile and Tianyi cloud have completed the Compatibility Certification
- 关于xlrd库的基础操作(新手向)
- 栈的生长方向和内存生长方向
- 软件测试体系学习及构建(14)-测试基础之软件测试和开发模型概述
- 3DE 运动轮廓数据修改
- What does container cloud mean? What is the difference with fortress machine?
- 高精度压位模板
猜你喜欢
随机推荐
常见设置模式
Preorder traversal of BM23 binary tree
Redis6.0新特性(上)
3DE 网格坐标点与物体的附加
variable
正则表达式
加速云原生应用落地,焱融 YRCloudFile 与天翼云完成兼容性认证
Are the two flame retardant standards of European furniture en 597-1 and en 597-2 the same?
The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
wcdma与LTE的区别
BM19 寻找峰值
The main relations and differences between Poisson sampling and Bernoulli sampling
[MySQL learning notes 14] DQL statement execution sequence
module. Exports points to problems
What does container cloud mean? What is the difference with fortress machine?
我的小工具-卡片学习APP 完成啦
BM22 比较版本号
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Not this year's 618?
Jetpack Compose 管理状态(一)








