Ernestas Gruodis
Ernestas Gruodis

Reputation: 8787

What is the purpose of byteBuffer.get() & 0xFF?

I found this method:

private static int getInt16(ByteBuffer input) {
    return ((input.get() & 0xFF) << 8) | (input.get() & 0xFF);
}

What is the purpose of input.get() & 0xFF? Isn't it the same as input.get()?

Upvotes: 2

Views: 422

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49803

This is pulling bytes (which are 8 bits in size) from input and combining them into Int16s, which is what the method returns. The & 0xFF is insuring that the bytes haven't been sign-extended (which may be unnecessary, but can't hurt).

Upvotes: 2

Related Questions