**Get started with Spring 5 and Spring Boot 2, through the ***Learn Spring* course:

*Learn Spring*course:

Last modified: June 27, 2022

**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.

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**.

**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

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

Let's see an example to explain some of the benefits of using *& 0xff* in more detail.

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);
```

**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.

Follow the Java Category

Follow the Java category to get regular info about the new articles and tutorials we publish here.