5 Replies Latest reply on May 31, 2011 9:52 AM by 800965

    unsigned primitives

    800965
      Hi everyone,

      I would like to write a BMP file format import in Java. The problem is Java doesn't support unsigned primitive types. There are numerous fields declared in the BMP file format as unsigned integer or unsigned short. Now, if I put them in a short or int primitive in Java I understand that I will get wrong results, the numbers will not be represented correctly. I'm not sure what would happen exactly. So, how can I import these fields correctly. What is a workaround?

      Thanks,
      PR.
        • 1. Re: unsigned primitives
          Kayaman
          Aardenon wrote:
          Now, if I put them in a short or int primitive in Java I understand that I will get wrong results, the numbers will not be represented correctly.
          Not really. The bit pattern would be exactly the same, the only difference is how it would be displayed. Let's say you have an unsigned byte 0xFF that you need to read in. You read it in and print it out and Java displays it as -1, which is correct for a signed value, even though the real value is meant to be 255.
          So, how can I import these fields correctly. What is a workaround?
          You can use a bigger variable, such as int for bytes and shorts and a long for ints. That's the simplest way to do. Of course you can also use the regular sized variable and just make sure that when conversions occur you treat them properly (usually involving the bitwise AND operator).
          • 2. Re: unsigned primitives
            jschellSomeoneStoleMyAlias
            Let's say you have an unsigned byte 0xFF that you need to read in. You read it in and print it out and Java displays it as -1, which is correct for a signed value, even though the real value is meant to be 255.
            Simple enough to print it correctly....

            printlin("x=" + ((0x0FF) & x)))

            The 0x0FF is a bit mask and has nothing to do with your example value.
            1 person found this helpful
            • 3. Re: unsigned primitives
              800965
              Sorry for a late reply, I was busy with something else meanwhile. I have one more question, so I fully understand.
              Kayaman wrote:
              Not really. The bit pattern would be exactly the same, the only difference is how it would be displayed.
              So does it mean I can actually do unsigned arithmetic on the signed ints in Java and the results stored will be correct only the interpretation displayed will be not as unsigned but as signed ints? It's been a long time I had such things at school and I don't remember how it works exactly.

              PR.
              • 4. Re: unsigned primitives
                Kayaman
                Aardenon wrote:
                So does it mean I can actually do unsigned arithmetic on the signed ints in Java and the results stored will be correct only the interpretation displayed will be not as unsigned but as signed ints?
                Basically yes. And jschell demonstrated how to display them correctly too (at least when dealing with bytes).

                Issues come up when you start to convert them to larger datatypes and Java treats them as signed values. For example here:
                    byte b = -1; // Value is 0xFF, so 255 as unsigned
                    int c = b;   // Value is now 0xFFFFFFFF, i.e. still -1 as unsigned, but not 255 as unsigned
                
                    c = (b & 0xFF); // c is now 255, i.e. 0x000000FF
                So we just need to bitmask the "extra" higher bits out when doing conversions (or displaying).
                1 person found this helpful
                • 5. Re: unsigned primitives
                  800965
                  Great, thanks very much.

                  PR.