Velocity Reviews > C++ > Re: Bit test.........driving me crazy

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

David White
Guest
Posts: n/a

 07-31-2003
Ishira <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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;nSampl e++)

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;nSampl e++)

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