Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the BIT related commands in the Redis string

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What this article shares with you is about the commands related to BIT in the Redis string, which the editor thinks is very practical, so I share it with you to learn. I hope you can get something after reading this article.

In the last article, we introduced some basic commands in the STRING data type, but did not cover BIT-related commands, so let's take a look at a few BIT-related commands.

BIT-related commands refer to several BITCOUNT/BITFIELD/BITOP/BITPOS/SETBIT/GETBIT commands. Flexible use of these commands can bring a lot of surprises to our project.

Prepare knowledge

Before learning these commands, we need to understand how strings are stored in redis. Strings in redis are stored in binary mode. For example, I execute the following command:

127.0.0.1 6379 > SET K1 aOK

A corresponding ASCII code is 97, converted to binary data is 01100001, our BIT-related commands are to operate on this binary data. Please read on.

GETBIT

The GETBIT command can return the bit value of value corresponding to key at offset. Taking K1 mentioned above as an example, the binary data corresponding to an is 01100001, so when offset is 0, the corresponding bit value is 0 offset 1, the corresponding bit value is 1 offset 2, the corresponding bit value is 1 offset 3, the corresponding bit value is 0, and so on. ., as follows:

127.0.0.1 GETBIT K1 0 (integer) 0127.0.1 0.1 GETBIT 6379 > GETBIT K1 (integer) 1127.0.0.1 GETBIT 6379 > GETBIT K1 2 (integer) 1127.0.0.1 GETBIT 6379 > GETBIT K1 4 (integer) 0127.0.1 0.1 GETBIT 6379 > GETBIT K1 5 (integer) 0127.0.1 1 GETBIT 6379 > GETBIT K1 6 (integer) 0127.0.1 Fraser 6379 > GETBIT K1 7 (integer) 1SETBIT

SETBIT can be used to modify binary data. For example, the ASCII code corresponding to an is 97, and the ASCII code corresponding to 99 is 01100001, and the binary is 01100011. The difference between the two is that the sixth bit is 0 and the other is 1. Through the SETBIT command, we can change the 0 of the sixth bit of K1 to 1 (the sixth bit is calculated from 0), as follows:

127.0.0.1 GET 6379 > GET k1 "c"

At this point, the characters stored in K1 become c. The number returned by SETBIT during execution, indicating the original bit value on that bit.

BITCOUNT

BITCOUNT can be used to count the number of 1s in this binary data, as follows:

127.0.0.1 6379 > BITCOUNT K1 (integer) 4

There is a very interesting case on the official website of BITCOUNT,redis: statistics on the number of users going online. The original excerpt is as follows:

For example, if today is the 100th day of the launch of the site, and user peter has read the site today, execute the command SETBIT peter 101; if peter continues to read the site tomorrow, execute the command SETBIT peter 1001, and so on. When you want to calculate the total number of times peter has been online, use the BITCOUNT command: execute BITCOUNT peter, and the result is the total number of days peter has been online.

The biggest advantage of this statistical method is that it saves space and is fast. It takes up one bit per day, which is only 365 bit a year, and 100.365 bit in 10 years, that is, 456 bytes. For such a large amount of data, bit is very fast.

BITOP

BITOP can perform AND, OR, XOR and NOT operations on one or more binary bit strings as follows: a corresponds to the conversion of ASCII code to binary is 01100001 and the corresponding binary bit string is 01100011. The result of executing AND\ OR\ XOR on these two binary bit strings is as follows:

127.0.0.1 set K1 aOK127.0.0.1:6379 > set K2 cOK127.0.0.1:6379 > BITOP and K3 K1 K2 (integer) 1127.0.0.1 set 6379 > BITOP or K3 K1 K2 (integer) 1127.0.1 set 6379 > get K3 "c" 127.0.1 1 cOK127.0.0.1:6379 6379 > BITOP xor K3 K1 K2 (integer) 1127.0.1 1 BITOP xor 6379 > get K3 "\ x02"

In addition, BITOP can also perform NOT operations, but note the number of parameters, as follows:

127.0.0.1 6379 > BITOP not K3 K4 (integer) 1

Here, the binary bit string of K4 will be inverted, and the result will be given to K3.

BITPOS

BITPOS is used to obtain the position of the first 1 or 0 in the binary bit string, as follows:

127.0.0.1 BITPOS 6379 > BITPOS K1 aOK127.0.0.1:6379 > BITPOS K1 (integer) 1127.0.1 BITPOS 6379 > BITPOS K1 0 (integer) 0

You can also set a range later, but the subsequent range is the range of bytes, not the range of binary bit strings.

That's all we'll cover about BIT-related commands in OK,STRING.

The above are the BIT-related commands in the Redis string, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report