 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
David White Guest
|
Posted: Thu Jul 31, 2003 2:16 am Post subject: Re: Bit test.........driving me crazy |
|
|
Ishira <ishira_pundit (AT) yahoo (DOT) com> wrote
| Quote: | 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.
| Quote: | 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).
| Quote: | unsigned char *pData=(unsigned char *)bits;
int offset;
for(int nSample=0,NumSamples=1*8;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.
| Quote: | {
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.
Where is 'off' defined?
This code is looking more like a bit-counter than a bit-tester.
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
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
|
|
| Back to top |
|
 |
Kevin Goodsell Guest
|
Posted: Thu Jul 31, 2003 5:41 am Post subject: Re: Bit test.........driving me crazy |
|
|
Ishira wrote:
This causes the behavior of the program to be undefined. You may not
modify a variable twice without an intervening sequence point.
As for the rest of your code, I don't know what it is intended to do.
Please post a *complete* program, with an explanation of what you
expected to happen, and what you actually observed.
-Kevin
|
|
| Back to top |
|
 |
Ishira Guest
|
Posted: Thu Jul 31, 2003 4:30 pm Post subject: Re: Bit test.........driving me crazy |
|
|
Sorry. I should have been more specific.
I want to count the number of ONs and OFFs in the char stream of 1s
and 0s.
This data of 1s and 0s is obtained from a capture board. So they are
basically signals.
I would like to use bit masking.
int nbytes;
int on=0,off=0;
unsigned char * pData="1001000100001010";
for(int nSample=0,NumSamples=nbytes*8;nSample<NumSamples;nSample++)
{
offset=nSample%8;
if( (pData[nSample/8] & (1u << offset)) == 0)
{
off++;
continue;
}
on++;
}
Thank you so much.
Ishi
[email]ishira_pundit (AT) yahoo (DOT) com[/email] (Ishira) wrote in message news:<595bee7e.0307301723.7e6f0ec9 (AT) posting (DOT) google.com>...
| Quote: | I have a simple bit test program here. It just doesnt seem to be getting it right.
What am I doing wrong?
char bits[8]={0,1,0,1,0,0,0,0};
unsigned char *pData=(unsigned char *)bits;
int offset;
for(int nSample=0,NumSamples=1*8;nSample
{
offset=nSample%8;
if( (pData[nSample/8] & (1u << offset)) == 0)
{
off=off++;
continue;
}
on++;
}
}
|
|
|
| Back to top |
|
 |
Kevin Goodsell Guest
|
Posted: Thu Jul 31, 2003 5:40 pm Post subject: Re: Bit test.........driving me crazy |
|
|
Ishira wrote:
| Quote: | Sorry. I should have been more specific.
I want to count the number of ONs and OFFs in the char stream of 1s
and 0s.
This data of 1s and 0s is obtained from a capture board. So they are
basically signals.
|
You seem to be representing the data in chars with one char for each
signal. You don't need (and shouldn't use) any bit masking operations in
this case.
| Quote: |
I would like to use bit masking.
|
Then you should choose a different representation for your data, but
first consider whether you'll gain anything useful by doing so. Saving a
few hundred bytes of memory is not usually worth bothering with more
complicated and error-prone code.
| Quote: |
int nbytes;
int on=0,off=0;
unsigned char * pData="1001000100001010";
|
nbytes = (get the number of chars in the data, somehow)
for (int i=0; i
{
if (pData[i] == '1') { ++on; }
else { ++off; }
}
-Kevin
|
|
| Back to top |
|
 |
john smith Guest
|
Posted: Thu Jul 31, 2003 8:27 pm Post subject: Re: Bit test.........driving me crazy |
|
|
you can count on a byte by byte basis using bitset. Assuming you have a
stream of data pointed to by char* p, and size of that stream is stored in
numbytes:
unsigned int numberofones = 0;
for(int i = 0; i < numbytes; ++i) {
bitset<8> tmp(p[i]);
numberofones += tmp.count();
}
cout << "number of ones: " << numberofones << " number of zeors: " <<
8*numbytes-numberofones << endl;
good luck
smith
"Ishira"
| Quote: | Sorry. I should have been more specific.
I want to count the number of ONs and OFFs in the char stream of 1s
and 0s.
This data of 1s and 0s is obtained from a capture board. So they are
basically signals.
I would like to use bit masking.
int nbytes;
int on=0,off=0;
unsigned char * pData="1001000100001010";
for(int nSample=0,NumSamples=nbytes*8;nSample<NumSamples;nSample++)
{
offset=nSample%8;
if( (pData[nSample/8] & (1u << offset)) == 0)
{
off++;
continue;
}
on++;
}
Thank you so much.
Ishi
[email]ishira_pundit (AT) yahoo (DOT) com[/email] (Ishira) wrote in message
news:<595bee7e.0307301723.7e6f0ec9 (AT) posting (DOT) google.com>...
I have a simple bit test program here. It just doesnt seem to be getting
it right.
What am I doing wrong?
char bits[8]={0,1,0,1,0,0,0,0};
unsigned char *pData=(unsigned char *)bits;
int offset;
for(int nSample=0,NumSamples=1*8;nSample
{
offset=nSample%8;
if( (pData[nSample/8] & (1u << offset)) == 0)
{
off=off++;
continue;
}
on++;
}
}
|
|
|
| Back to top |
|
 |
Ishira Guest
|
Posted: Thu Jul 31, 2003 10:20 pm Post subject: Re: Bit test.........driving me crazy |
|
|
Yes. Thanks. You are right. 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.
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.
Kevin Goodsell <usenet1.spamfree.fusion (AT) neverbox (DOT) com> wrote
| Quote: | Ishira wrote:
Sorry. I should have been more specific.
I want to count the number of ONs and OFFs in the char stream of 1s
and 0s.
This data of 1s and 0s is obtained from a capture board. So they are
basically signals.
You seem to be representing the data in chars with one char for each
signal. You don't need (and shouldn't use) any bit masking operations in
this case.
I would like to use bit masking.
Then you should choose a different representation for your data, but
first consider whether you'll gain anything useful by doing so. Saving a
few hundred bytes of memory is not usually worth bothering with more
complicated and error-prone code.
int nbytes;
int on=0,off=0;
unsigned char * pData="1001000100001010";
nbytes = (get the number of chars in the data, somehow)
for (int i=0; i
{
if (pData[i] == '1') { ++on; }
else { ++off; }
}
-Kevin
|
|
|
| Back to top |
|
 |
Kevin Goodsell Guest
|
Posted: Fri Aug 01, 2003 4:25 am Post subject: Re: Bit test.........driving me crazy |
|
|
Please stop top-posting. Re-read section 5 of the FAQ for posting
guidelines: http://www.parashift.com/c++-faq-lite/
Ishira wrote:
| Quote: | Yes. Thanks. You are right. 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.
|
I really don't think that's what you want. I recommend one of the following:
std::string pData = "000011101001100"; // whatever
OR
unsigned long pData = 0xA2680; // whatever
The first is considerably easier to deal with.
-Kevin
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|