The function of the Setbit function is to set or clear bits (bit) on the specified offset for the string value stored by the key.
In Redis, stored strings are stored in the form of a two-level system. For example, the string "AA" binary form 01000001 01000001, a total of 16 bits, each letter in 8-bit binary notation.
Setbit (name, offset, value): Offset is calculated from the highest bit, the highest bit is No. 0, such as A's ASCII code is 65, binary is 01000001, the No. 0 digit value is 0, and the 7th digit value is 1.
So if you want to turn "A" into "B", you're turning binary 01000001 into binary 01000010, which is to set the 6th bit to 1 and the 7th bit to 0.
The code is as follows:
Redis
>>> r = Redis. Redis ()
>>> r.set (' Xie ', ' A ')
True
>>> r.setbit (' Xie ', 6, 1)
0
> >> r.setbit (' Xie ', 7, 0)
1
>>> r.get (' Xie ')
B ' B '
>>>
The "AA" into "BB" is the binary 0 1000001 01000001 into 01000010 0100010, the practice is to set the 6th bit 1, 7th bit set to 0, the 14th bit set to 1, 15th bit set to 0.
The code is as follows:
>>> r.set (' Xie ', ' AA ')
True
>>> r.setbit (' Xie ', 6, 1)
0
>>> R.setbit (' Xie ', 7, 0)
1
>>> r.get (' Xie ')
B ' BA '
>>> r.setbit (' Xie ', 1)
0
>>> r.setbit (' Xie ', 0)
1
>>> r.get (' Xie ')
B ' BB '
>>>