• preface

  • 1. bitmap

  • 1.1. What is a bitmap?

  • 2. Redis commands

  • 2.1. SETBIT

  • 2.2 GETBIT

  • 2.3 BITCOUNT

  • 2.4 BITOP

preface

1. bitmap

1.1. What is a bitmap

The bitmap in Redis is the same as the actual bitmap, which is a kind of bitwise operation structure. The structure is shown as follows:

Therefore, 0 or 1 is stored in bitmap, which is used to identify yes or no. Taking user login as an example, to count how many users log in wechat, the data in the corresponding bit should be set as 1 if the user logs in, and it is set according to the increment. To count how many users are logged in, you can calculate all the data in the bitmap to get all the logged users (if you swipe the offer finger, you can know that such calculation is counting the number of all 1s in a binary).

Redis also uses such a structure to achieve the corresponding requirements. Redis also provides SETBIT, GETBIT, BITCOUNT, and BITOP commands for processing bitmap arrays.

2. Redis commands

2.1. SETBIT

SETBIT is used to set the value of the binary bits at the specified offset of an array. The offset of a bit array starts at 0, and the binary value can be either 0 or 1

The command output is as follows:

I set the binary value of the array named bit offset 0 to 1, so bit can be interpreted as 0001;

Set the offset of the array bit to 3, then we can read bit as 1001;

2.2. GETBIT

GETBIT is used to get the value of the bits at the specified offset of the bit array: the command is shown in the figure above.

2.3. BITCOUNT

The BITCOUNT command is used to count the number of bits in a bit array with a literal value of 1:

As shown in the figure above, the value of 1 in the bit array is obtained by command. We know that the performance is very high when calculating the bit, so the number of 1 directly obtained by bit operation has a great improvement in performance. Here, the specific calculation can refer to the offer15 problem: leetcode-cn.com/problems/er…

2.4. BITOP

The BITOP command can perform bitwise and (and), bitwise or (OR), bitwise xor (XOR) on multiple groups of bits:

The result of setting the bit array in the figure above is: 1001

The data structure of the bit2 array is 1101

Bitop and bit2 to get 1001; After the OR operation, 1101

The bitop operation can be applied in the following scenarios:

For example, if you count the number of users who log in for three consecutive days, you can set an array of bits per day.

For example, 20201208 is a bit array, 20201209 is a bit array, and 20201210 is a bit array.

The filling data is as follows, so I want to count the users who have logged in for three consecutive days. Then I will operate and on the three bit arrays to get the final data, and then count the number of 1 to get the number of users who have logged in for three consecutive days. In the figure below, two users will be counted.

The above is about how to use bitmap records in Redis. It is easier to understand the implementation and principle of connecting bitmap to the actual scene.

About GETBIT, SETBIT, BITCOUNT, BITOP implementation principle continue to study in the next chapter!

References:

  1. Design and Implementation of Redis