Re: Bit test.........driving me crazy

Discussion in 'C++' started by David White, Jul 31, 2003.

  1. David White

    David White Guest

    Ishira <> wrote in message
    news:...
    > I have a simple bit test program here. It just doesnt seem to be getting

    it right.
    >
    > What am I doing wrong?


    What are you trying to do? I think I can tell, but you should explain it
    clearly.

    > char bits[8]={0,1,0,1,0,0,0,0};


    Why not make this unsigned char as well? Also, it would be a better test to
    turn on multiple bits and different bits in each char (but that can come
    later).

    > unsigned char *pData=(unsigned char *)bits;
    >
    > int offset;
    >
    > for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSample++)


    I understand "bit test" to mean finding out whether a specific bit is a 1 or
    a 0. To do so does not require a loop.

    > {
    > offset=nSample%8;
    > if( (pData[nSample/8] & (1u << offset)) == 0)


    The range of nSample is 0 to 7, so dividing it by 8 will always give you
    zero.

    > {
    > off=off++;


    Where is 'off' defined?

    This code is looking more like a bit-counter than a bit-tester.

    >
    > continue;
    >
    > }
    >
    >
    > on++;


    Where is 'on' defined?

    > }
    >
    >
    >
    > }


    What does this } belong to?

    It looks as though you are trying to count all the 1 and 0 bits in the 64
    bits that make up your 8 chars, but you only have a single loop of 8 to do
    it. You need either one loop of 64 or two nested loops of 8 each.

    Returning to this line:
    for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSample++)

    What is NumSamples for? You don't need it. And are you sure 1*8 is right and
    not 8*8?

    Try this instead and see what happens:
    for(int nSample=0; nSample < sizeof(bits)*8; ++nSample)

    DW
    David White, Jul 31, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Gower

    ItemTemplate error driving me crazy

    William Gower, Apr 23, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    307
    William Gower
    Apr 23, 2004
  2. =?Utf-8?B?VGltOjouLg==?=

    Insert and Variables driving me crazy! Newbie...

    =?Utf-8?B?VGltOjouLg==?=, Dec 20, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    326
    =?Utf-8?B?VGltOjouLg==?=
    Dec 21, 2004
  3. Shapper
    Replies:
    0
    Views:
    321
    Shapper
    Apr 29, 2005
  4. Joe Blanchard via .NET 247
    Replies:
    1
    Views:
    757
    Eliyahu Goldin
    May 15, 2005
  5. Ishira
    Replies:
    4
    Views:
    391
    Kevin Goodsell
    Aug 1, 2003
Loading...

Share This Page