[Tutor] Strange operator

Bill Mill bill.mill at gmail.com
Fri Nov 5 03:53:54 CET 2004


Chad,

On Thu, 4 Nov 2004 18:17:58 -0800 (PST), Chad Crabtree
<flaxeater at yahoo.com> wrote:
> So if I understand this correctly it's similar to the chmod 777
> directive, using binary representation to mean (111)  I understood
> this
> system already but that is really neat.  So to extrapolate
> 
> LEFT=1
> RIGHT=2
> TOP=4
> BOTTOM=8
> WILLYWONKA=16
> 
> would be the proper progression of options.  then to see if a option
> was
> set I would do it like this.
> 
> if flag>=WILLYWONKA:
>     print "Go to the Factory"

To see if it was set, you would do:

if flag & WILLYWONKA: wonkify()

Imagine if flag==32; that is "100000" in binary. In this case, flag >=
WILLYWONKA is true, but flag & WILLYWONKA would be false, because the
5th bit is not set in flag.

So, to set the WILLYWONKA value in flag, do "flag |= WILLYWONKA". To
toggle it, do "flag ^ WILLYWONKA". To test if it's there, do "flag &
WILLYWONKA".

> So this is like a really really really compact switch statement?
> 

Don't really understand what you mean here.

Peace
Bill Mill
bill.mill at gmail.com

> 
> Danny Yoo wrote:
> 
> > Hi Chad,
> >
> >
> >Underneath the surface, integers in Python are stored as a series of
> bits.
> >For example, the following sequence of bits:
> >
> >    100010100100
> >
> >can stand for the integer 2212:
> >
> >###
> >
> >
> >>>>int('100010100100', 2)
> >>>>
> >>>>
> >2212
> >###
> >
> >
> >So that's a traditional interpretation of a series of bits.  But
> there's
> >another interpretation we can use: a series of bits may record a
> bunch of
> >yes/no answers.  In that sense,
> >
> >    100010100100
> >
> >might mean:
> >
> >    [Yes, No, No, No, Yes, No, Yes, No, No, Yes, No, No]
> >
> >
> >
> >A bitwise OR allows us to turn any one of those bits from No to Yes.
>  For
> >example, perhaps we may like to turn on the very last bit there.  We
> can
> >do this by saying:
> >
> >###
> >
> >
> >>>>2212 | 1
> >>>>
> >>>>
> >2213
> >###
> >
> >
> >We should be careful not to interpret the result as an addition:
> it's not.
> >We're actually turning on the very last bit, which we can see if we
> try
> >doing another OR:
> >
> >###
> >
> >
> >>>>2213 | 1
> >>>>
> >>>>
> >2213
> >###
> >
> >That 'bit' is on already, so doing an OR again doesn't affect the
> answer.
> >There are other bitwise operators out there that act like toggle
> switches;
> >
> >
> >A bitwise representation of these yes/no choices is nice because
> it's VERY
> >compact.  A single integer can normally hold about 32 individual
> bits.
> >(32 bit computing!)  And to be able to represent 32 separate
> 'yes/no'
> >flags in a single integer is a very neat thing.
> >
> >
> >We may have run into this sort of thing before: the re.compile()
> function
> >can take in an optional set of flags.  For example, we might say
> something
> >like:
> >
> >    re.compile("hello world", re.VERBOSE | re.IGNORECASE)
> >
> >Without knowing about bits, that statement should actually look
> weird: how
> >are we giving two particular bits of information in a single
> argument?
> >
> >
> >We can look at what that OR is producing:
> >
> >###
> >
> >
> >>>>re.VERBOSE
> >>>>
> >>>>
> >64
> >
> >
> >>>>re.IGNORECASE
> >>>>
> >>>>
> >2
> >
> >
> >>>>re.VERBOSE | re.IGNORECASE
> >>>>
> >>>>
> >66
> >###
> >
> >
> >It's producing a single integer, but that integer is made up of
> bits! So
> >we're really saying that two particular bits should be set to "Yes".
> >
> >###
> >
> >
> >>>>int('1000010', 2)
> >>>>
> >>>>
> >66
> >###
> >
> >
> >
> >Does this make sense so far?  A lot of this bit-fiddling just has to
> deal
> >with using a very efficient representation for a collection of
> choices.
> >
> >
> >Hope this helps!
> >
> >
> >
> >
> >
> 
> __________________________________
> Do you Yahoo!?
> Check out the new Yahoo! Front Page.
> www.yahoo.com
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list