Performing bitwise left shifts on "signed" data in Java - is it better to switch to JNI?

I asked questions about hexadecimal and bitwise manipulation (here and elsewhere) last week, trying to wrap my head around their representation in Java. After much thought and disappointment, I should ask for the last time how to perform logical arithmetic on bits that should be unsigned, but presented as signed in Java.

Good: I port the program in C # to Java. The program deals with processing bitmap images, and since so much of the data in the application is presented as an unsigned 8-bit integer. There are many suggestions to use the data type in Java instead to "mimic" as close as possible to an 8-byte unsigned value. byteshort

I do not believe that this is possible for me, since C # code performs various shift operations and And with my byte data. For example, if it datais a byte array, and this code block exists in C #:

int cmdtype = data[pos] >> 5;
int len = (data[pos] & 0x1F) + 1;

if (cmdtype == 7)
{
    cmdtype = (data[pos] & 0x1C) >> 2;
    len = ((data[pos] & 3) << 8) + data[pos + 1] + 1;
    pos++;
}

, data short , Java. , , 8- , 16- , . ? , " " byte 0XFF char Java , , .

, , . , JNI. , uint8_t # byte.

JNI? , , . Java, ? , , .

.

+5
2

: short, .

, 1111 1101 (-5) short 1111 1111 1111 1101 (-5), 0000 0000 1111 1101 (253). AND :

short shortValue = ((short)(byteValue)) & 0xFF;

, , , Java Swing. - , . , "" , , , , .

+3

Java >>> ( " > ", ) . , # ( , ).

"", # 32- , . : # ( 24 0 , 7). Java, - :

short shortCast = ( (short)byteVal ) & 0xff;
short shiftedShortCast = shortCast << whatever;
+2

All Articles