468,491 Members | 2,048 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

character array as bits

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
Dec 17 '07 #1
15 9062
On 17 Aralęk, 23:42, prakash437 <prakash...@gmail.comwrote:
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[i] >j) & 1;
printf("%d", bit);
}
printf(" ");
}

return 0;
}
Dec 17 '07 #2
In article <df**********************************@i3g2000hsf.g ooglegroups.com>,
prakash437 <pr********@gmail.comwrote:
>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.
--
So you found your solution
What will be your last contribution?
-- Supertramp (Fool's Overture)
Dec 17 '07 #3
On Dec 17, 1:42 pm, prakash437 <prakash...@gmail.comwrote:
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.
Dec 17 '07 #4
prakash437 wrote:
>
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.

--
pete
Dec 17 '07 #5
prakash437 wrote:
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;
}
Dec 18 '07 #6
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

On Dec 18, 12:22 am, pete <pfil...@mindspring.comwrote:
prakash437 wrote:
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.

--
pete
Dec 18 '07 #7
[Please don't top post. Corrected.]

prakashraovadd...@googlemail.com wrote:
pete <pfil...@mindspring.comwrote:
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.

...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.

--
Peter
Dec 18 '07 #8
user923005 <dc*****@connx.comwrites:
On Dec 17, 1:42 pm, prakash437 <prakash...@gmail.comwrote:
>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.)

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 18 '07 #9
On Dec 17, 10:01 pm, Keith Thompson <ks...@mib.orgwrote:
user923005 <dcor...@connx.comwrites:
On Dec 17, 1:42 pm, prakash437 <prakash...@gmail.comwrote:
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.)
I have some older Unix-type machines here that have <values.hinstead
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.).
Dec 18 '07 #10
Martin Ambuhl <ma*****@earthlink.netwrites:
prakash437 wrote:
>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.
[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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 18 '07 #11
pr***************@googlemail.com wrote:
>
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))))

--
pete
Dec 18 '07 #12
pete <pf*****@mindspring.comwrites:
#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)
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
Dec 18 '07 #13
Ben Pfaff wrote:
>
pete <pf*****@mindspring.comwrites:
#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)
OK, next time.

--
pete
Dec 19 '07 #14
pr***************@googlemail.com wrote:
>
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)

--
Posted via a free Usenet account from http://www.teranews.com

Dec 20 '07 #15
On Dec 18, 6:50 am, CBFalconer <cbfalco...@yahoo.comwrote:
prakashraovadd...@googlemail.com wrote:
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:
Sorry for that .. I make sure i do not do it again ..

And i thank you all for your replies ..

Prakash

>
--
<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)

--
Posted via a free Usenet account fromhttp://www.teranews.com
Dec 20 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by DraguVaso | last post: by
8 posts views Thread by hello smith | last post: by
21 posts views Thread by aegis | last post: by
8 posts views Thread by Brand Bogard | last post: by
17 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
35 posts views Thread by rajash | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.