[Tutor] Bitwise operation

David Hutto smokefloat at gmail.com
Mon Feb 28 10:49:37 CET 2011


On Mon, Feb 28, 2011 at 4:43 AM, Martin A. Brown <martin at linux-ip.net> wrote:
>
>  : 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,
And skill/memory/reference.

>
> -Martin
>
> --
> Martin A. Brown
> http://linux-ip.net/
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
According to theoretical physics, the division of spatial intervals as
the universe evolves gives rise to the fact that in another timeline,
your interdimensional counterpart received helpful advice from me...so
be eternally pleased for them.


More information about the Tutor mailing list