Question Details

No question body available.

Tags

java integer byte

Answers (3)

March 3, 2026 Score: 5 Rep: 50,679 Quality: Medium Completeness: 100%

What does & 0xFF do?

In Java, byte is a signed integral type. That means it can represent negative numbers as well as positive. It's why the value range of byte is [-128, 127] instead of [0, 255]. And the way Java represents signed integral types is with two's complement.

For example, if you have byte b = -2; then the binary representation of b would be:

11111110

Doing b & 0xFF then does two things:

  1. It first promotes b to an int. This is a widening conversion, which for signed integral types involves sign extension.

  2. It performs the bitwise AND operation on the two operands: (int) b and 0xFF. Note the 0xFF is an int literal and is the hexadecimal representation of 255 (in decimal). The leading zeros are implied: 0x000000FF.

In binary, that would look like:

  11111111 11111111 11111111 11111110 // -2 as a 32-bit int
& 00000000 00000000 00000000 11111111 // 0x000000FF
―――――――――――――――――――――――――――――――――――――
  00000000 00000000 00000000 11111110

The result in decimal is 254 and is the unsigned value of b. In other words, doing b & 0xFF for a given byte b will give you the unsigned value of b as an int. You'll always end up with an int in the range [0, 255].

Note int is also signed. If you want the unsigned value of an int then you need to store it in a long and do & 0xFFFFFFFFL (the L tells Java the literal is a long). That gives you the unsigned value of the int for the same reason & 0xFF gives the unsigned value of a byte. And of course, long is signed as well. But there's no integral primitive large enough to hold the unsigned value of a long, so if you need that then you'd have to move to BigInteger or similar.

Converting byte[4] to int

When converting a byte[4] into an int you need to use the unsigned byte values because you don't want the leading 1 bits of a negative number when bit shifting. Let's say you have 42,117,314 in a byte array:

// 42,117,314
byte[] b = {2, -125, -109, 34};

You would convert that array into an int like so:

int b0 = (b[0] & 0xFF) > 24);
  b[1] = (byte) (i >> 16);
  b[2] = (byte) (i >>  8);
  b[3] = (byte) i;
  return b;
}

Simplified Implementation

I suspect you're learning, and so doing the bit masking and shifting yourself is educational. However, as noted in this answer, you can use java.nio.ByteBuffer to do these conversions for you. For example:

public static int toInt(byte[] b) {
  // This implementation only takes the first 4 bytes of 'b' into account. It
  // will throw BufferUnderflowException if b.length < 4.
  return ByteBuffer.wrap(b).getInt();
}

public static byte[] toBytes(int i) { return ByteBuffer.allocate(4).putInt(i).array(); }

ByteBuffer has methods for working with short (2 bytes) and long (8 bytes) as well. It also makes it easier to change the byte order between big and little endian, as needed. The above example always uses big endian.

Additionally, there is the Byte::toUnsignedInt(byte) method that hides the & 0xFF from you (and the method name makes it clear what's happening for anyone not immediately familiar with what & 0xFF is doing). There are similar methods on Byte and Integer to get the unsigned value as a long.

March 3, 2026 Score: 2 Rep: 11,651 Quality: Low Completeness: 60%

Kind of missing some important things here.

  1. Java ints are signed, so theoritically you cannot convert byte[4] to an int, you have to put it into a long or you risk converting to a negative number.

  2. If you deserializing, meaning want the negative numbers back, you can simply ByteArray.wrap(byte[4]).getInt(). and let the wonderful JDK authors handle the conversion for you.

March 3, 2026 Score: 1 Rep: 44,152 Quality: Low Completeness: 40%

It needs & 0xFF because byte is a signed value in the range -128..127.

((int)(bytes[1])