ZoriaRPG

Some Binary Bit Extraction Utilities

Aug 8th, 2018
105
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Function to extract a series of bits from a value.
  2. // Params: 'num_bits' bits from 'init_pos' position (e.g., pos 8, 8 bits, pos 16, 8m bits, pos 24 8 bits.
  3. // Returns the extracted value as int.
  4. // Signed values may have undefined behaviour based on endian-ness, so we use abs(). -Z
  5. int extract_bits(int base_val, int num_bits, int init_pos)
  6. {
  7.     unsigned int tempVal = abs(base_val); //Disregard sign.
  8.     return ( ( (1 << num_bits) - 1 ) & ( tempVal >> (init_pos - 1) ) );
  9. }
  10.  
  11. //int foo 11110010001100111100010000110100; //11110010 00110011 11000100 00110100
  12. //int lowestEight = bitExtract(foo,8,8); //00110100
  13. //int secondEight = bitExtract(foo,8,16); //11000100
  14.  
  15. // I don't know if any of these have specific merit beyond the above. -Z
  16.  
  17. unsigned short extract(unsigned short value, int begin, int end)
  18. {
  19.     unsigned short mask = (1 << (end - begin)) - 1;
  20.     return (value >> begin) & mask;
  21. }
  22.  
  23. int extractBit(int byte, int pos)
  24. {
  25.     if( !((pos >= 0) && (pos < 16)) )
  26.     {
  27.         return 0;
  28.     }
  29.     return ( ( byte & (1<<pos) ) >> pos);
  30. }
  31.  
  32. uint32_t getBits( uint32_t allbits, int lsb, int count )
  33. {
  34.     uint32_t mask = ~(0xffffffff << count) << lsb ;
  35.     return (allbits & mask) >> lsb ;
  36. }
  37.  
  38. uint32_t getBits( uint32_t allbits, int lsb, int msb )
  39. {
  40.     uint32_t mask = ~(0xffffffff << (msb + 1 - lsb)) << lsb ;
  41.     return (allbits & mask) >> lsb ;
  42. }
  43.  
  44.  
  45. int extractBitRange(int byte, int startingPos, int offset)
  46. {
  47.  
  48.  
  49.    if(  !(((startingPos + offset) >= 0) && ( (startingPos + offset) < 16)) )
  50.    {
  51.         return 0;
  52.    }
  53.    return ( byte >> startingPos ) & ~(0xff << (offset + 1));
  54. }
  55.  
  56. unsigned extract(unsigned num, unsigned hi, unsigned lo) {
  57.     uint32_t range = (hi - lo + 1);  //number of bits to be extracted
  58.     //shifting a number by the number of bits it has produces inconsistent
  59.     //results across machines so we need a special case for extract(num, 31, 0)
  60.     if(range == 32)
  61.         return num;
  62.     uint32_t result = 0;
  63.     //following the rule above, ((1 << x) - 1) << y) makes the mask:
  64.     uint32_t mask = ((1 << range) -1) << lo;
  65.     //AND num and mask to get only the bits in our range
  66.     result = num & mask;
  67.     result = result >> lo; //gets rid of trailing 0s
  68.     return result;
  69. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×