3

I've been using images to store data, since editing binary data through Paint.net is much friendlier than most hex editors.

However, some of my data is long integers. Long integers are twice the size of a 32-bit integer in java, 64-bits. How does one get the long to two integers, and more importantly, back to a long when reading the image? Since Java does not have unsigned ints, the top bit of the integer or long is the negative sign bit, even though bit 32 (the lower integer/pixel) will be an ordinary bit in the long integer.

Most methods of converting long to int discard the upper bits, as well, which will or may contain bitwise (binary) information!

What I need to do is

  1. Transform a single long into two integers that faithful contain its bit data
  2. Transform two integers back into a long that faithfully contains their bit data.
  • [Duplicate of: Java storing two ints in a long](http://stackoverflow.com/questions/12772939/java-storing-two-ints-in-a-long) – Mr. Polywhirl Feb 06 '14 at 00:33
  • Though the tricky part is getting it back, the bitshifting isn't a big deal, search didn't uncover a solution for the second part. –  Feb 06 '14 at 00:39
  • Possible duplicate of [Java storing two ints in a long](https://stackoverflow.com/questions/12772939/java-storing-two-ints-in-a-long) – Vadzim May 28 '17 at 07:56

3 Answers3

9

No need to use Autoboxing (Long, Integer, etc.). Primitives work just fine. The following is the best you can do in the Java programming language.

Join

Combining two ints into a long

int lo; // Integer to fill lower bits
int hi; // Integer to fill upper bits
long val = (((long) hi) << 32) | (lo & 0xffffffffL);

 

Split

Retrieving the (upper) bits 31-16

int hi = (int) (val >> 32);

Retrieving the (lower) bits 15-0

int lo = (int) val;

 

Note:

Be aware of the difference between:

  • n >> 32 (Sign-extend right-shift)
  • n >>> 32 (Zero-fill right-shift)

Since Java used only signed bits, the >>> operator was introduced to handle integers as if they were "unsigned".

Community
  • 1
  • 1
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • Is the bitwise AND really necessary? Integers are always 4 bytes no matter what, am I missing something? Is there a value where int & 0xffffffffL changes anything? – Wasabi Thumbs May 02 '22 at 13:36
1
int u,v
long n = u;
if (v < 0) { ++n; }
n <<= 32;
n += v;
lkreinitz
  • 281
  • 3
  • 9
  • 2
    While this code may answer the question, providing additional context regarding *how* and/or *why* it solves the problem would improve the answer's long-term value. - [From Review](http://stackoverflow.com/review/low-quality-posts/13311895) – Michael Parker Aug 12 '16 at 15:36
-1

You'll either need to make two new longs with the same values or you can typecast the integers.

int x = 0;
(long) int x = 2;

This declares a new integer with a set value of 0 and then changes the value to 2 once it becomes a long.

Simple response, but i hope it helps

ChriskOlson
  • 513
  • 1
  • 3
  • 12