0

I am trying to combine following bit sequences into one variable then i will turn it to a decimal.

*b1=0b001011;
*b2=0b101010;
*b3=0b0001;

Bit order should be like following;

newBin=0001101010001011 (newBin=b3b2b1)

I tried following code piece but couldn't get correct decimal equivalent.

int combine=(*b1<<16)|(*b2<<10)|*b3;

when i printf the combine it is giving 4097 but it should give 6795.

I would appreciate any help / suggestion.

Important notes: I had i already tried combine=(*b1<<12)|(*b2<<6)|*b3; and combine=(*b3<<16)|(*b2<<10)|*b1; results are all same;4097.

in the complete program i am parsing an 32 bit value. I used the code given by "forefinger" from below link.

How do I get bit-by-bit data from an integer value in C?

Following is the complete code of my version;

int *get_bits(int n, int bitswanted){
  int *bits = malloc(sizeof(int) * bitswanted);

  int k;
  for(k=0; k<bitswanted; k++){
    int mask =  1 << k;
    int masked_n = n & mask;
    int thebit = masked_n >> k;
    bits[k] = thebit;
  }

  return bits;
}

int main()
{
    long r=0b0010110100111110000010110110101010000001;
    int i;
    int byte1,byte2,byte3,byte4,byte5;
    //int *Lbits,*Mbits,*Hbits;
    int bw6=6,bw4=4;

    byte1 = (r>>32) & 0xFF;
    byte2 = (r>>24) & 0xFF;
    byte3 = (r>>16) & 0xFF;
    byte4 = (r>>8) & 0xFF;
    byte5 = (r>>0) & 0xFF;

   int *Lbits=get_bits(byte3,bw6);
   int *Mbits=get_bits(byte4,bw6);
   int *Hbits=get_bits(byte5,bw4);

   int combine=(Hbits<<12)|(&Mbits<<6)|Lbits;



  for(i=bw6-1; i>=0;i--)
  {
    printf("%d", Lbits[i]);
  }
  printf("\n");

  for(i=bw6-1; i>=0;i--)
  {
    printf("%d", Mbits[i]);
  }
  printf("\n");

  for(i=bw4-1; i>=0;i--)
  {
    printf("%d", Hbits[i]);
  }
  printf("\n");

  printf("%d",combine);
}
Teo Laferla
  • 41
  • 1
  • 7
  • 1
    Well, you appear to have 6 significant bits, so you should probably left-shift by 6 and 12, respectively. – 500 - Internal Server Error Apr 17 '19 at 10:46
  • If `b1` and `b2` are supposed to have 6 bits each you would have to use `(b3<<12)|(b2<<6)|b1`. Please also show the declarations of `b1`, `b2`, `b3`. – Bodo Apr 17 '19 at 10:48
  • If you guys mean like combine=(*b1<<12)|(*b2<<6)|*b3; i've tried it, same result;4097. – Teo Laferla Apr 17 '19 at 10:50
  • You still didn't show the declaration of `b1`, `b2` and `b3`. You use them as pointers, so you might even have undefined behavior. We don't know without seeing the missing code. – Bodo Apr 17 '19 at 10:59

3 Answers3

0
  1. You are shifting too much, you end up leaving zeroes in between your numbers
  2. Do not use int, you probably want to use unsigned long long int for this or uint64_t Int's size is not specified in the C standard.
  3. I tried running your code in python and it results 763905 to me, so you might have some other bugs that are not shown here.
  4. Why are you using them as pointers? That seems unrelated to the question you're asking here.
LtWorf
  • 7,286
  • 6
  • 31
  • 45
0

So when br3 is MSB it should be:

int combine=(*b3<<12)|(*b2<<6)|*b1;


  0b0001     (1d)  * 4096  (12 left shift)  = 4096
 +0b101010   (42d) *   64  (6 left shift)   = 2688
 +0b001011   (11d) *    1                   =   11
  _________________________________________________
                                               6795
Mike
  • 4,041
  • 6
  • 20
  • 37
  • I am sorry i couldn't i understand how you calculated 6795. printf("%d", combine) gives 4097. Was i doing something wrong? – Teo Laferla Apr 17 '19 at 11:06
0

Your function get_bits doesn't return an int or similar which you could combine by bit shifting and bitwise OR. It returns an int array where every element contains the value of a single bit.

(It would be possible to combine the data from these arrays as needed by processing the bits one by one, but this concept has other pitfalls because you would have to make sure you don't access the dynamically allocate memory out of bounds.)

Here is a modified version of your program that works with unsigned int instead of int arrays.

#include <stdio.h>

unsigned int get_bits(unsigned int n, int bitswanted){
    return n & ((1<<bitswanted)-1);
}

int main()
{
    unsigned long r=0b0010110100111110000010110110101010000001;
    int i;
    int byte1,byte2,byte3,byte4,byte5;

    int bw6=6,bw4=4;

    byte1 = (r>>32) & 0xFF;
    byte2 = (r>>24) & 0xFF;
    byte3 = (r>>16) & 0xFF;
    byte4 = (r>>8) & 0xFF;
    byte5 = (r>>0) & 0xFF;

   int Lbits=get_bits(byte3,bw6);
   int Mbits=get_bits(byte4,bw6);
   int Hbits=get_bits(byte5,bw4);

   int combine=(Hbits<<12)|(Mbits<<6)|Lbits;

   printf("%d\n",combine);
}

The program prints the result 6795.

see https://ideone.com/44pjjF

Bodo
  • 9,287
  • 1
  • 13
  • 29