character array as bits

P

prakash437

hello everyone,
I have asked a similar question on the C++ group, however, i did not
get the answer i was expecting ...

How can i use a unsigned char array (of size, say 10 characters) as a
set of 80 bits, and apply a whole range of bit operations (>>, <<, &
|) etc ...

Thanks for your time ..
Prakash
 
A

Ali Karaali

hello everyone,
I have asked a similar question on the C++ group, however, i did not
get the answer i was expecting ...

How can i use a unsigned char array (of size, say 10 characters) as a
set of 80 bits, and apply a whole range of bit operations (>>, <<, &
|) etc ...

Thanks for your time ..
Prakash

#include <stdio.h>

int main()
{
unsigned char array[10];
int i, j, bit;

for( i = 0; i < 10; ++i) {
for( j = 0; j < 8; ++j) {
bit = (array >> j) & 1;
printf("%d", bit);
}
printf(" ");
}

return 0;
}
 
W

Walter Roberson

prakash437 said:
I have asked a similar question on the C++ group, however, i did not
get the answer i was expecting ...
How can i use a unsigned char array (of size, say 10 characters) as a
set of 80 bits, and apply a whole range of bit operations (>>, <<, &
|) etc ...

You cannot do that in standard C, except by writing routines that
do the operations part-by-part.
 
U

user923005

hello everyone,
I have asked a similar question on the C++ group, however, i did not
get the answer i was expecting ...

How can i use a unsigned char array (of size, say 10 characters) as a
set of 80 bits, and apply a whole range of bit operations (>>, <<, &
|) etc ...

It's a FAQ:
20.8: How can I implement sets or arrays of bits?

A: Use arrays of char or int, with a few macros to access the
desired bit at the proper index. Here are some simple macros
to
use with arrays of char:

#include <limits.h> /* for CHAR_BIT */

#define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))

(If you don't have <limits.h>, try using 8 for CHAR_BIT.)

References: H&S Sec. 7.6.7 pp. 211-216.
 
P

pete

prakash437 said:
hello everyone,
I have asked a similar question on the C++ group, however, i did not
get the answer i was expecting ...

How can i use a unsigned char array (of size, say 10 characters) as a
set of 80 bits, and apply a whole range of bit operations (>>, <<, &
|) etc ...

The way that you can:
use a unsigned char array (of size, say 10 characters)
as a set of 80 bits,
and apply a whole range of bit operations (>>, <<, &, |) etc ...
is:
one byte at a time.
 
M

Martin Ambuhl

prakash437 said:
hello everyone,
I have asked a similar question on the C++ group, however, i did not
get the answer i was expecting ...

How can i use a unsigned char array (of size, say 10 characters) as a
set of 80 bits, and apply a whole range of bit operations (>>, <<, &
|) etc ...

Those bit operations are defined only for unsigned integer scalars. You
could, of course, do something like

#include <string.h>

int main(void)
{
unsigned long shiftreg; /* long long doesn't exist in
C++ */
unsigned char chararray[sizeof(unsigned long)];

/* code that does other stuff, including storing stuff in char array */

memcpy(&shiftreg, chararray, sizeof shiftreg);
/* do bit operations on the chars stored in shiftreg */
memcpy(chararray, &shiftreg, sizeof shiftreg);

/* etc */
return 0;
}
 
W

Walter Roberson

Those bit operations are defined only for unsigned integer scalars. You
could, of course, do something like
#include <string.h>

int main(void)
{
unsigned long shiftreg; /* long long doesn't exist in
C++ */
unsigned char chararray[sizeof(unsigned long)];

/* code that does other stuff, including storing stuff in char array */
memcpy(&shiftreg, chararray, sizeof shiftreg);
/* do bit operations on the chars stored in shiftreg */
memcpy(chararray, &shiftreg, sizeof shiftreg);
/* etc */
return 0;
}

If you proceed that way, then you need to know the byte order
of your long. The bit operators (except ~) are defined by -value-,
and when you push the bits into the long, you might find that the
least value bit of the long corresponds to something in the
second or third character rather than to something in the last
character.
 
P

prakashraovaddina

Thanks for your replies ..

However, what i am looking is not something to mask or unmask a bit
but something that could allow me to treat the whole buffer as a set
of bits ..

Importantly i am looking to apply a shift operation whose effect can
be felt on the whole buffer .. However, i assume such an
implementation would be quite difficult unless i am ready to work on
each byte at a time ..

Prakash
 
P

Peter Nilsson

[Please don't top post. Corrected.]

...i am looking to apply a shift operation whose effect can
be felt on the whole buffer .. However, i assume such an
implementation would be quite difficult unless i am ready to
work on each byte at a time ..

No, such an implementation is quite _easy_ if you are ready
to work on each byte at a time.

Note: You can also use unsigned int (or unsigned long), not
just unsigned char, to implement bit sets.
 
J

Jack Klein

On Mon, 17 Dec 2007 17:01:54 -0800 (PST),
Thanks for your replies ..

You're welcome, but you could show your gratitude better by not top
posting. New material that you add to a thread belongs after the
quoted material you are replying to.
However, what i am looking is not something to mask or unmask a bit
but something that could allow me to treat the whole buffer as a set
of bits ..

Importantly i am looking to apply a shift operation whose effect can
be felt on the whole buffer .. However, i assume such an
implementation would be quite difficult unless i am ready to work on
each byte at a time ..

Yes, you have to work on one byte at a time. There are other
possibilities that work with some compilers, but are non-portable and
will fail on other compilers.

See the fax encoding and decoding examples on
http://jk-technology.com/C_Unleashed/code_list.html for some idea of
how to shift arbitrary numbers of bits across byte boundaries.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
C

CBFalconer

However, what i am looking is not something to mask or unmask a
bit but something that could allow me to treat the whole buffer
as a set of bits ..

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 
K

Keith Thompson

user923005 said:
I have asked a similar question on the C++ group, however, i did not
get the answer i was expecting ...

How can i use a unsigned char array (of size, say 10 characters) as a
set of 80 bits, and apply a whole range of bit operations (>>, <<, &
|) etc ...

It's a FAQ:
20.8: How can I implement sets or arrays of bits?

A: Use arrays of char or int, with a few macros to access the
desired bit at the proper index. Here are some simple macros to
use with arrays of char:

#include <limits.h> /* for CHAR_BIT */ [snip]
(If you don't have <limits.h>, try using 8 for CHAR_BIT.)

References: H&S Sec. 7.6.7 pp. 211-216.

Hmm. I'm a little surprised the FAQ still has that "If you don't have
<limits.h>" clause. There should be vanishingly few C implementations
still in use that don't have it. (Yes, it's required for freestanding
implementations as well.)
 
U

user923005

It's a FAQ:
20.8: How can I implement sets or arrays of bits?
A: Use arrays of char or int, with a few macros to access the
desired bit at the proper index. Here are some simple macros to
use with arrays of char:
#include <limits.h> /* for CHAR_BIT */ [snip]
(If you don't have <limits.h>, try using 8 for CHAR_BIT.)
References: H&S Sec. 7.6.7 pp. 211-216.

Hmm. I'm a little surprised the FAQ still has that "If you don't have
<limits.h>" clause. There should be vanishingly few C implementations
still in use that don't have it. (Yes, it's required for freestanding
implementations as well.)

I have some older Unix-type machines here that have <values.h> instead
of limits.h. People tend to keep Unix machines a long time (longer
than they should really) and so we support ancient 1980's stuff. I
would say it is still pertinent because real systems that people are
using to get work done are still configured in that way. I did
recently show one customer that a used, 5 year old machine {same
vendor chain, but updated by a decade from what they were using} would
outperform their relic by at least 20:1 up to 500:1 in all areas
(disk, CPU, Net, etc.).
 
K

Keith Thompson

Martin Ambuhl said:
Those bit operations are defined only for unsigned integer scalars.
[snip]

Well, that's not *quite* correct. The bitwise operations are defined
for both signed and unsigned types. But for signed types,
particularly for negative values, the results can be
implementation-defined and/or undefined (I haven't bothered to
memorize which it is).

Bitwise operations on signed types are rarely as useful as bitwise
operations on unsigned types, but the compiler probably won't warn you
if you apply them to signed types.
 
P

pete

Thanks for your replies ..

However, what i am looking is not something to mask or unmask a bit
but something that could allow me to treat the whole buffer as a set
of bits ..

Importantly i am looking to apply a shift operation whose effect can
be felt on the whole buffer .. However, i assume such an
implementation would be quite difficult unless i am ready to work on
each byte at a time ..

The biggest thing that shift operators will work on,
is the biggest integer type.

If you alter your goal,
you could use macros to work with sizeof(int) bytes at a time,
on objects
that don't have size or alignment problems with type unsigned,
accessing them through an (unsigned int) type lvalue,
(or sizeof(long) or maybe long long, if you write your own macros)
/*
** Some bitwise macros for unsigned U
*/
#define READ_UBIT(U, N) ((U) >> (N) & 1u)
#define FLIP_UBIT(U, N) ((void)((U) ^= 1u << (N)))
#define SET_UBIT(U, N) ((void)((U) |= 1u << (N)))
#define CLEAR_UBIT(U, N) ((void)((U) &= ~(1u << (N))))
 
B

Ben Pfaff

pete said:
#define READ_UBIT(U, N) ((U) >> (N) & 1u)

Perhaps you have memorized the relative precedence of >> and &.
But many programmers have not. Thus, I would prefer to see this
written as:
#define READ_UBIT(U, N) (((U) >> (N)) & 1u)
 
P

pete

Ben said:
Perhaps you have memorized the relative precedence of >> and &.
But many programmers have not. Thus, I would prefer to see this
written as:
#define READ_UBIT(U, N) (((U) >> (N)) & 1u)

OK, next time.
 
P

prakash437

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

Sorry for that .. I make sure i do not do it again ..

And i thank you all for your replies ..

Prakash
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top