3

First of all: Im aware of the fact that Java doesnt know any (un)signess

My question concerns the following situation:

Imagine you have an int i = -1 // ( == 0b1111111_11111111_11111111_11111111). Now I want to "convert" this in a long representing the byte value, e.g in this concrete case: long l = 0xFFFFFFFF // ( == 0b1111111_11111111_11111111_11111111).

My question is how one can obtain l from i in the above example.

Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77

3 Answers3

4

You could use bitwise math, should be self explanatory from the code below. Ask in comments if you don't understand.

public class LongConversion {
  public static void main(String args[]) {
    for (int i = -1; i > -40; i -= 9) {
      // THIS IS THE IMPORTANT LINE, THE REST IS JUST FOR DEMONSTRATION
      long l = 0x00000000FFFFFFFFl & (long) i;

      System.out.println("Original i: " + i);
      System.out.println(Integer.toHexString(i));
      System.out.println("Longed i: " + l);
      System.out.println(zeroLong(l));
      System.out.println();
    }
  }

  public static String zeroLong(long l) {
    return String.format("%16s", Long.toHexString(l)).replace(' ', '0');
  }
}

This code outputs:

Original i: -1
ffffffff
Longed i: 4294967295
00000000ffffffff

Original i: -10
fffffff6
Longed i: 4294967286
00000000fffffff6

Original i: -19
ffffffed
Longed i: 4294967277
00000000ffffffed

Original i: -28
ffffffe4
Longed i: 4294967268
00000000ffffffe4

Original i: -37
ffffffdb
Longed i: 4294967259
00000000ffffffdb
durron597
  • 31,968
  • 17
  • 99
  • 158
2

Java 8 added some support for dealing with unsigned types. In this case, you could use Integer.toUnsignedLong, which is equivalent to durron597's answer.

long unsigned = Integer.toUnsignedLong(myIntger);
Community
  • 1
  • 1
mkobit
  • 43,979
  • 12
  • 156
  • 150
-1

You can use the API to do the work:

long l = Long.parseLong(Integer.toBinaryString(i), 2);

The toBinaryString() method javadoc says

Returns a string representation of the integer argument as an unsigned integer in base 2.


Although, this technique is not be as fast as using a bit mask, it is not "slow".

I did some proper timings on my (typical) PC and this conversion took just 270 nanoseconds, which is still pretty damn fast in my book.

Also, it uses the API to do the work instead of bit kung fu, which is easier to read and somewhat self-documenting.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • String parsing is never the right answer in a situation like this. Very slow. – durron597 Nov 29 '12 at 16:37
  • @durron597 Although, it's not as fast as a bit mask, but it's not slow. I did some timings on my (typical) PC and the conversion took just 270 nanoseconds, which is pretty damn fast in my book. And further, it uses the API to do the work, which I thought was a valid alternative to bit kung fu – Bohemian Nov 29 '12 at 19:25
  • how long does bit kung fu take? – durron597 Nov 29 '12 at 19:28
  • @durron597 using a bitmask with the same test harness took 1.6 nanoseconds - yes very fast. But the string version can still churn through 3.7 million ops per second. How fast does it need to be? And would an extra 268 nanoseconds on a process really be noticed? Unless you were doing hard core real-time crunching, it probably wouldn't matter but the code would be easier to read. All I'm saying is that it ain't so bad that this answer "isn't useful" – Bohemian Nov 29 '12 at 19:32
  • You never know... there's no reason to pick the option that's 170 times slower. Unless it's homework and then who cares ;) – durron597 Nov 29 '12 at 19:34
  • How about a good comment instead of a 170x slower solution just to be more clear (although I don't see how that is more clear than bit kung fu (which seems very clear to me)? – brimborium Nov 30 '12 at 08:45
  • btw: It's still a good addition (although I don't advise to use it ^^). -> +1 – brimborium Nov 30 '12 at 08:46
  • your benchmark is very subjective. Consider what would happen if you tried to run this on an embedded environment. – Jaberino Feb 16 '16 at 15:10