Discussion:
[Tutor] Bitwise &
ਨਿਹੰਗ ਪੰਜਾਬੀ
2015-10-14 20:47:02 UTC
Permalink
'if (n & 1)' below works but I don't understand why/how. Kindly help.

==============
... if (n & 1):
... print "n is odd"
... else:
... print "n is even"
...
fn(5)
n is odd
fn(4)
n is even

===============

Thanks
Ni
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Ben Finney
2015-10-14 23:29:20 UTC
Permalink
Post by ਨਿਹੰਗ ਪੰਜਾਬੀ
'if (n & 1)' below works but I don't understand why/how. Kindly help.
Can you kindly help us understand your confusion? You chose a subject
field that indicates why it works, so I don't know what your specific
confusion is.
--
\ “Telling pious lies to trusting children is a form of abuse, |
`\ plain and simple.” —Daniel Dennett, 2010-01-12 |
_o__) |
Ben Finney

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.p
Joel Goldstick
2015-10-14 23:29:47 UTC
Permalink
Post by ਨਿਹੰਗ ਪੰਜਾਬੀ
'if (n & 1)' below works but I don't understand why/how. Kindly help.
==============
... print "n is odd"
... 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
Post by ਨਿਹੰਗ ਪੰਜਾਬੀ
Thanks
Ni
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
--
Joel Goldstick
http://joelgoldstick.com/stats/birthdays
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.py
Robert Nanney
2015-10-14 23:37:35 UTC
Permalink
To elaborate a little more this is comparing the 'one' bit. Any odd number
will have the 'one' bit set.
Post by Joel Goldstick
Post by ਨਿਹੰਗ ਪੰਜਾਬੀ
'if (n & 1)' below works but I don't understand why/how. Kindly help.
==============
... print "n is odd"
... 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
Post by ਨਿਹੰਗ ਪੰਜਾਬੀ
Thanks
Ni
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
--
Joel Goldstick
http://joelgoldstick.com/stats/birthdays
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/lis
Laura Creighton
2015-10-14 23:39:29 UTC
Permalink
Post by Joel Goldstick
& is a bitwise operator, so any odd number and 1 will be one (true), and
any even number will be zero (false)
You and Ben seem to have missed the problem in the answer.
I think that Ni needs to understand _how bitwise operators work_

And it is 1:38 here in the morning, I must get to bed. Somebody
else explain it! (and if they don't, Ni, I will get to it tomorrow.)

Laura

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Joel Goldstick
2015-10-15 00:08:17 UTC
Permalink
Post by Laura Creighton
Post by Joel Goldstick
& is a bitwise operator, so any odd number and 1 will be one (true), and
any even number will be zero (false)
You and Ben seem to have missed the problem in the answer.
I think that Ni needs to understand _how bitwise operators work_
And it is 1:38 here in the morning, I must get to bed. Somebody
else explain it! (and if they don't, Ni, I will get to it tomorrow.)
Laura
Ok
the integers can be represented in binary like: 5 = 0101, 0 = 0000, 2 ==
0010, etc.

When you perform the & operation, it does a bitwise and operation. 0101 &
0001 = 1, 0010 & 0001 = 0.

Hope that helps
--
Joel Goldstick
http://joelgoldstick.com/stats/birthdays
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-10-14 23:35:36 UTC
Permalink
Post by ਨਿਹੰਗ ਪੰਜਾਬੀ
'if (n & 1)' below works but I don't understand why/how. Kindly help.
Do you understand what bitwise & does?
It takers the logical AND of each bit in the two operands.

So, keeping it simple with 2 digit numbers we get

0 = 00
1 = 01
2 = 10
3 = 11

Notice that the second bit in each odd number is a 1.

So anding two odd numbers results in the last two
bits both being 1 and (1 AND 1) is always 1.

So long as any bit is 1 the overall result of (X & Y)
will be True, only all zero will be False.

So you are guaranteed that two odd numbers ANDed
together will be True
Post by ਨਿਹੰਗ ਪੰਜਾਬੀ
==============
... print "n is odd"
... print "n is even"
So this will indeed work.

However its not necessarily good style since it is
not obvious how it works unless you understand bitwise
operations so arguably,

def f(n):
if n%2 == 0 :
print 'n is even
else...

Would be clearer.

Although that's just a tad subjective.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinf
Steven D'Aprano
2015-10-15 00:26:31 UTC
Permalink
Post by ਨਿਹੰਗ ਪੰਜਾਬੀ
'if (n & 1)' below works but I don't understand why/how. Kindly help.
==============
... print "n is odd"
... print "n is even"
& is the "bitwise AND" operator. It takes each pair of bits (one from
each of the two arguments) and combines them like this:

0 & 0 --> 0
0 & 1 --> 0
1 & 0 --> 0
1 & 1 --> 1

So if we take two numbers, say 25 and 43, and look at them in base 2
(binary):

25 --> 011001 in binary
43 --> 101011 in binary

the & operator constructs a new binary number from each pair of bits.
Starting from the left:

0 & 1 --> 0
1 & 0 --> 0
1 & 1 --> 1
0 & 0 --> 0
0 & 1 --> 0
1 & 1 --> 1

so the result of 25 & 43 is binary 001001 which equals 9 in
decimal:

py> 25 & 43
9


Your function fn tests for odd numbers by using the bitwise AND of the
number with 1. The binary (base 2) version of decimal 1 is 00000001
(fill in as many leading zeroes as you need), so the result of n&1 will
be 1 if the binary version of n ends with a 1 bit, otherwise 0.

So fn could be written like this (except it will probably be slower):

def fn(n):
if bin(n).endswith("1"):
print "n is odd"
else:
print "n is even"


Why does ending with a 1 bit mean that the number is odd? Here is a
reminder about *decimal* numbers:

7453 in decimal means:

7 thousands (7 * 10**3)
4 hundreds (4 * 10**2)
5 tens (5 * 10**1)
3 units (3 * 10**0)

The binary number 111001 means:

1 * 2**5 = 32
1 * 2**4 = 16
1 * 2**3 = 8
0 * 2**2 = 0
0 * 2**1 = 0
1 * 2**0 = 1

Adding them together gives (32+16+8+1) = 57. Let's check if this is
right:

py> int("111001", 2)
57

But the important thing to notice is that, in binary, every bit
represents an *even* number, a power of 2: 2, 4, 8, 16, 32, 64, ...
EXCEPT the first bit (the one on the far right) which represents the
number of units.

So if the far-right bit is 0, the number is an even number, and if the
far-right bit is 1, the number is odd.

By the way, there is also a "bitwise OR" operator that uses this
truth-table:

0 | 0 --> 0
0 | 1 --> 1
1 | 0 --> 1
1 | 1 --> 1

and a "bitwise XOR" (exclusive-or) operator:

0 ^ 0 --> 0
0 ^ 1 --> 1
1 ^ 0 --> 1
1 ^ 1 --> 0
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https:
Loading...