Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the need of a bitwise "and" for some char to byte conversions in Java?

Here I'm working with a Java to C# sample app translation that involves cryptography (AES and RSA and so on...)

At some point in Java code (the one that actually works and being translated to C#), I've found this piece of code:

for (i = i; i < size; i++) {
    encodedArr[j] = (byte) (data[i] & 0x00FF);
    j++;
} // where data variable is a char[] and encodedArr is a byte[]

After some googling (here), I've seen that this is a common behaviour mainly on Java code...

I know that char is a 16-bit type and byte is 8-bit only, but I couldn't understand the reason for this bitwise and operation for a char->byte conversion.

Could someone explain?

Thank you in advance.

like image 377
cezarlamann Avatar asked May 28 '15 19:05

cezarlamann


3 Answers

In this case, it is quite unnecessary, but the sort of thing people put in "just in case". According to the JLS, 5.1.3. Narrowing Primitive Conversion

A narrowing conversion of a char to an integral type T likewise simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the resulting value to be a negative number, even though chars represent 16-bit unsigned integer values.

Similar code is often needed in widening conversions to suppress sign extension.

like image 66
Patricia Shanahan Avatar answered Oct 21 '22 19:10

Patricia Shanahan


When you convert 0x00FF to binary it becomes 0000 0000 1111 1111

When you and anything with 1, it is itself:

1 && 1 = 1, 0 && 1 = 0

When you and anything with 0, it is 0

1 && 0 = 0, 0 && 0 = 0

When this operation occurrs encodedArr[j] = (byte) (data[i] & 0x00FF); it's taking the last 8 bits and the last 8 bits only of the data and storing that. It is throwing away the first 8 bits and storing the last 8

The reason why this is needed is because a byte is defined as an eight bit value. The bitwise and exists to stop a potential overflow -> IE assigning 9 bits into a byte

A char in Java is 2 bytes! This logic is there to stop an overflow. However, as someone pointed out below, this is pointless because the cast does it for you. Perhaps someone was being cautious?

like image 34
Dudemanword Avatar answered Oct 21 '22 20:10

Dudemanword


It's a way to truncate the value by keeping only the least significat bits so it "fits" in a byte!

I hope it helps!

like image 33
tooomg Avatar answered Oct 21 '22 18:10

tooomg