When you convert 0x00FF to binary, it becomes 0000 0000 1111 1111
When you and all with 1, it itself:
1 && 1 = 1, 0 && 1 = 0
When you and everything with 0, it's 0
1 && 0 = 0, 0 && 0 = 0
When this operation occurs encodedArr[j] = (byte) (data[i] & 0x00FF); It takes the last 8 bits and the last 8 bits of data only and saves them. It discards the first 8 bits and stores the last 8
The reason this is necessary is because a byte is defined as an eight-bit value. Bitwise and exists to stop potential overflow → IE by assigning 9 bits per byte
A char in Java is 2 bytes ! This logic is to stop the overflow. However, as someone remarked, this is pointless because the actor does it for you. Perhaps someone was careful?
source share