On 6/17/10 1:29 PM, Grant Edwards wrote: > On 2010-06-17, Stephen Hansen <me+list/pyt...@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/
signature.asc
Description: OpenPGP digital signature
-- http://mail.python.org/mailman/listinfo/python-list