当前位置:网站首页>Chapter V operation bit and bit string

Chapter V operation bit and bit string

2022-06-21 17:38:00 yaoxin521123

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 :67891112 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 2244500.

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 :

IDNameClassificationDietSwims
1PenguinBirdCarnivore1
2GiraffeMammalHerbivore0
3CheetahMammalCarnivore0
4BullfrogAmphibianCarnivore1
5SharkFishCarnivore1
6Fruit BatMammalHerbivore0
7Snapping TurtleReptileCarnivore1
8ManateeMammalHerbivore1
9AntInsectOmnivore0
10RattlesnakeReptileCarnivore0

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

DietChunkBitmap
Carnivore11011101001
Herbivore10100010100
Omnivore10000000010

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

SwimsChunkBitmap
True11001101100
False10110010011

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 .

原网站

版权声明
本文为[yaoxin521123]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211524256500.html