[Tutor] Strange operator

Marilyn Davis marilyn at deliberate.com
Fri Nov 5 00:11:39 CET 2004


On Thu, 4 Nov 2004, Chad Crabtree wrote:

> I was perusing the wax sourcecode and ran accross this
>         style |= styles.frame(kwargs)
>         style |= styles.window(kwargs)
> 
> the pipe-equal operator is something I've never seen this before,
> after 
> a little searching I found this
> 
> Bitwise OR              *|*        1 if lhs bit OR rhs bit = 1

It is totally cool.

If you have a bit pattern, x =  10101100
And a mask,                m =  00000010
Then x |= m turns *on*          10101110
any bit in x that has
a 1 in m -- leaving the rest of x in tact.

Similarly, 
If you have a bit pattern, x =  10101100
And a mask,                m =  11111011
Then x &= m turns *off*         10101011            
any bit in x that has
a 0 in m -- leaving the rest of x in tact.

Coolest is ^, the exclusive or.  1 ^ 0 = 1
                                 1 ^ 1 = 0
                                 0 ^ 0 = 0

If you have a bit pattern, x =  10101100
And a mask,                m =  00000110
Then x ^= m turns *toggles*     10101011            
any bit in x that has
a 0 in m -- leaving the rest of x in tact.

This is the basis of encryption!  If x is your message and you give
your friend a copy of your mask, you can ^= your message with your mask.
Send the messed-up message to your friend and she can ^= the messed-up
message with the same mask and get back to the original message.

(I hope I didn't make any typos to confuse you!)

Marilyn Davis


> 
> http://www.mhuffman.com/notes/language/py_intro.htm#LOGIC
> 
> which confuses me even more because I'm not sure how this is useful. 
> Could someone please explain this?
> 
> 
> 
> 
> 		
> __________________________________ 
> 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