Bitwise manipulation

  • Follow


how can u find whether a no has consecutive zero bits either in from
LSB or MSB.
if they exist in between, then what to do? plz clarify.

0
Reply nehilparashar (24) 7/14/2007 8:27:30 PM

"Nehil" <nehilparashar@gmail.com> wrote in message 
news:1184444850.478621.170190@22g2000hsm.googlegroups.com...
> how can u find whether a no has consecutive zero bits either in from
> LSB or MSB.
> if they exist in between, then what to do? plz clarify.
>
Take the binary complement of the number (~). Take two copies, and shift one 
a bit (<<). Then AND (&, just one). If the answer is non-zero, you have 
consecutive zero bits.

-- 
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

0
Reply regniztar (3128) 7/14/2007 9:02:28 PM


On Jul 15, 2:02 am, "Malcolm McLean" <regniz...@btinternet.com> wrote:
> "Nehil" <nehilparas...@gmail.com> wrote in message
>
> news:1184444850.478621.170190@22g2000hsm.googlegroups.com...> how can u find whether a no has consecutive zero bits either in from
> > LSB or MSB.
> > if they exist in between, then what to do? plz clarify.
>
> Take the binary complement of the number (~). Take two copies, and shift one
> a bit (<<). Then AND (&, just one). If the answer is non-zero, you have
> consecutive zero bits.
>
> --
> Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm

Thanks for your answer. but the solution you suggested only tells true
or false, whether consecutive zero bits are there or not.

i need something different :
i want to know how many consecutive zero bits are there?
for exapmle :
let say arr[0] = 10; so bit representation will be (32-bit)

00000000 00000000 00000000 00001010

so answer should be => 29 from MSB and 1 from LSB.
please clarify.
Thanks.

0
Reply nehilparashar (24) 7/14/2007 9:13:00 PM

Nehil wrote:
> how can u find whether a no has consecutive zero bits either in from
> LSB or MSB.
> if they exist in between, then what to do? plz clarify.

     Are you the same "Nehil" who is undertaking to write
a garbage collector for C?  If so, have you ever heard the
advice "Walk before you run?"

     Yes, we all know that "A man's reach should exceed his
grasp," but perhaps by not quite so wide a span ...

-- 
Eric Sosman
esosman@ieee-dot-org.invalid
0
Reply esosman2 (2945) 7/14/2007 9:30:17 PM

On Jul 15, 2:30 am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> Nehil wrote:
> > how can u find whether a no has consecutive zero bits either in from
> > LSB or MSB.
> > if they exist in between, then what to do? plz clarify.
>
>      Are you the same "Nehil" who is undertaking to write
> a garbage collector for C?  If so, have you ever heard the
> advice "Walk before you run?"
>
>      Yes, we all know that "A man's reach should exceed his
> grasp," but perhaps by not quite so wide a span ...
>
> --
> Eric Sosman
> esos...@ieee-dot-org.invalid

===================================================================
Yes, i'm the same Nehil. :) but i could not understand your wordings.
i
came to this doubt in that project only. i designed a bitmap for each
page i allocate, and now want to know how many pages have been
allocated by knowing how many bits are there zero??

is this approach good?

0
Reply nehilparashar (24) 7/14/2007 9:47:25 PM

Nehil wrote:
> 
> On Jul 15, 2:02 am, "Malcolm McLean" <regniz...@btinternet.com> wrote:
> > "Nehil" <nehilparas...@gmail.com> wrote in message
> >
> > news:1184444850.478621.170190@22g2000hsm.googlegroups.com...> how can u find whether a no has consecutive zero bits either in from
> > > LSB or MSB.
> > > if they exist in between, then what to do? plz clarify.
> >
> > Take the binary complement of the number (~). Take two copies, and shift one
> > a bit (<<). Then AND (&, just one). If the answer is non-zero, you have
> > consecutive zero bits.
> >
> > --
> > Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm
> 
> Thanks for your answer. but the solution you suggested only tells true
> or false, whether consecutive zero bits are there or not.
> 
> i need something different :
> i want to know how many consecutive zero bits are there?
> for exapmle :
> let say arr[0] = 10; so bit representation will be (32-bit)
> 
> 00000000 00000000 00000000 00001010
> 
> so answer should be => 29 from MSB and 1 from LSB.
> please clarify.

/* BEGIN new.c */

#include <stdio.h>

#define N               10U
#define str(s)          # s
#define xstr(s)         str(s)

void consecutive_zero_bits(unsigned n, unsigned *most, unsigned *least);

int main(void)
{
    unsigned most, least;
    
    consecutive_zero_bits(N, &most, &least);
    puts(xstr(N));
    printf("%u from MSB and %u from LSB.\n", most, least);
    return 0;
}

void consecutive_zero_bits(unsigned n, unsigned *most, unsigned *least) 
{
    unsigned mask, count;

    count = 0;
    for (mask = 1; mask != 0; mask *= 2) {
        if( (mask & n) == 0) {
            ++count;
        } else {
            *least = count;
            break;
        }
    }
    count = 0;
    for (mask = (0u - 1) / 2 + 1; mask != 0; mask /= 2) {
        if( (mask & n) == 0) {
            ++count;
        } else {
            *most = count;
            break;
        }
    }
}

/* END new.c */

-- 
pete
0
Reply pfiland (6613) 7/14/2007 9:55:34 PM

"Nehil" <nehilparashar@gmail.com> wrote in message 
news:1184449645.784279.206060@o61g2000hsh.googlegroups.com...
> On Jul 15, 2:30 am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
>> Nehil wrote:
>> > how can u find whether a no has consecutive zero bits either in from
>> > LSB or MSB.
>> > if they exist in between, then what to do? plz clarify.
>>
>>      Are you the same "Nehil" who is undertaking to write
>> a garbage collector for C?  If so, have you ever heard the
>> advice "Walk before you run?"
>>
>>      Yes, we all know that "A man's reach should exceed his
>> grasp," but perhaps by not quite so wide a span ...
>>
>> --
>> Eric Sosman
>> esos...@ieee-dot-org.invalid
>
> ===================================================================
> Yes, i'm the same Nehil. :) but i could not understand your wordings.
> i
> came to this doubt in that project only. i designed a bitmap for each
> page i allocate, and now want to know how many pages have been
> allocated by knowing how many bits are there zero??
>
> is this approach good?
>
A garbage collector, let alone an efficient garbage collector, is a very 
difficult routine to write. If you can't even do basic bit manipulation - no 
shame on you, we were all beginners at some time - then there is no way you 
are going to achieve a garbage collector, except by some miracle.


-- 
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
 

0
Reply regniztar (3128) 7/15/2007 8:33:52 AM

On Jul 15, 1:33 pm, "Malcolm McLean" <regniz...@btinternet.com> wrote:
> "Nehil" <nehilparas...@gmail.com> wrote in message
>
> news:1184449645.784279.206060@o61g2000hsh.googlegroups.com...
>
> > On Jul 15, 2:30 am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> >> Nehil wrote:
> >> > how can u find whether a no has consecutive zero bits either in from
> >> > LSB or MSB.
> >> > if they exist in between, then what to do? plz clarify.
>
> >>      Are you the same "Nehil" who is undertaking to write
> >> a garbage collector for C?  If so, have you ever heard the
> >> advice "Walk before you run?"
>
> >>      Yes, we all know that "A man's reach should exceed his
> >> grasp," but perhaps by not quite so wide a span ...
>
> >> --
> >> Eric Sosman
> >> esos...@ieee-dot-org.invalid
>
> > ===================================================================
> > Yes, i'm the same Nehil. :) but i could not understand your wordings.
> > i
> > came to this doubt in that project only. i designed a bitmap for each
> > page i allocate, and now want to know how many pages have been
> > allocated by knowing how many bits are there zero??
>
> > is this approach good?
>
> A garbage collector, let alone an efficient garbage collector, is a very
> difficult routine to write. If you can't even do basic bit manipulation - no
> shame on you, we were all beginners at some time - then there is no way you
> are going to achieve a garbage collector, except by some miracle.
>
> --
> Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm

Thanks all for your answers and suggestions.
However i've developed it myself and it is working efficiently. it was
bit manipulation that makes me nervous while thinking logic for it.
but i'll practice for it.

the allocator and de-allocator both r working fine.
Actually, the problem i asked to find some effecient solution for it.
by linear searching, i'd already done it.

thnkas all.

0
Reply nehilparashar (24) 7/15/2007 12:34:48 PM

Nehil wrote:
> how can u find whether a no has consecutive zero bits either in from
> LSB or MSB.

What does "either in from LSB or MSB" mean?

> if they exist in between, then what to do? 

Between what?

 > plz clarify.

I suggest that _you_ clarify.

-- 
Thad
0
Reply ThadSmith (1279) 7/17/2007 4:16:45 PM

8 Replies
28 Views

(page loaded in 0.805 seconds)


Reply: