[Tutor] Bitwise operation
Martin A. Brown
martin at linux-ip.net
Mon Feb 28 10:43:18 CET 2011
: i'm confused with & and AND. for eg:
: >>> 1110 & 0110
: 64
: >>> 1110 and 0110
: 72
:
: i'm expecting if 1110 and with 0110 will get 0110 or 6.
: pls advise.
Above, python thinks you are representing one number in decimal
notation and the other in octal.
Decimal (no leading zeroes):
>>> 14 & 6
6
Binary (leading with '0b'):
>>> 0b1110 & 0b0110
6
Octal (leading with '0'):
>>> 016 & 006
6
Hexadecimal (leading with '0x'):
>>> 0xe & 0x06
6
Additionally, you will want to distinguish which operator you
intend, bitwise or boolean. It seems fairly clear that you are
intending the bitwise operator if you are expecting '6' to be the
answer.
& bitwise 'and'
and boolean 'and'
Read these three sections, at least, to understand the
operators you are trying to use:
http://docs.python.org/reference/expressions.html#unary-arithmetic-and-bitwise-operations
http://docs.python.org/reference/expressions.html#boolean-operations
http://docs.python.org/reference/expressions.html#summary
This may also be useful:
http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex
Good luck,
-Martin
--
Martin A. Brown
http://linux-ip.net/
More information about the Tutor
mailing list