1. Overview
0xff is a number represented in the hexadecimal numeral system (base 16). It's composed of two F numbers in hex. As we know, F in hex is equivalent to 1111 in the binary numeral system. So, 0xff in binary is 11111111.
In this article, we'll discover how to use the 0xff value. In addition, we'll see how to represent it using multiple data types and how to use it with the & operator. Finally, we'll review some of the benefits associated with using it.
2. Representing 0xff With Different Data Types
Java allows us to define numbers interpreted as hex (base 16) by using the 0x prefix, followed by an integer literal.
The value 0xff is equivalent to 255 in unsigned decimal, -127 in signed decimal, and 11111111 in binary.
So, if we define an int variable with a value of 0xff, since Java represents integer numbers using 32 bits, the value of 0xff is 255:
int x = 0xff;
assertEquals(255, x);
However, if we define a byte variable with the value 0xff, since Java represents a byte using 8 bits and because a byte is a signed data type, the value of 0xff is -1:
byte y = (byte) 0xff;
assertEquals(-1, y);
As we see, when we define a byte variable with the 0xff value, we need to downcast it to a byte because the range of the byte data type is from -128 to 127.
3. Common Usage of & 0xff Operation
The & operator performs a bitwise AND operation. The output of bitwise AND is 1 if the corresponding bits of two operands is 1. On the other hand, if either bit of the operands is 0, then the result of the corresponding bit is evaluated to 0.
Since 0xff has eight ones in the last 8 bits, it makes it an identity element for the bitwise AND operation. So, if we apply the x & 0xff operation, it will give us the lowest 8 bits from x. Notice that, if the number x is less than 255, it'll still be the same. Otherwise, it'll be the lowest 8 bits from x.
In general, the & 0xff operation provides us with a simple way to extract the lowest 8 bits from a number. We can actually use it to extract any 8 bits we need because we can shift right any of the 8 bits we want to be the lowest bits. Then, we can extract them by applying the & 0xff operation.
Let's see an example to explain some of the benefits of using & 0xff in more detail.
4. Extracting RGBA Color Coordinates Using & 0xff
Let's assume that we have an integer number x, stored in 32 bits, that represents a color in the RGBA system, which means that it has 8 bits for each parameter (R, G, B, and A):
- R = 16 (00010000 in binary)
- G = 57 (00111001 in binary)
- B = 168 (10101000 in binary)
- A = 7 (00000111 in binary)
So, x in binary would be represented as 00010000 00111001 10101000 00000111 — which is the equivalent to 272214023 in decimal.
Now, we have our x value in decimal, and we want to extract the value for each parameter.
As we know, the >> operation shifts bits to the right. Therefore, when we do (10000000 00000000 >> 8), it gives us 10000000. As a result, we can extract the value of each parameter:
int rgba = 272214023;
int r = rgba >> 24 & 0xff;
assertEquals(16, r);
int g = rgba >> 16 & 0xff;
assertEquals(57, g);
int b = rgba >> 8 & 0xff;
assertEquals(168, b);
int a = rgba & 0xff;
assertEquals(7, a);
5. Conclusion
In this tutorial, we've discussed how the & 0xff operation effectively divides a variable in a way that leaves only the value in the last 8 bits and ignores the rest of the bits. As we've seen, this operation is especially helpful when we shift right a variable and need to extract the shifted bits.
As always, the code presented in this article is available over on GitHub.
The post Understanding the & 0xff Value in Java first appeared on Baeldung.