Bit Test........puzzling

M

Mike Wahler

Ishira said:
Hello all,

I have written a program to test bits in a bit stream. I cant figure
out what is wrong with it.

I have a char * array of 0's and 1's and I basically need to go
through this array and test for ONs and OFFs.

void On_OFF(){

char *pData="11010111";

int offset;

for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSample++)
{

offset=nSample%8;


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

off+=off;

continue;

}


on++;
}

}

#include <stdio.h>

void On_OFF(void)
{
char *pData="11010111";
char *p = pData;

while(*p)
{
printf("pData[%lu] : %s\n", (unsigned long)(p - pData),
*p - '0' ? "ON" : "OFF");
++p;
}
}

int main()
{
On_OFF();
return 0;
}

Output:

pData[0] : ON
pData[1] : ON
pData[2] : OFF
pData[3] : ON
pData[4] : OFF
pData[5] : ON
pData[6] : ON
pData[7] : ON


If you mean something else, please be more clear
with your questions.

-Mike
 
M

Mike Wahler

nagaraj jois said:
Hi group,
declaring nSample inside for loop; is it posible with Ansi C compiler?

Yes, it's valid C99 (but not C89).

BTW please don't top-post. Thank you.

-Mike
 
M

Mike Wahler

Ishira said:
Thank you all so much for your time.

I was representing the data wrong.

What I needed to do was
unsigned char pData[]="0xff,0xff";
and not represent each bit value (0/1) as a char by itself like this
unsigned char pData[]="11111111";


Duh! Thank you all for replying. Sometimes it is just enough to have
someone to listen to your question. Things seem to become clear
miraculously.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

void on_off(const unsigned char *data)
{
static const char *values[] = {"ON", "OFF"};
int mask = 0;
size_t i = 0;

while(*data)
{
printf("data[%lu] == 0x%X ==", (unsigned long)i++, *data);

for(mask = (UCHAR_MAX + 1) / 2; mask; mask /= 2)
printf(" %-3s", values[!(*data & mask)]);

putchar('\n');
++data;
}
}

int main()
{
on_off("\xD7\xA6");
return 0;
}


Output:

data[0] == 0xD7 == ON ON OFF ON OFF ON ON ON
data[1] == 0xA6 == ON OFF ON OFF OFF ON ON OFF


HTH,
-Mike
 
I

Ishira

Hello all,

I have written a program to test bits in a bit stream. I cant figure
out what is wrong with it.

I have a char * array of 0's and 1's and I basically need to go
through this array and test for ONs and OFFs.

void On_OFF(){

char *pData="11010111";

int offset;

for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSample++)
{

offset=nSample%8;


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

off+=off;

continue;

}


on++;
}

}

Thanks for looking into it.
Ishi
 
G

GADRE

You are trying to compare
if( (pData[nSample/8] & (1u << offset)) == 0)

the value of character '1' which is 0x33 (or Decimal 49) in ASCII to the
binary value 1u<<offset, which would be 1,2,4,8,16 etc.

your loop can be rewritten in a simple way as (assuning on and off are
globals).

// uncompiled and thus untested code
void On_OFF()
{
char *pData="11010111";
char *p;
for (p = pData; *p != '\0'; p++)
if (*p == '1')
on++;
else if (*p == '0')
off++;
else
{
// error. you only expect '1' or '0'
}
}


Gurus here will come up with a tighter code for the same thing.

My 2c

numlokoff.

The value of character '1' is generally 0x33 (Deciman 49)
Ishira said:
Hello all,

I have written a program to test bits in a bit stream. I cant figure
out what is wrong with it.

I have a char * array of 0's and 1's and I basically need to go
through this array and test for ONs and OFFs.

void On_OFF(){

char *pData="11010111";

int offset;

for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSample++)
{

offset=nSample%8;


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

off+=off;

continue;

}


on++;
}

}

Thanks for looking into it.
Ishi
 
R

root

Ishira said:
Hello all,

I have written a program to test bits in a bit stream. I cant figure
out what is wrong with it.

I have a char * array of 0's and 1's and I basically need to go
through this array and test for ONs and OFFs.

void On_OFF(){

char *pData="11010111";

int offset;

for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSample++)
{

offset=nSample%8;


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

off+=off;

continue;

}


on++;
}

}

Thanks for looking into it.
Ishi


Maybe you want something like this:

char *pData = '\xFF\00\FF\00';

int i;

for(i=0; i<4*8; i++)
on += pData[i / 8] & (1 << (i % 8));

off = 4*8 - on;

untested...
 
N

nagaraj jois

Hi group,
for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSample++)
declaring nSample inside for loop; is it posible with Ansi C compiler?
Nagaraj.

Ishira said:
Hello all,

I have written a program to test bits in a bit stream. I cant figure
out what is wrong with it.

I have a char * array of 0's and 1's and I basically need to go
through this array and test for ONs and OFFs.

void On_OFF(){

char *pData="11010111";

int offset;

for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSample++)
{

offset=nSample%8;


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

off+=off;

continue;

}


on++;
}

}

Thanks for looking into it.
Ishi
 
I

Ishira

Thank you all so much for your time.

I was representing the data wrong.

What I needed to do was
unsigned char pData[]="0xff,0xff";
and not represent each bit value (0/1) as a char by itself like this
unsigned char pData[]="11111111";


Duh! Thank you all for replying. Sometimes it is just enough to have
someone to listen to your question. Things seem to become clear
miraculously.

Thanks.
 
R

Rich Grise

Hello all,

I have written a program to test bits in a bit stream. I cant figure
out what is wrong with it.

I have a char * array of 0's and 1's and I basically need to go
through this array and test for ONs and OFFs.

void On_OFF(){

char *pData="11010111";

int offset;

for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSample++)
{

offset=nSample%8;

/*********************************************************************/
if( (pData[nSample/8] & (1u << offset)) == 0)
/*********************** ^^ ***********************************/
{

off+=off;

continue;

}


on++;
}

}

Thanks for looking into it.
Ishi

Cheers!
Rich
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top