简体   繁体   中英

How to combine 3 binary data in C?

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);
}
  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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM