1. Overview
1.概述
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.
0xff 是一个用十六进制数字系统(基数16)表示的数字。它是由两个F十六进制数字组成的。我们知道,十六进制的F相当于二进制数字系统中的1111。因此,0xff在二进制中是1111111。
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.
在这篇文章中,我们将发现如何使用0xff值。此外,我们将看到如何使用多种数据类型来表示它,以及如何用&操作符来使用它。最后,我们将回顾与使用它有关的一些好处。
2. Representing 0xff With Different Data Types
2.用不同的数据类型表示0xff
Java allows us to define numbers interpreted as hex (base 16) by using the 0x prefix, followed by an integer literal.
Java允许我们通过使用0x前缀来定义解释为十六进制(base 16)的数字,后面是一个整数字头。
The value 0xff is equivalent to 255 in unsigned decimal, -127 in signed decimal, and 11111111 in binary.
值0xff相当于无符号十进制的255,有符号十进制的-127,以及二进制的1111111。
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变量,其值为0xff,由于Java使用32位表示整数,0xff的值是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变量,其值为0xff,因为Java用8位来表示一个字节,而且字节是一个有符号的数据类型,0xff的值为-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.
正如我们所看到的,当我们用0xff值定义一个byte变量时,我们需要将其下移为byte,因为byte数据类型的范围是从-128到127。
3. Common Usage of & 0xff Operation
3.& 0xff操作的常见用法
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.
&操作符执行bitwise AND操作。如果两个操作数的相应位是1,那么位智和的输出就是1;另一方面,如果操作数的任何一个位是0,那么相应位的结果就被评估为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.
由于0xff的最后8位有8个1,这使得它成为比特和操作的特征元素。因此,如果我们应用x & 0xff操作,它将会给我们来自x的最低8位。注意,如果数字x小于255,它仍然是一样的。否则,它将是x的最低8位。
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.
一般来说,& 0xff操作为我们提供了一个简单的方法来提取一个数字的最低8位。实际上,我们可以用它来提取我们需要的任何8位,因为我们可以将我们希望成为最低位的8位中的任何一位右移。然后,我们可以通过应用& 0xff操作来提取它们。
Let’s see an example to explain some of the benefits of using & 0xff in more detail.
让我们看一个例子来更详细地解释使用& 0xff的一些好处。
4. Extracting RGBA Color Coordinates Using & 0xff
4.使用&0xff提取RGBA颜色坐标
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):
我们假设有一个整数x,存储在32位,它代表RGBA系统中的一种颜色,这意味着它的每个参数(R、G、B和A)都有8位。
- 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.
因此,x在二进制中表示为00010000 00111001 10101000 00000111–相当于十进制中的272214023。。
Now, we have our x value in decimal, and we want to extract the value for each parameter.
现在,我们有了十进制的x值,我们想提取每个参数的值。
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:
正如我们所知,>>操作将比特向右移动。因此,当我们做(10000000 00000000 >> 8)时,它给我们的是10000000。结果是,我们可以提取每个参数的值。
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
5.总结
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.
在本教程中,我们已经讨论了& 0xff操作如何有效地分割一个变量,只留下最后8位的值,而忽略了其余的位。正如我们所看到的,当我们将一个变量右移并需要提取移位后的比特时,这个操作特别有用。
As always, the code presented in this article is available over on GitHub.
一如既往,本文介绍的代码可在GitHub上获得over。