473,412 Members | 2,284 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,412 software developers and data experts.

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 10363
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DraguVaso | last post by:
Hi, I need to a way to find the 7-bit code from a character. Or a function like Asc() but that will give the character code based on the 7-bit ecoding (I needf it fo convert text to GSM 03.38)....
8
by: hello smith | last post by:
Hello, I have an unsigned char array. I want to determine if each char's ascii value is less than 127. Is there a faster way than looping through the characters and checking if each is less than...
21
by: aegis | last post by:
7.4#1 states The header <ctype.h> declares several functions useful for classifying and mapping characters.166) In all cases the argument is an int, the value of which shall be representable as an...
8
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
3
by: roopa.v1 | last post by:
Hi, How to assign long to character array and later extract it
14
by: mast2as | last post by:
Hi everyone, I am trying to implement some specs which specify that an array of parameter is passed to a function as a pointer to an array terminated by a NULL chatacter. That seemed fairly easy...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
17
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when...
35
by: rajash | last post by:
Hello everyone, Thanks again for all the suggestions, though I think some people are a bit fussy in their answers. Here is a solution to Exercise 1.14. It deals well with control characters...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.