How to get two 12 bit integers from 3 bytes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • James Hannon
    New Member
    • Dec 2010
    • 2

    How to get two 12 bit integers from 3 bytes

    Does anyone know how to recover two 12 bit integers from 3 bytes
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    How much do you know about bit operations? To achieve this, you'll need to do some shifting and masking. There are 8 bits in a byte, so 3 bytes gives you 24 bits.

    There are a lot of ways you can approach this, so you'll need to think about the problem, but here's the general idea...

    3 bytes...
    XXXXXXX YYYYYYYY ZZZZZZZZ

    Map the bits from the source bytes into two 12-bit integers, represented as 16-bit (or 32-bit if you like) integers.

    0000XXXX XXXXYYYY
    0000YYYY ZZZZZZZZ

    (Your mapping may be different, this is just the example I'm using)

    You'll need two masks, one for 8 bits and another for 4 bits.

    Code:
    byte mask4bit = 0xF; // 00001111
    byte mask8bit = 0xFF; // 11111111
    You can get just the bits for each part using bit-wise AND (& symbol) and you can copy them into the destination using bit-wise OR (| symbol).

    For the first number, you'll need to copy the first 8-bits into the destination, then you'll need to shit it left by 12 bits so you can then copy the remaining for bits in. For the second number, you'd copy the first 4 bits, shift left by 12 bits, then copy the next 8 bits over.

    Does that make sense? That's the general idea, you'll need to apply it to your scenario of course. If you have any questions, please let me know.

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      James,

      I certainly hope this isn't a coursework question. If it is, stop reading right now.

      @GaryTexmo has the right of it - you'll need to use bit manipulation operators to get the job done properly.

      What is important is how your input input data are structured. Without knowing this, all we can do is give you some general suggestions as to how to proceed.

      For example, if the three bytes are in the bottom 24 bits of an integer type, then the extraction is very simple:
      Code:
      unsigned value1 = valueIn & 0x0fff;
      unsigned value2 = valueIn >> 12 & 0x0fff;
      On the other hand, if you have three separate bytes that need to be combined, the logic is slightly different:
      Code:
      unsigned long valueInLong = (valueIn0<<16) | (valueIn1<<8) | (valueIn2);
      unsigned value1 = valueIn & 0x0fff;
      unsigned value2 = valueIn >> 12 & 0x0fff;
      
      /* or */
      
      unsigned value1 = (valueIn0<<4) | ((valueIn1&0x00f0)>>4);
      unsigned value2 = ((valueIn1&0x000f)<<8) | (valueIn2);
      Of course, this all goes out the window, if there are big-endian versus little-endian bit ordering issues.

      Cheers!
      Oralloy

      Comment

      • James Hannon
        New Member
        • Dec 2010
        • 2

        #4
        Thankfully not a coursework problem, I'm parsing some information from a external device talking to a port.

        I have the information as byte array, its big endian and my system is little endian. As far as i can tell the format is
        XXXXXXXX XXXXYYYY YYYYYYYY

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          Well, if you search on Yahoo for "swap bits function", you can find a fair number of ways to swap bits. The second item returned is a good forum post giving four or five different swap bits implementations .

          What you do, of course, depends on your performance needs. Do you simply extract as-is, and then do a table look-up, or do you invert the bytes first, then use one of the methods suggested, above.

          Luck!
          Oralloy

          Comment

          Working...