Determining combination of bits

Josiah Carlson jcarlson at uci.edu
Mon Nov 8 18:40:05 EST 2004


Grant Edwards <grante at visi.com> wrote:
> 
> On 2004-11-08, Diez B. Roggisch <deetsNOSPAM at web.de> wrote:
> >> A lapse of mind?
> >>>>> n = 6
> >>>>> n & (-n)
> >> 2
> >> 
> >> You probably meant n&1 or perhaps n%2.
> >
> > No, the exact right thing: 6 is binary
> >
> > 110
> >
> > with the least significant bit beeing 
> >
> > 10
> 
> The least significant bit of 110 is this one here -----+
>                                ^                       |
>                                |                       |
>                                +-----------------------+
> 
> It's a 0 (zero).
> 
> What I think you're trying to say is something like the value
> of the rightmost 1.

From what I remember of high school chemistry (7 years ago), we used to
talk about 'significant figures' quite often.  If the teacher asked for
some number to 5 significant figures, it had better have them...

sig_fig(83737256,5) -> 83737000
sig_fig(1,5)        -> 1.0000

etc.

Now, in the case of 'least significant bit of n', that can be
interpreted as either n&1, or the rightmost bit that is significant
(nonzero).

The n&-n produces the rightmost bit that is nonzero, which is certainly
a valid interpretation of 'least significant bit of n'.

 - Josiah




More information about the Python-list mailing list