14 Replies Latest reply: Feb 12, 2007 4:37 PM by 807606 RSS

    Bit Packing (Bytes into Int)

    807606
      I have two bytes and im trying to pack their bits into an int.
      I have it working fine now:
      int result =     ((b2 < 0 ? (b2 + 256) : b2) << 8) | ((b1 < 0 ? (b1 + 256) : b1) << 0);

      Is there a way to do it without checking if the byte is negative?
      I dont want the value of the byte converted to an int. I want the bits
      copy and pasted in.


      For example, this:
      int result =     b2 << 8 | b1;
      wont work for this reason:
      byte= 1010,1010 = -86
      int for -86 = 11111111111111111111111110101010
      int = 1010,1010 = 170



      public class ShiftTest{
      
      public static void main(String[] args) {
           new ShiftTest();
      }
      
      public ShiftTest(){
      
           int num = 682;
      
           byte b1 = (byte)num;
           byte b2 = (byte)(num >>> 8);
      
           int result =     ((b2 < 0 ? (b2 + 256) : b2) << 8) |
                               ((b1 < 0 ? (b1 + 256) : b1) << 0);
           System.out.println("Result: " + result + " , " + Integer.toBinaryString(result));
      
           System.out.println();
           System.out.println("Num Bits: " + Integer.toBinaryString(num));
           System.out.println("B1: " + (b1) + " , " + ((int)b1) + " , " + Integer.toBinaryString((int)b1));
           System.out.println("B2: " + (b2) + " , " + ((int)b2) + " , " + Integer.toBinaryString((int)b2));
      
      }
      
      
      }
        • 1. Re: Bit Packing (Bytes into Int)
          JosAH
          If you want to pack bytes b1 and b2 in an int simply do this:
          int i= ((b1&0xff)<<8)|(b2&0xff);
          kind regards,

          Jos
          • 2. Re: Bit Packing (Bytes into Int)
            796440
            (b && 0xff) << 8 I think.

            Too lazy to try it.

            ---- Edit ----
            Yeah, what Jos said.
            ---- ---- ----
            • 3. Re: Bit Packing (Bytes into Int)
              807606
              And the other way:
              byte b1 = (byte)(num & 0xff);
              byte b2 = (byte)((num >>> 8) & 0xff);
              I'm surprised you didn't know this. The 0xFF trick must have been posted here a million times.
              • 4. Re: Bit Packing (Bytes into Int)
                JosAH
                Jverd wrote:
                Yeah, what Jos said.
                Indeed; would you be so kind to repeat that once more for the record?
                Yeah, what Jos said.
                Thank you; one more for the road please?
                Yeah, what Jos said.
                You're great; thanks a bunch ;-)

                kind regards,

                Jos (<--- what he says ;-)
                • 5. Re: Bit Packing (Bytes into Int)
                  807606
                  Ah, yes.
                  int result2 = ((b2 & 0xff) << 8) | (b1 & 0xff);
                  is certainly the more correct way to do this, lol.

                  D'oh

                  p.s.: and as always thanks
                  except for Jos that modest b@stard, lol.
                  • 6. Re: Bit Packing (Bytes into Int)
                    807606
                    byte b1 = (byte)(num & 0xff);
                    byte b2 = (byte)((num >>> 8) & 0xff);
                    Mr Evil, btw this isnt needed.
                    When int is cast to a byte it automatically just truncates the bits
                    to the last 8.

                    So its just
                    byte b1 = int;
                    byte b2 = int >>> 8
                    (where in this case >>> and >> are interchangeable really)

                    The problem was that for bitshifting the byte is cast up to an int -
                    unhappily if its a negative number because of twos complement.
                    • 7. Re: Bit Packing (Bytes into Int)
                      JosAH
                      Mr Evil, btw this isnt needed.
                      When int is cast to a byte it automatically just truncates the bits
                      to the last 8.

                      So its just
                      byte b1 = int;
                      Nope, the compiler will whine about a possible loss of precision. You
                      have to narrow cast your int to a byte explicitly:

                      byte b1= (byte)i;
                      byte b2= (byte)(i>>8);

                      modest regards,

                      Jos ;-)
                      • 8. Re: Bit Packing (Bytes into Int)
                        807606
                        Mr Evil, btw this isnt needed.
                        When int is cast to a byte it automatically just
                        truncates the bits
                        to the last 8.
                        Sorry, you're right. I was thinking that negative int values would yield negative bytes, but the only int values that would not turn into negative bytes by merely truncating, are all out of a byte's range, so it would be pointless.
                        • 9. Re: Bit Packing (Bytes into Int)
                          JosAH
                          Mr Evil, btw this isnt needed.
                          When int is cast to a byte it automatically just
                          truncates the bits
                          to the last 8.
                          Sorry, you're right.
                          No he isn't. See my reply #7.

                          kind regards,

                          Jos
                          • 10. Re: Bit Packing (Bytes into Int)
                            807606
                            I am right regarding what I was talking about to Mr Evil you Jos
                            are right about what you were arguing:

                            Your:
                            byte b1= (byte)i;
                            byte b2= (byte)(i>>8);

                            is much different than his:
                            byte b1 = (byte)(num & 0xff);
                            byte b2 = (byte)((num >>> 8) & 0xff);
                            You were telling me you indeed need the (byte) cast.
                            Meanwhile what I was saying to him is that he didnt need the & oxff.

                            There was some argument crossfire there, lol.
                            • 11. Re: Bit Packing (Bytes into Int)
                              JosAH
                              I am right regarding what I was talking about to Mr Evil you Jos
                              are right about what you were arguing:

                              Your:
                              byte b1= (byte)i;
                              byte b2= (byte)(i>>8);

                              is much different than his:
                              byte b1 = (byte)(num & 0xff);
                              byte b2 = (byte)((num >>> 8) & 0xff);
                              You were telling me you indeed need the (byte) cast. Meanwhile what
                              I was saying to him is that he didnt need the & oxff.

                              There was some argument crossfire there, lol.
                              Ok, agreed but your example was still wrong; doing a:

                              byte b1= i;

                              makes the compiler whine, i.e. you still need the explicit cast.

                              kind regards,

                              Jos
                              • 12. Re: Bit Packing (Bytes into Int)
                                807606
                                Ok, agreed but your example was still wrong; doing a
                                notice that in my code example in post 1 i did the explicit cast.
                                In my fervor, When I deleted his &oxff i deleted the cast.

                                : P
                                • 13. Re: Bit Packing (Bytes into Int)
                                  JosAH
                                  Ok, agreed but your example was still wrong;
                                  notice that in my code example in post 1 i did the explicit cast.
                                  In my fervor, When I deleted his &oxff i deleted the cast.
                                  Ok, let's call it a day.

                                  kind regards,

                                  Jos
                                  • 14. Re: Bit Packing (Bytes into Int)
                                    807606
                                    Ok, let's call it a day.
                                    Haha, I was just teasing.
                                    I just got home from work to do a full counter of dishes so
                                    im glad you called it - i have an excuse to crash now.

                                    Except for the small matter of dinner...