-1
U32 BitMap[6] /* 6 words for 96 persons*/

How to make a program having loop to Read 6 words in above bitmap, which we have to read 2 bits per person and store person id and result in tPersonMsg

/* 2 Bits representing  00-> default value, 01->Command Successful, 10->Command Failed
   * | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 |  9 |  8 |  7 |  6 |  5 |  4 |  3 |  2 |  1 |  0 |
   * |<Pr15>|--------------------------------------------------------------------------------------------------------------------------------------|<Pr2>|<Pr1>|<Pr0>|
   * | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 |  9 |  8 |  7 |  6 |  5 |  4 |  3 |  2 |  1 |  0 |
   * |<P31>|--------------------------------------------------------------------------------------------------------------------------------------|<P18>|<P17>|<P16>|
--- similarly for 96 persons*/

to get the result of the persons for which command has failed in following structure.

typedef enum eFinalResult {
    Succ= 0,
    Fail = 1,
    noResponse = 2,
} eFinalResult ;

typedef struct {
   U32                        Person_Id;
   tFinalResult         Person_Result;
} tResult;


typedef struct {
   U32             NumPersons;
   tResult         Result[32];
} tPersonMsg;

I am not here to annoy anybody , I am a beginner in C programming
Till now I am trying to make program as follows:

for (i=0; i<6; i++)  /* Loop for 6 words*/  
{  
   k = 0;  
   t= 0x3;  
   for (j=0; j<31; j+2) /* Loop for bits to reach even position like 0th bit,2nd bit,4th ...and so on*/  
   {  
      bits = (a[i] & t) >>j;  
      k++;  
      if (a[i] == 2)  
      {  
         Command Failed  
         Person Id = j/2;  
      }  
    t = t<<2;  
    }  
}  
Sim
  • 3
  • 2

3 Answers3

1

For one bit, for the case when char == 8 bits.

int get1bit(unsigned char *array, int bitpos)
{
int res = array[bitpos >> 3];
res >>= (bitpos & 0x07);
return(res & 0x01);
}
dsnk
  • 123
  • 6
1

You have observed that six, 32 bit words are required to hold the data for 96 people: 96 people x 2 bits per person = 192 bits of data, and 192 / 32 = 6 words to hold them.

You can also see that one word will contain 32 / 2 bits per result = 16 results.

So, to find the correct word you divide the person's ID by 16 and the remainder is the 'index' of their result bits within the word. Use the index multiplied by 2 (the number of bits in each result) to shift the word containing the result right, so that the correct result bits are in the least significant position and mask out the remaining bits to obtain the result.

static const U32 BITS_PER_RESULT = 2;
static const U32 RESULTS_PER_WORD = 32 / BITS_PER_RESULT;
static const U32 RESULT_MASK = 0x3;

// The following line is commented out because, although implemented in
// several compilers, it is not part of the C standard (at the moment).
/*static const U32 RESULT_MASK = 0b11;*/

tResult FindResultForPerson(U32 personId)
{
    // Find the word in the results array that contains the relevant bits.
    U32 resultBits = BitMap[personId / RESULTS_PER_WORD];

    // Shift the result word right so that the required bits are in the
    // least significant bit position.
    resultBits >>= ((personId % RESULTS_PER_WORD) * BITS_PER_RESULT);

    // Mask out any more significant bits to leave only the required result.
    return resultBits & RESULT_MASK;
}

At some point you will want to ensure that the value passed to the function in personId is not out of range and that the BitMap array contains correctly formatted and valid data, but that's further down the line...

Evil Dog Pie
  • 2,300
  • 2
  • 23
  • 46
  • @chux Good point [GCC extension](http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html), also Clang and TCC, I believe. Its also part of the C++14 standard, but that's not C. – Evil Dog Pie Sep 08 '15 at 16:01
  • Thanks for giving the answer – Sim Sep 09 '15 at 11:25
-1

if The real question is to get the value of the two bits at any position. the answer would be to prepare the mask. n&(n-1) would always check the value of the last bit(depends on the Arch of the processor also). or the easy step would be to use a mask of max 32 bits or 64 bits (again depends on the ARCH). Stackoverflow has many questions related to Masking and get the value of the bits.

  • @ Rajavelu What is "n" here? As I want to ready 2 bits then again next 2 bits then again next 2 bits.... – Sim Sep 08 '15 at 06:28
  • n would be the number (integer) to take reference.The question doesnt provide what exactly you are looking for as mentioned in the previous comment what command refers here and your code is not displayed completely.So provided the flow to be followed.If you are looking for a solution ,please refer the same or if you simply wants to annoy.please this would not be the platform for the same. – Rajavelu Prabhu Sep 08 '15 at 06:52
  • I am not here to annoy, I am a novice. Till now – Sim Sep 08 '15 at 07:13