Question

How can i store a bit in a bit? I need to extract various no. of bits from different variables and put them in the buffer as shown.

unsigned char a;
a=5;

Now I want to take LSB and store it in a bit of a buffer of type unsigned char.

unsigned char buffer[5];

For extraction I am using

a & 00000001

Now how to store it and further bits after this?

Was it helpful?

Solution

I'm not sure of what you want to do but here is an exemple of bitwise shifting.

unsigned char b;
unsigned char c;

b = a & (1 << 0); /* Will store the least significant bit of a in b*/
c = a & (1 << 1); /* Will store the 2nd least significant bit of a in c*/

You can use this to create variables that only have a limited amount of bits.

typedef struct bit_s
{
    unsigned int    a : 1; /* only 1 bit available */
    unsigned int    b : 12; /* only 12 bits available */
}              bit_t;

bit_t var;

var.a = 0;
var.a = 1;
var.a = 2; /* Will overflow the variable and create a warning with -Woverflow */

OTHER TIPS

use shift operator >>

buffer[0] = a & 0x01; // 1 will be stores to buffer[0] if LSB of a is one
buffer[1] = (a >> 1) & 0x01; // 1 will be stored to buffer[1] if 2nd bit of a is one.

Something like:

(apologies for syntax errors etc. this is untested code! ) 


unsigned char getbit;
unsigned char storeBit[5];

for (x = 0; x < inBufferLength ; x++) {
  // get least significant bit
  getbit = inBuffer[x] & x00000001;
  int ByteNum = x / 8;
  int BitNum = x % 8;
  if (getBit) {
    switch (BitNum) {
     case 0;
        storeBit[ByteNum] = storeBit[ByteNum] | x10000000;
        break; 
     case 1;
        storeBit[ByteNum] = storeBit[ByteNum] | x01000000;
        break; 
     case 2;
        storeBit[ByteNum] = storeBit[ByteNum] | x00100000;
        break;
     case 3;
        storeBit[ByteNum] = storeBit[ByteNum] | x00010000;
        break; 
     case 4;
        storeBit[ByteNum] = storeBit[ByteNum] | x00001000;
        break; 
     case 5;
        storeBit[ByteNum] = storeBit[ByteNum] | x00000100;
        break; 
     case 6;
        storeBit[ByteNum] = storeBit[ByteNum] | x00000010;
        break; 
     case 7;
        storeBit[ByteNum] = storeBit[ByteNum] | x00000001;
        break;
     }
  }

Setting bits...

/** Set bit in any sized bit block.
 *
 * @return   none
 *
 * @param   bit    - Bit number.
 * @param   bitmap - Pointer to bitmap.
 *
 * @note    Please note that this function does not know the size of the
 *          bitmap and it cannot range check the specified bit number.
 */
void SetBit( int bit, unsigned char *bitmap)
{
    int n, x;

    x = bit / 8;                        // Index to byte.
    n = bit % 8;                        // Specific bit in byte.

    bitmap[x] |= (1 << n);      // Set bit.
}

Resetting bits...

/** Reset bit in any sized mask.
 *
 * @return  None
 *
 * @param   bit    - Bit number.
 * @param   bitmap - Pointer to bitmap.
 *
 * @note    Please note that this function does not know the size of the
 *          bitmap and it cannot range check the specified bit number.
 */
void ResetBit( int bit, unsigned char *bitmap)
{
    int n, x;

    x = bit / 8;                        // Index to byte.
    n = bit % 8;                        // Specific bit in byte.

    bitmap[x] &= (1 << n);              // Reset bit.
}

Toggle bit...

/** Toggle bit in any sized bit mask.
 *
 * @return   none
 *
 * @param   bit    - Bit number.
 * @param   bitmap - Pointer to bitmap.
 *
 * @note    Please note that this function does not know the size of the
 *          bitmap and it cannot range check the specified bit number.
 */
void ToggleBit( int bit, unsigned char *bitmap)
{
    int n, x;

    x = bit / 8;                        // Index to byte.
    n = bit % 8;                        // Specific bit in byte.

    bitmap[x] ^= (1<<n);        // Toggle bit.
}

Checking bit...

/** Checks specified bit.
 *
 * @return  1 if bit set else 0.
 *
 * @param   bit    - Bit number.
 * @param   bitmap - Pointer to bitmap.
 *
 * @note    Please note that this function does not know the size of the
 *          bitmap and it cannot range check the specified bit number.
 */
int IsBitSet( int bit, const unsigned char *bitmap)
{
    int n, x;

    x = bit / 8;            // Index to byte.
    n = bit % 8;            // Specific bit in byte.

    // Test bit (logigal AND).
    if (bitmap[x] & (1<<n))
        return 1;

    return 0;
}

Is bit reset...

/** Checks specified bit.
 *
 * @return  1 if bit reset else 0.
 *
 * @param   bit    - Bit number.
 * @param   bitmap - Pointer to bitmap.
 *
 * @note    Please note that this function does not know the size of the
 *          bitmap and it cannot range check the specified bit number.
 */
int IsBitReset( int bit, const unsigned char *bitmap)
{
    return IsBitSet( bit, bitmap) ^ 1;
}

Hope this helps...

Usefull bit twiddling hacks...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top