Java &0XFF Example
For understanding the &0XFF (or &0xff) we must introduce with bitwise AND operator (&), conversion from hexadecimal to binary and vice-versa and decimal to binary and vice-versa. Before moving ahead in this section, we should also understand of shifting of operators.
Bitwise Right Shift Operator
The Right Shift Operator shifts the bits of the number towards right a specified n number of positions. Right shift operator represented by the symbol >>, read as double greater than. When you write x>>n, the meaning is to shift the bits x towards the right n specified positions.
The operator >> shifts the bits towards the right and also preserve the sign bit, which is the leftmost bit. The leftmost bit represents the sign of the number. The sign bit 0 represents a positive number, and 1 represents a negative number. So, after performing >> on a positive number, we get a positive value in the result also. When we perform >> on a negative number, again we get a negative value.
If x=10, then calculate x>>2 value.
Shifting the value of x towards the right two positions will make the rightmost 2 bits to be lost. The value of x is 10. The binary representation of 10 is 00001010. The procedure to do right shift explained in the following example:
Observe the above example, after shifting the bits to the right the binary number 00001010 (in decimal 10) becomes 00000010 (in decimal 2).
Difference Between >> and >>> operator
Both >> and >>> are used to shift the bits towards the right. The difference is that the >> preserve the sign bit while the operator >>> does not preserve the sign bit. To preserve the sign bit, we need to add 0 in the MSB.
What is &0XFF?
In general, &0XFF is basically a bitwise AND (&) operator that extracts the lowest 8 bits from a number. Sometimes it is also called a mask. Usually, it is used in color representation, IP address representation, etc.
The ARGB value is an integer, so it is represented in memory by 4 bytes (or equivalently 32 bits representation).
Consider the following ARGB value:
00000001 00000010 00000011 00000100
Each byte represents a color component:
It can also be represented as 0xAARRGGBB (alpha, red, green, blue). By using the bitwise-and with 0xFF, we just keep the last component that is blue. For other colors, we use the following:
In the case of alpha, we can skip &0xFF, because it doesn't do anything; same for shifting by 0 in the blue case.
Difference Between 0xff and 0xffffff
0xff denotes the hexadecimal value ff which is equivalent to integer 255 (15*16^1+15*16^0) and the binary representation of 255 is 00000000 00000000 00000000 11111111.
Similarly, the representation of 0xffffff is 00000000 11111111 11111111 11111111. It is also equivalent to a 255-integer value that represents the ARGB color.
The bitwise AND operator (or binary AND) "&" is applied to two integers x1 and x2 (for example, x1 & x2). It returns an integer with all its bits equal to 0 except the ones which are equal to 1 in both x1 and x2.
For example, consider the following example.
Hence, (&0xff) permits to keep only the values of the last byte (i.e., the value of the blue component of the color), and ignores all the rest of the bits of other colors.