在Java中,”&” 的作用是什么?

huangapple go评论124阅读模式
英文:

What's the purpose of & in Java?

问题

这段代码中,& 在这个上下文中用于按位与运算。

英文:

So, I was following a pacman tutorial and the coder uses & not &&. He doesn't explain what it does though. I searched it up and it says that unlike &&, & checks both statements. That makes sense. But the coder doesn't use it in a comparison context. This is his code.

screenData[] is an 255 element int array.

int ch = 0, pos = 0;
if((ch & 16) != 0) {
      screenData[pos] = (int) (ch & 15);
	}

Please tell me why he did that and what's the use of & in this context.

答案1

得分: 5

& 进行位与运算

因此,ch & 15 检查前四个最低位中是否有任何位被设置(因为15在二进制中是1111)。该操作的结果是一个所有其他位都设置为0的数字,只有那些在ch中也设置为1的四个低位位被设置为1。

英文:

& performs bitwise “and”.

Thus, ch & 15 checks whether any of the first four lowest bits are set (because 15 is 1111 in binary). The result of the operation is a number with all other bits set to 0, and only those of the four low-level bits set to 1 which were also set to 1 in ch.

答案2

得分: 3

The & operator used in the code is the bitwise AND operator. This operator checks the corresponding bits of the two operands at the bit level and generates its result.

In the code snippet, the second bit (corresponding to 16) of the variable ch is checked using the ch & 16 expression. If the second bit is 1 (not 0), the last 4 bits of ch (the corresponding bits 15) are assigned to the screenData[pos] array.
This usage is used to control certain bits of ch and act accordingly.

For example, the expression ch & 16 is used to check the second bit of the variable ch, while the expression ch & 15 is used to get the last 4 bits of ch.
Hence, the & operator is used to control or manipulate certain bits in bitwise operations.

英文:

The & operator used in the code is the bitwise AND operator. This operator checks the corresponding bits of the two operands at the bit level and generates its result.

In the code snippet, the second bit (corresponding to 16) of the variable ch is checked using the ch & 16 expression. If the second bit is 1 (not 0), the last 4 bits of ch (the corresponding bits 15) are assigned to the screenData[pos] array.
This usage is used to control certain bits of ch and act accordingly.

For example, the expression ch & 16 is used to check the second bit of the variable ch, while the expression ch & 15 is used to get the last 4 bits of ch.
Hence, the & operator is used to control or manipulate certain bits in bitwise operations.

答案3

得分: 1

在Java中,& 有两个用途:

  • 最常见的用途是与其他位操作运算符(例如~,|,^)结合使用,进行位掩码操作,用于检查特定位或屏蔽特定位。
  • 另一个用途是进行逻辑比较。
    • && 用于短路条件
    • & 用于非短路条件

位掩码等

int v = 0xFF;
v = v & 0xF0; //现在 v = 0xF0

在条件语句中

int v = 10;
int k = 1;
if (v == 11 && k++ == 4) {
   // 做一些操作
}
System.out.println(k); // 输出 1

由于 v 为 false,没有理由检查 k++ == 4,因此 k 不会增加。 换句话说,它有效地短路了评估。 这是因为当一系列 && 分隔各种逻辑测试时,如果一个失败,整个表达式被评估为 false。 与 ||(或运算符)不同,如果所有表达式都是 OR 并且除了一个之外都评估为 false,则表达式为 true

int v = 10;
int k = 1;
if (v == 11 & k++ == 4) {
   // 做一些操作
}
System.out.println(k); // 输出 2

使用单个 &,无论第一个条件的结果如何,甚至无论条件语句的第二部分如何,都会测试 k。 据我看来,这种用法较少,可能会引发难以找到的错误。

在你的示例中,以下是正在发生的事情。

int ch = 0, pos = 0;
if((ch & 16) != 0) { //16 在二进制中是10000,所以它检查该位是否已设置,如果“不等于0”,则已设置,条件成功

      screenData[pos] = (int) (ch & 15); // 15 在二进制中是1111,因此它掩盖了 ch 的高阶位,保留了 ch 的低四位,并将结果分配给数组。

如果 ch 起始值不为 0,上述示例将更有趣。

英文:

>What's the purpose of & in Java?

There are two uses for the & (AND) operator

  • the most common is as a bit masking operation in combination with other bit manipulation operators (e.g ~, |, ^) to check for certain bits or to mask off certain bits.
  • the other is for logical comparisons.
    • && for short circuiting conditionals
    • & for non-short circuiting conditionals

Bit Masking, etc

int v = 0xFF;
v = v & 0xF0; //now v = 0xF0

In Conditionals

int v = 10;
int k = 1;
if (v == 11 && k++ == 4) {
   // do something
}
System.out.println(k); // prints 1

since v is false, there was no reason to check if k++ == 4 so k is not incremented. In other words it efficiently short circuits the evaluation. This is because, when a series of && separates various logical tests, if one fails, the whole expression is evaluated as false. Unlike the || (OR operator), if a all the expressions are OR's and evaluate false except for one, the expression is true.

int v = 10;
int k = 1;
if (v == 11 & k++ == 4) {
   // do something
}
System.out.println(k); // prints 2

With a single & k is tested regardless of the outcome of the first or even the second part of the conditional. This is, imho, less used and can induce bugs that are hard to find.

In your example, here is what is going on.

int ch = 0, pos = 0;
if((ch & 16) != 0) { //16 is 10000 in binary so it checks to see if that 
                     //bit is set, if `not ==  0`, it is set, so the 
                     //conditional succeeds
                
      screenData[pos] = (int) (ch & 15); // 15 is 1111 in binary to it 
                                         // masks off the high order bits
                                         // keeping the low   
                                         // order four bits of ch and 
                                         // assigns the result to the array.

The above example would be more interesting if ch wasn't 0 to start.

huangapple
  • 本文由 发表于 2023年6月12日 03:36:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定