Thread: Re: [Dev-C++] using char* as bitfield
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: David M. <ci...@ya...> - 2003-09-29 12:19:24
|
Do you know how many bits you will need at compile time? If you then use a bitset. It is part of the STL. If you don't then you can use vector<bool> and hope your compiler vendor optimized it for space. Hope this helps David McKen ----- Original Message ----- From: "Christian Schäfer" <ca...@kr...> To: <dev...@li...> Sent: Monday, September 29, 2003 6:09 AM Subject: [Dev-C++] using char* as bitfield > hi dev-cpp-users, > > i want to allocate a string and then perform operations on its bits, > that I like to use as flags. > > but all the time such operation will give me an error on types.. > > expl: if(mystring & 0x0020) ... > > any thoughts? > > > gruss > /Christian mailto:ca...@kr... > > --- > > > I propose that the following character sequence for joke markers: > > :-) > > 19-Sep-82 11:44 Scott E Fahlman > > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users __________________________________ Do you Yahoo!? The New Yahoo! Shopping - with improved product search http://shopping.yahoo.com |
|
From: <ca...@kr...> - 2003-09-29 12:32:17
|
hi David,
> Do you know how many bits you will need at compile time?
yes, I do.
> If you then use a bitset. It is part of the STL.
I should have said, that I am doing this in c not in c++.
> If you don't then you can use vector<bool> and hope your
> compiler vendor
> optimized it for space.
it should be possible to use a char*, right?
gruss
/Christian mailto:ca...@kr...
---
I propose that the following character sequence for joke markers:
:-)
19-Sep-82 11:44 Scott E Fahlman
|
|
From: Per W. <pw...@ia...> - 2003-09-29 15:08:34
|
Yes, it is possible to do with char*
char* alloc_bits(unsigned bits) {
char *bits;
unsigned size;
size =3D (bits+7)/8;
bits =3D malloc(size);
if (bits) memset(bits,0,size);
return bits;
}
void set_bit(char* bits,unsigned bit) {
array[bit/8] |=3D 1 << (bit&3);
}
char *mybits;
mybits =3D alloc_bits(1000);
if (mybits) set_bit(mybits,117);
/Per W
On Mon, 29 Sep 2003, [ISO-8859-15] Christian Sch=E4fer wrote:
> hi David,
>
> > Do you know how many bits you will need at compile time?
> yes, I do.
>
> > If you then use a bitset. It is part of the STL.
> I should have said, that I am doing this in c not in c++.
>
> > If you don't then you can use vector<bool> and hope your
> > compiler vendor
> > optimized it for space.
>
> it should be possible to use a char*, right?
>
>
>
> gruss
> /Christian mailto:ca...@kr...
>
> ---
>
>
> I propose that the following character sequence for joke markers:
>
> :-)
>
> 19-Sep-82 11:44 Scott E Fahlman
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: <ca...@kr...> - 2003-09-29 15:13:27
|
hi Per,
Monday, September 29, 2003, 5:04:14 PM, you wrote:
> Yes, it is possible to do with char*
> char* alloc_bits(unsigned bits) {
> char *bits;
> unsigned size;
> size = (bits+7)/8;
> bits = malloc(size);
> if (bits) memset(bits,0,size);
> return bits;
> }
> void set_bit(char* bits,unsigned bit) {
> array[bit/8] |= 1 << (bit&3);
> }
> char *mybits;
> mybits = alloc_bits(1000);
> if (mybits) set_bit(mybits,117);
> /Per W
wow, thanks!
gruss
/Christian mailto:ca...@kr...
---
I propose that the following character sequence for joke markers:
:-)
19-Sep-82 11:44 Scott E Fahlman
|
|
From: <ca...@kr...> - 2003-09-29 19:57:07
|
hi Per,
I made some changes to your proposed code.
> bits = malloc(size);
> if (bits) memset(bits,0,size);
now is:
bits = calloc(size, sizeof(char));
this should do the trick.
just thought I'd post it.
gruss
/Christian mailto:ca...@kr...
---
I propose that the following character sequence for joke markers:
:-)
19-Sep-82 11:44 Scott E Fahlman
|
|
From: <ca...@kr...> - 2003-09-30 08:24:38
|
hi Per,
I'll show you my code now. somehow it does not work as planned..
char *alloc_bits(unsigned int);
void set_bit(char*, unsigned int);
int main(int argc, char *argv[])
{
char *mybits;
mybits = alloc_bits(60);
if (mybits) printf("bits[%d]: %X\n",sizeof(mybits), *mybits);
if (mybits) set_bit(mybits,2);
if (mybits) printf("bits[%d]: %X\n",sizeof(mybits), *mybits);
system("PAUSE");
return 0;
}
char *alloc_bits(unsigned int bits)
{
return calloc(((bits+7)/8), sizeof(char));
}
void set_bit(char* bits,unsigned int bit)
{
bits[bit/8] |= 1 << (bit&3);
}
what exactly does the set_bit() function do?
I don't understand it fully especially the (bit&3) part.
also I am totally lost in writing a get_bit() function..
any help appreciated!
gruss
/Christian mailto:ca...@kr...
---
I propose that the following character sequence for joke markers:
:-)
19-Sep-82 11:44 Scott E Fahlman
|
|
From: Per W. <pw...@ia...> - 2003-09-30 09:25:39
|
First:
sizeof(mybits) is just size of pointer - not size of the array in bytes or
bits.
mybits[bit/8] |=3D 1 << (bit&3);
isn't so complicated. Each character contains 8 bits. So by dividing bit
with 8, I get which character the bit is stored in.
bit&3 will use the 3 least significant bits for a numeric range of 0-7.
1 << (bit&3) will then access one of the 8 bits in the character.
char test_bit(const char* mybits,unsigned bit) {
return (mybits[bit/8] | (1 << (bit&3))) !=3D 0;
}
char clear_bit(const char *mybits,unsigned bit) {
mybits[bit/8] &=3D ~(1 << (bit&3));
}
/Per W
On Tue, 30 Sep 2003, Christian Sch=E4fer wrote:
> hi Per,
>
> I'll show you my code now. somehow it does not work as planned..
>
> char *alloc_bits(unsigned int);
> void set_bit(char*, unsigned int);
>
> int main(int argc, char *argv[])
> {
> char *mybits;
>
> mybits =3D alloc_bits(60);
>
> if (mybits) printf("bits[%d]: %X\n",sizeof(mybits), *mybits);
>
> if (mybits) set_bit(mybits,2);
>
> if (mybits) printf("bits[%d]: %X\n",sizeof(mybits), *mybits);
>
> system("PAUSE");
> return 0;
> }
>
> char *alloc_bits(unsigned int bits)
> {
> return calloc(((bits+7)/8), sizeof(char));
> }
>
> void set_bit(char* bits,unsigned int bit)
> {
> bits[bit/8] |=3D 1 << (bit&3);
> }
>
> what exactly does the set_bit() function do?
> I don't understand it fully especially the (bit&3) part.
> also I am totally lost in writing a get_bit() function..
>
> any help appreciated!
>
>
> gruss
> /Christian mailto:ca...@kr...
>
> ---
>
>
> I propose that the following character sequence for joke markers:
>
> :-)
>
> 19-Sep-82 11:44 Scott E Fahlman
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: <ca...@kr...> - 2003-09-30 10:25:33
|
hi Per,
Tuesday, September 30, 2003, 11:25:00 AM, you wrote:
> First:
> sizeof(mybits) is just size of pointer - not size of the array in bytes or
> bits.
yes, my fault. corrected. ;-)
> mybits[bit/8] |= 1 << (bit&3);
> isn't so complicated. Each character contains 8 bits. So by dividing bit
> with 8, I get which character the bit is stored in.
that was clear.
> bit&3 will use the 3 least significant bits for a numeric range of 0-7.
understood.
> 1 << (bit&3) will then access one of the 8 bits in the character.
ok. with you.
everything seems clear to me now. but the out put of my current code,
will be:
mybits: 0000 0000 0000 0000
mybits: 0400 0000 0000 0000
this does mean that the first byte changed from
(bin) 00000000 to
(bin) 00000100
right?
this should be
(bin) 0100000
(hex) 40
since I changed the second bit of all, right?
somewhere I made a mistake, I know, but where?
char *alloc_bits(unsigned int);
void set_bit(char*, unsigned int);
int get_bit(const char*, unsigned int);
void clear_bit(char*, unsigned int bit);
int main(int argc, char *argv[])
{
char *mybits;
mybits = alloc_bits(60);
if (mybits) printf("mybits: %X%X %X%X %X%X %X%X (%s)\n", *(mybits+0), *(mybits+1), *(mybits+2), *(mybits+3), *(mybits+4), *(mybits+5), *(mybits+6), *(mybits+7), mybits);
if (mybits) set_bit(mybits,2);
if (mybits) printf("mybits: %X%X %X%X %X%X %X%X (%s)\n", *(mybits+0), *(mybits+1), *(mybits+2), *(mybits+3), *(mybits+4), *(mybits+5), *(mybits+6), *(mybits+7), mybits);
system("PAUSE");
return 0;
}
char *alloc_bits(unsigned int bits)
{
return calloc(((bits+7)/8), sizeof(char));
}
void set_bit(char *bits,unsigned int bit)
{
bits[bit/8] |= 1 << (bit&3);
}
int get_bit(const char *bits, unsigned int bit)
{
return ((bits[bit/8] | (1 << (bit&3))) != 0) ? bit : 0;
}
void clear_bit(char *mybits,unsigned int bit) {
mybits[bit/8] &= ~(1 << (bit&3));
}
gruss
/Christian mailto:ca...@kr...
---
I propose that the following character sequence for joke markers:
:-)
19-Sep-82 11:44 Scott E Fahlman
|
|
From: Per W. <pw...@ia...> - 2003-09-30 11:20:40
|
Sorry, I must have got brain-fever
It should be bit&7 not bit&3
Don't know where the 3 came from in the first example, but then I managed
to continue with it for the test_bit() and clear_bit() functions too...
Am I bad or just a bit tired :-)
The code I sent defined bit 0 as least significant bit of first char, and
bit 7 as most significant bit of first char.
If you want to reverse order of bits within a character, you can use
0x80 >> (bit&7)
Then you will allocate bits from high bit to low in each character.
Also, the bits are zero-relative. If you do set_bit(mybits,2) the relevant
character should contain binary value 00000100 (4 decimal) if you use the
bit order of my example, or 00100000 (32 decimal) if you use the reverse
bit order of (0x80 >> (bit&7)).
/Per W
On Tue, 30 Sep 2003, Christian Sch=E4fer wrote:
> hi Per,
>
> Tuesday, September 30, 2003, 11:25:00 AM, you wrote:
> > First:
> > sizeof(mybits) is just size of pointer - not size of the array in bytes=
or
> > bits.
> yes, my fault. corrected. ;-)
>
> > mybits[bit/8] |=3D 1 << (bit&3);
>
> > isn't so complicated. Each character contains 8 bits. So by dividing bi=
t
> > with 8, I get which character the bit is stored in.
> that was clear.
>
> > bit&3 will use the 3 least significant bits for a numeric range of 0-7.
> understood.
>
> > 1 << (bit&3) will then access one of the 8 bits in the character.
> ok. with you.
>
> everything seems clear to me now. but the out put of my current code,
> will be:
>
> mybits: 0000 0000 0000 0000
> mybits: 0400 0000 0000 0000
>
> this does mean that the first byte changed from
>
> (bin) 00000000 to
> (bin) 00000100
>
> right?
> this should be
>
> (bin) 0100000
> (hex) 40
>
> since I changed the second bit of all, right?
>
> somewhere I made a mistake, I know, but where?
>
>
> char *alloc_bits(unsigned int);
> void set_bit(char*, unsigned int);
> int get_bit(const char*, unsigned int);
> void clear_bit(char*, unsigned int bit);
>
> int main(int argc, char *argv[])
> {
> char *mybits;
>
> mybits =3D alloc_bits(60);
>
> if (mybits) printf("mybits: %X%X %X%X %X%X %X%X (%s)\n", *(mybits+0),=
*(mybits+1), *(mybits+2), *(mybits+3), *(mybits+4), *(mybits+5), *(mybits+=
6), *(mybits+7), mybits);
>
> if (mybits) set_bit(mybits,2);
>
> if (mybits) printf("mybits: %X%X %X%X %X%X %X%X (%s)\n", *(mybits+0),=
*(mybits+1), *(mybits+2), *(mybits+3), *(mybits+4), *(mybits+5), *(mybits+=
6), *(mybits+7), mybits);
>
> system("PAUSE");
> return 0;
> }
>
> char *alloc_bits(unsigned int bits)
> {
> return calloc(((bits+7)/8), sizeof(char));
> }
>
> void set_bit(char *bits,unsigned int bit)
> {
> bits[bit/8] |=3D 1 << (bit&3);
> }
>
> int get_bit(const char *bits, unsigned int bit)
> {
> return ((bits[bit/8] | (1 << (bit&3))) !=3D 0) ? bit : 0;
> }
>
> void clear_bit(char *mybits,unsigned int bit) {
> mybits[bit/8] &=3D ~(1 << (bit&3));
> }
>
>
>
> gruss
> /Christian mailto:ca...@kr...
>
> ---
>
>
> I propose that the following character sequence for joke markers:
>
> :-)
>
> 19-Sep-82 11:44 Scott E Fahlman
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|