How do I extract bits from 32 bit number

后端 未结 4 1508
小蘑菇
小蘑菇 2020-12-18 12:23

I have do not have much knowledge of C and im stuck with a problem since one of my colleague is on leave.

I have a 32 bit number and i have to extract bits from it.

相关标签:
4条回答
  • 2020-12-18 13:04

    I combined the top 2 answers above to write a C program that extracts the bits for any range of bits (not just 10 through 25) of a 32-bit unsigned int. The way the function works is that it returns bits lo to hi (inclusive) of num.

    #include <stdio.h>
    #include <stdint.h>
    
    unsigned extract(unsigned num, unsigned hi, unsigned lo) {
        uint32_t range = (hi - lo + 1);  //number of bits to be extracted
        //shifting a number by the number of bits it has produces inconsistent 
        //results across machines so we need a special case for extract(num, 31, 0)
        if(range == 32)
            return num;
        uint32_t result = 0;
        //following the rule above, ((1 << x) - 1) << y) makes the mask:
        uint32_t mask = ((1 << range) -1) << lo;
        //AND num and mask to get only the bits in our range
        result = num & mask;
        result = result >> lo; //gets rid of trailing 0s
        return result;
    }
    int main() {
        unsigned int num = 0xd7448eab;
        printf("0x%x\n", extract(num, 10, 25));
    }
    
    0 讨论(0)
  • 2020-12-18 13:12

    You need masks to get the bits you want. Masks are numbers that you can use to sift through bits in the manner you want (keep bits, delete/clear bits, modify numbers etc). What you need to know are the AND, OR, XOR, NOT, and shifting operations. For what you need, you'll only need a couple.

    You know shifting: x << y moves bits from x *y positions to the left*.

    How to get x bits set to 1 in order: (1 << x) - 1

    How to get x bits set to 1, in order, starting from y to y + x: ((1 << x) -1) << y

    The above is your mask for the bits you need. So for example if you want 16 bits of 0xD7448EAB, from 10 to 25, you'll need the above, for x = 16 and y = 10.

    And now to get the bits you want, just AND your number 0xD7448EAB with the mask above and you'll get the masked 0xD7448EAB with only the bits you want. Later, if you want to go through each one, you'll need to shift your result by 10 to the right and process each bit at a time (at position 0).

    The answer may be a bit longer, but it's better design than just hard coding with 0xff or whatever.

    0 讨论(0)
  • 2020-12-18 13:13

    I want bits 10 through 25.

    You can do this:

    unsigned int number = 0xD7448EAB;
    unsigned int value = (number & 0x3FFFC00) >> 10;
    

    Or this:

    unsigned int number = 0xD7448EAB;
    unsigned int value = (number >> 10) & 0xFFFF;
    
    0 讨论(0)
  • 2020-12-18 13:15

    OK, here's how I wrote it:

    #include <stdint.h>
    #include <stdio.h>
    
    main() {
        uint32_t in = 0xd7448eab;
        uint16_t out = 0;
    
        out = in >> 10; // Shift right 10 bits
        out &= 0xffff;  // Only lower 16 bits
        printf("%x\n",out);
    }
    

    The in >> 10 shifts the number right 10 bits; the & 0xffff discards all bits except the lower 16 bits.

    0 讨论(0)
提交回复
热议问题