Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98619 > unrolled thread

Re: bitwise operator, bits dont go into bitbucket..?

Started bykent nyberg <kent@z-sverige.nu>
First post2015-11-10 17:56 -0500
Last post2015-11-10 17:56 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: bitwise operator, bits dont go into bitbucket..? kent nyberg <kent@z-sverige.nu> - 2015-11-10 17:56 -0500

#98619 — Re: bitwise operator, bits dont go into bitbucket..?

Fromkent nyberg <kent@z-sverige.nu>
Date2015-11-10 17:56 -0500
SubjectRe: bitwise operator, bits dont go into bitbucket..?
Message-ID<mailman.225.1447196189.16136.python-list@python.org>
On Wed, Nov 11, 2015 at 09:33:38AM +1100, Chris Angelico wrote:
> On Wed, Nov 11, 2015 at 9:27 AM, kent nyberg <kent@z-sverige.nu> wrote:

> 
> If you want to check specific bits (in C or Python, either way), it's
> much more common to use bitwise AND than bit shifts:
> 
> >>> 0b100011011101010110 & 0b000000010000
> 16
> >>> print(bin(_))
> 0b10000
> 

So, to check if 0b010[this one bit]010 is set,   i do   & 0b0001000

That is,  I set just that one to 1 in the other and then the & operator will make it return
0 if its not set. Since every other is zero, the return will be zero if its not. Since & operator sets 0
if not both are 1. Right?  

Id so, Thanks.   

My misunderstanding was that
0b01000   (for example,) first could be shifted left. To become 0b10000.
And then shifted right to become 0b00001. 
The shifting would turn every other digit to 0 and leave only the wanted one untouched. 
That way, I could check if its 0 or 1.  If you understand my own logic of how it works.
But I got it wrong,  and I think I know how to do it with & operator.

> This will be either the same number as the right hand side (if the bit
> had been set) or zero (if it hadn't).
> 
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web