type conversion - What is a the quickest way to bitwise convert two ints to a long in java? -
i've been using images store data, since editing binary data through paint.net friendlier hex editors.
however, of data long integers. long integers twice size of 32-bit integer in java, 64-bits. how 1 long 2 integers, , more importantly, long when reading image? since java not have unsigned ints, top bit of integer or long negative sign bit, though bit 32 (the lower integer/pixel) ordinary bit in long integer.
most methods of converting long int discard upper bits, well, or may contain bitwise (binary) information!
what need
- transform single long 2 integers faithful contain bit data
- transform 2 integers long faithfully contains bit data.
no need use autoboxing (long
, integer
, etc.). primitives work fine. following best can in java programming language.
join
combining 2 int
s long
int lo; // integer fill lower bits int hi; // integer fill upper bits long val = (((long) hi) << 32) | (lo & 0xffffffffl);
split
retrieving (upper) bits 31-16
int hi = (int) (val >> 32);
retrieving (lower) bits 15-0
int lo = (int) val;
note:
be aware of difference between:
n >> 32
(sign-extend right-shift)n >>> 32
(zero-fill right-shift)
since java used signed bits, >>>
operator introduced handle integers if "unsigned".
Comments
Post a Comment