[Tutor] Bitwise &

Joel Goldstick joel.goldstick at gmail.com
Wed Oct 14 19:29:47 EDT 2015


On Wed, Oct 14, 2015 at 4:47 PM, ਨਿਹੰਗ ਪੰਜਾਬੀ <niihung at gmail.com> wrote:

>  'if (n & 1)'  below works but I don't understand why/how.  Kindly help.
>
> ==============
> >>> def fn(n):
> ...     if (n & 1):
> ...         print "n is odd"
> ...     else:
> ...         print "n is even"
> ...
> >>> fn(5)
> n is odd
> >>> fn(4)
> n is even
>
> ===============
>

& is a bitwise operator, so any odd number and 1 will be one (true), and
any even number will be zero (false)
Any

>
> Thanks
> Ni
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays


More information about the Tutor mailing list