A brief introduction to Redis in Python:setbit function __ function

Source: Internet
Author: User
Tags bit set redis in python

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 '
>>>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.