How does Redis's setbit this bit understand, with bitcount use?
This is a simple explanation of how setbit is used.
<img src= "https://pic4.zhimg.com/8650852c348cf08ff711e44ca13aaf47_b.png" data-rawwidth= "958" data-rawheight= "class=" Origin_image zh-lightbox-thumb "width=" 958 "data-original=" https://pic4.zhimg.com/ 8650852c348cf08ff711e44ca13aaf47_r.png ">
In Redis, the stored strings exist in a two-tier system.
Example:
Set a key-value, the name of the key is "Andy" value for character ' a '
<img src= "https://pic3.zhimg.com/85f9a07a4cfb815029f4ce266daa78a2_b.png" data-rawwidth= "524" data-rawheight= "class=" Origin_image zh-lightbox-thumb "width=" 524 "data-original=" https://pic3.zhimg.com/ 85f9a07a4cfb815029f4ce266daa78a2_r.png ">
We know that the ASCII code of ' a ' is 97. The conversion to binary is: 01100001. The scientific name of offset is called "offset". Each of the binary is offset value, for example, here offset 0 equals ' 0 ', offset 1 equals ' 1 ', Offset2 equals ' 1 ', offset 6 equals ' 1 ', yes, offset is counted from left to right, that is, from high to low.
How do we change Andy's ' a ' to ' B ' through the setbit command?
That is, the 01100001 becomes 01100010 (the ASCII code of B is 98), this is very simple, that is, the ' a ' in the offset 6 from 0 to 1, the offset 7 from 1 to 0.
<img src= "https://pic1.zhimg.com/4a65befc057124bb36c880758b26b7fc_b.png" data-rawwidth= "521" data-rawheight= "class=" Origin_image zh-lightbox-thumb "width=" 521 "data-original=" https://pic1.zhimg.com/ 4a65befc057124bb36c880758b26b7fc_r.png ">
You may also find that after each setbit is finished, there is a return value of (integer) 0 or (integer) 1, which is the bit value of the offset bit before you setbit it.
This time, we get Andy again and see the results:
<img src= "https://pic2.zhimg.com/9591d4a73f3471847bbc6ade20d7f94d_b.png" data-rawwidth= "340" data-rawheight= "class=" Content_image "width=" 340 ">
Sure enough, it changed from ' a ' to ' B '.
This is the basic use of "setbit" in Redis.
Bitcount is the two-level code that counts strings, and how many ' 1 ' are there. So here,
The result of Bitcount Andy is 3.
Above. Transfer from https://www.zhihu.com/question/27672245/answer/123641959
Redis's Setbit command