Bitmaps —

Bitmap is not the actual data type , But in String A set of bit oriented operations defined on a type . Because strings are binary safe blob, And their maximum length is 512 MB, So they are suitable for setting 2
^ 32 Different bits .

Bit operations are divided into two groups : Constant time unit operation , If the bit is set to 1 or 0, Or get its value , And working with bitgroups , For example, calculate the number of set digits within a given positioning range ( for example , Demography ).

One of the biggest advantages of bitmaps is that they usually save a lot of space when storing information . for example , Through incremental users ID In systems representing different users , You can use only 512MB Memory of 40 Single bit information of 100 million users ( for example , Know if users want to receive newsletters ).

use SETBIT and GETBIT Command settings and retrieval bits :

> setbit key 10 1
( integer )1
> getbit key 10
( integer )1
> getbit key 11
( integer )0
SETBIT Command takes the number of digits as its first parameter , Set the second parameter as the value of this bit , Namely 1 or 0. If the addressing bit exceeds the current string length , The command will automatically enlarge the string .

GETBIT Returns only the bit value at the specified index . Out of range bits ( Addressing bits beyond the length of the string stored in the target key ) Always considered zero .

There are three sets of commands running on a set of bits :

BITOP Bitwise between different strings . The operations provided are AND,OR,XOR and NOT.
BITCOUNT Perform population count , Report set to 1 Number of digits .
BITPOS Find has specified value 0 or 1 The first bit of .
BITPOS and BITCOUNT Can use the byte range of string for operation , Instead of running over the entire length of the string . Here is BITCOUNT A simple example of a call :

> setbit key 0 1
( integer )0
> setbit key 100 1
( integer )0
> bitcount key
( integer )2
The common user cases of bitmaps are :

Various real-time analysis .
Storage and objects ID Associated space efficient but high performance Boolean information .

for example , Imagine that you want to know the longest continuity of daily visits by website users . You start counting days from zero , That is, the date you published the website , And use the SETBIT Set up some . As bit index , You just need to get the current unix time , Subtract the initial offset , Then divide by 3600
* 24.

This is for each user , You have a small string containing daily access information . use BITCOUNT Easily get the number of days a given user has visited the site , Just a few times at a time BITPOS call , Or just get and analyze the client bitmap , You can easily calculate the longest stripe .

Bitmaps are easy to split into multiple keys , For example, to segment data sets , Because it's usually best to avoid using large keys . To split bitmaps on different keys instead of setting all bits as keys , A simple strategy is to store each key M Bit and get with tag /
M Key name and N Bits are used for bit addressing within the key - number MOD M.

©2019-2020 Toolsou All rights reserved,
It's unexpected Python Cherry tree (turtle The gorgeous style of Library )Unity3D of UGUI Basics -- Three modes of canvas os Simple use of module computer network --- Basic concepts of computer network ( agreement , system )html Writing about cherry trees , Writing about cherry trees Some East 14 Pay change 16 salary , Sincerity or routine ?Unity-Demo Examples ✨ realization UI- Backpack equipment drag function 06【 Interpretation according to the frame 】 Data range filtering -- awesome java Four functional interfaces ( a key , simple )