Little explanation

Ben Caradoc-Davies ben at wintersun.org
Tue Mar 2 12:39:25 CET 2004


On Tue, 2 Mar 2004 11:52:49 +0100, Angelo Secchi <secchi at sssup.it> wrote:
> What is difficult to understand for me is the meaning of the bitwise
> operator &. In particular what do expressions like
> i & 0x100000000L
> (i >> 24) & 0x7f
> i & 0xffffff
> mean?

This syntax is taken from directly from the C programming language. Consult
"ANSI C", second edition, by Kernighan and Ritchie, or see the Python Language
Reference:

    http://www.python.org/doc/current/ref/bitwise.html
    http://www.python.org/doc/current/ref/shifting.html

"&" is "bitwise and". ">>" is "bitshift right". Bitwise boolean operators
operate on each bit of the operands, rather than their values as a whole. "&"
allows you to select only those bits which are set in both operands. ">>"
allows you to move your bits around so that you can interpret your bits as a
small integer (selecting a few bits from within a word or longer).

Write the *binary* representation of these numbers and try it yourself. Much
easier than trying to understand it in hexadecimal.

-- 
Ben Caradoc-Davies <ben at wintersun.org>
http://wintersun.org/
Imprisonment on arrival is the authentic Australian immigration experience.



More information about the Python-list mailing list