473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Bit Test........puzzling

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

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

off+=off;

continue;

}
on++;
}

}

Thanks for looking into it.
Ishi
Nov 13 '05 #1
9 5169
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" <is***********@yahoo.com> wrote in message
news:59**************************@posting.google.c om... 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;nSampl e++)
{

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

off+=off;

continue;

}
on++;
}

}

Thanks for looking into it.
Ishi

Nov 13 '05 #2

Ishira <is***********@yahoo.com> wrote in message
news:59**************************@posting.google.c om...
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;nSampl e++)
{

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

Nov 13 '05 #3
Ishira <is***********@yahoo.com> wrote:
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;nSampl e++)
{

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...
Nov 13 '05 #4
root <ro**@iserv.hvf-bs.net> wrote:
int i;

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

Nov 13 '05 #5
Hi group,
for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSampl e++) declaring nSample inside for loop; is it posible with Ansi C compiler?
Nagaraj.

Ishira <is***********@yahoo.com> wrote in message
news:59**************************@posting.google.c om... 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;nSampl e++)
{

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

off+=off;

continue;

}
on++;
}

}

Thanks for looking into it.
Ishi

Nov 13 '05 #6

nagaraj jois <jo***@tatanova.com> wrote in message
news:n5****************@news.cpqcorp.net...
Hi group,
for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSampl e++)

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

Nov 13 '05 #7
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.





"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<bg**********@slb0.atl.mindspring.net>...
nagaraj jois <jo***@tatanova.com> wrote in message
news:n5****************@news.cpqcorp.net...
Hi group,
for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSampl e++)

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

Nov 13 '05 #8
is***********@yahoo.com (Ishira) wrote in message news:<59**************************@posting.google. com>...
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;nSampl e++)
{

offset=nSample%8;

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

off+=off;

continue;

}
on++;
}

}

Thanks for looking into it.
Ishi


Cheers!
Rich
Nov 13 '05 #9

Ishira <is***********@yahoo.com> wrote in message
news:59**************************@posting.google.c om...
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

Thanks.





"Mike Wahler" <mk******@mkwahler.net> wrote in message

news:<bg**********@slb0.atl.mindspring.net>...
nagaraj jois <jo***@tatanova.com> wrote in message
news:n5****************@news.cpqcorp.net...
Hi group,
> for(int nSample=0,NumSamples=1*8;nSample<NumSamples;nSampl e++)
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

Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Berthold Hoellmann | last post by:
Hello, When I use ./configure --with-thread --with-fpectl --with-signal-module \ --with-pymalloc --enable-shared --with-cxx=g++ make test on 2.3.3 I get
0
by: Remy Blank | last post by:
Ok, here we go. I added the possibility for tests using the unittest.py framework to be skipped. Basically, I added two methods to TestCase: TestCase.skip(msg): skips unconditionally...
4
by: Edvard Majakari | last post by:
Hi, I just found py.test and converted a large unit test module to py.test format (which is actually almost-no-format-at-all, but I won't get there now). Having 348 test cases in the module and...
0
by: Andrea M. Segovia | last post by:
I just compiled (but did not install) perl 5.8.0 on an SGI Origin 300 server (IP35) running IRIX 6.5.20m. Make test reported one test error, which I narrowed down to .../lib/ExUtils/t/Constant.t...
0
by: Jussi Mononen | last post by:
Hi, I'm having problems to successfully execute the test scripts on a Compaq host ( OSF1 tr51bdev V5.1 2650 alpha ). Almost all tests end up with the following error message "PARI: *** ...
0
by: Pieter Van Waeyenberge | last post by:
hello first of all sorry for not replyin to my original post .. been very busy, and i prepared decent test cases now On the following link ull find some safari test cases with issues...
0
by: Tim Haughton | last post by:
I've just released an article on using Test Driven Development with C# and Windows Forms. GUI's are often difficult to test, so I thought it might be of interest. The article along with the...
6
by: ypjofficial | last post by:
HI, I have following terrific confusion. class test { public: int i; int * j; int **k;
15
by: raashid bhatt | last post by:
From 5 -6 day i have been learning c pointers i think i have learned pretty much Please Check me if i am right char (ptr*); // Means a pointer to array of 10 chars char *ptr; // Means array of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.