Java is beaten or between bytes and int

I am trying to execute bitwise or byte value that I have in Java.

For example, I run:

 byte b = (byte)0b11111111; int result = 0 | b; 

My expected result for this would be 0b00000000 00000000 00000000 11111111 or 255 . However, I get -1 or 0b11111111 11111111 11111111 11111111 .

I assume that Java will convert my byte to an int extension through a character extension before performing the operation, and I was just curious if there was a way to get the desired result without using a bit mask ( 0b11111111 ).

+5
source share
1 answer

Using a bitmask is a standard solution to disable character expansion when converting byte to int . You just need to take this piece of Java ugliness.

 int result = 0 | (b & 0xFF); 
+7
source

Source: https://habr.com/ru/post/1212145/


All Articles