how to get bit info
Stephen Hansen
me+list/python at ixokai.io
Thu Jun 17 17:39:19 EDT 2010
On 6/17/10 1:29 PM, Grant Edwards wrote:
> On 2010-06-17, Stephen Hansen <me+list/python at ixokai.io> wrote:
>
>>>>> BIT_1 = 1 << 0
>>>>> BIT_2 = 1 << 1
>
> ...
>
>> Basically, those BIT_X lines are creating numbers which have *only* the
>> specified bit set. Then you do "byte & BIT_X", and that will return 0 if
>> the byte doesn't have the specified bit in it. You can then set the bit
>> with "byte | BIT_X", and unset the bit with "byte ^ BIT_X".
>
> Just to clarify, "byte ^ BIT_X" inverts (toggles) bit X.
>
> If you want to make sure bit X is a 0 (which is what people usually
> mean by "unset"), you do "byte & ~BIT_X"
Doh, you're correct. I got so used to the pattern of only ever flipping
the bit off after for some reason I knew it was on, like:
if blah & CONSTANT_A:
do-stuff
blah = blah ^ CONSTANT_A
That I forgot ^ was invert >_>
Ahem! Thanks for the correction.
--
Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100617/34123da2/attachment-0001.sig>
More information about the Python-list
mailing list