I need to know how to put bits into a character array.
for example,
I want to put 0001 bits into a character array using C or C++.
Need your help guys. Thanks.
I need to know how to put bits into a character array.
for example,
I want to put 0001 bits into a character array using C or C++.
Need your help guys. Thanks.
Is that really all?
char buf[1];
buf[0] = char(1);
If you want bit masking then it would be something like
enum Enum
{
MASK_01 = 0x1,
MASK_02 = 0x2,
MASK_03 = 0x4,
MASK_04 = 0x8,
};
char buf[4];
buf[0] = Enum::MASK_01;
buf[1] = Enum::MASK_02;
buf[2] = Enum::MASK_03;
buf[3] = Enum::MASK_04;
If you provide information on what you are actually trying to do, we may be able to help you more.
EDIT: Thanks for the extra information. Does this help:
enum Enum
{
BIT_0000000000000001 = 0x0001,
BIT_0000000000000010 = 0x0002,
BIT_0000000000000100 = 0x0004,
BIT_0000000000001000 = 0x0008,
BIT_0000000000010000 = 0x0010,
BIT_0000000000100000 = 0x0020,
BIT_0000000001000000 = 0x0040,
BIT_0000000010000000 = 0x0080,
BIT_0000000100000000 = 0x0100,
BIT_0000001000000000 = 0x0200,
BIT_0000010000000000 = 0x0400,
BIT_0000100000000000 = 0x0800,
BIT_0001000000000000 = 0x1000,
BIT_0010000000000000 = 0x2000,
BIT_0100000000000000 = 0x4000,
BIT_1000000000000000 = 0x8000,
};
int main( int argc, char* argv[] )
{
char someArray[8];
memset( someArray, 0, 8 );
// create an int with the bits you want set
int combinedBits = BIT_0000000000000001|
BIT_0000000000000010|
BIT_1000000000000000;
// clear first two bytes
memset( someArray, 0, 2 );
// set the first two bytes in the array
*(int*)someArray |= combinedBits;
// retrieve the bytes
int retrievedBytes = *(int*)someArray;
// test if a bit is set
if ( retrievedBytes & BIT_0000000000000001 )
{
//do something
}
}
Now the naming of the enums is intentionally intense for clarity. also you may notice that there are only 16 bits in the enum, instead of a possible 32 for an int. This is because you mentioned the first two bytes. Using this method, only the first two bytes of the array will be changed, using those enums. Im not sure if this code would be messed up by endianess, so you will have to make sure you test on your own machines. HTH.
Maybe this more generic code will give you the idea:
void setBitAt( char* buf, int bufByteSize, int bitPosition, bool value )
{
if(bitPosition < sizeof(char)*8*bufByteSize)
{
int byteOffset= bitPosition/8;
int bitOffset = bitPosition - byteOffset*8;
if(value == true)
{
buf[byteOffset] |= (1 << bitOffset);
}
else
{
buf[byteOffset] &= ~(1 << bitOffset);;
}
}
}
//use it as follow:
char chArray[16];
setBitAt(chArray,16*sizeof(char),5,true); //to set bit at pos 5 to 1
You put bits in a character array using C or C++ the way you put anything into anything else -- they're all bits anyway.
Since sizeof(char) == 1
by definition, you can only put 8 bits per element of the array.
If you need help with how to twiddle bits, that's an entirely different issue and has nothing to do with char
s and arrays.
C doesn't support binary literals, so you'll have to represent the value as hex.
char *p;
*p++ = 0x10;
*p++ = 0xFE;
Take a look at the functions hton()
and htonl()
for converting multi-byte values to network-byte-order.