0

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.

Ragy
  • 51
  • 1
  • 3
  • 1
    For bit twiddling see http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c – SiegeX Mar 05 '10 at 01:57
  • @Ragy: Lets say we want to write a function to do the job, what will be the function signature? Particularly, how would you represent your "bits"? – Arun Mar 05 '10 at 02:04

4 Answers4

2

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.

0xC0DEFACE
  • 8,825
  • 7
  • 34
  • 35
  • Hey thanks, but my problem is completely different. I am designing a Reliable data transmission over UDP, where in the UDP data buffer which is a character array and in my first 2 bytes of it I have to put bits like 00010000.... and so on and I want to know how to achieve this. Please let me know if you need any info, thanks in advance for your help, I really appreciate it. – Ragy Mar 06 '10 at 03:49
2

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
Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
uray
  • 11,254
  • 13
  • 54
  • 74
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 chars and arrays.

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
0

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.

tomlogic
  • 11,489
  • 3
  • 33
  • 59