Re[3]: [Dev-C++] using char* as bitfield
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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
|