Java 中的管道 (|) 运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3312611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:59:03  来源:igfitidea点击:

Pipe (|) operator in Java

javaoperators

提问by bentham

I've got this statement in Java:

我在 Java 中有这样的声明:

System.out.println(3|4); 

Why is the output 7?

为什么输出是7?

采纳答案by Jonathon Faust

It's a bitwise OR operation. It's modifying things at a binary level.

这是一个按位或运算。它正在二进制级别修改事物。

             011                     3
in binary: | 100     in decimal:  |  4
             ___                   ___
             111                     7

Open Windows calc using scientific mode. You can flip between decimal and binary (and hex) and perform bitwise operations including or, and, xor, etc.

使用科学模式打开 Windows calc。您可以在十进制和二进制(和十六进制)之间切换,并执行按位运算,包括或、和、异或等。

To do a bitwise or in your head or on paper, compare each digit of the same ordinal. If either number is a 1, the result at that ordinal will be 1.

要按位或在头脑中或在纸上进行,请比较相同序数的每个数字。如果任一数字为 1,则该序号的结果将为 1。

回答by dcp

It's doing a bitwise ORoperation, and 3 OR4 is 7.

它在做一个bitwise OR操作,3 OR4 是 7。

See here: http://en.wikipedia.org/wiki/Bitwise_OR#OR

见这里:http: //en.wikipedia.org/wiki/Bitwise_OR#OR

回答by Mike Daniels

The operator |does a "bitwise OR". The output of bitwise OR on two bits is 1 if either bit is 1 or 0 if both bits are 0. Bitwise OR on two numbers just does a bitwise OR on each bit individually.

运算符|执行“按位或”。如果任一位为 1,则两位的按位 OR 的输出为 1,如果两位均为 0,则为 0。两个数字上的按位 OR 仅对每个位分别进行按位或运算。

Heres how 3|4works:

这是如何3|4工作的:

  3:  00000011
  4:  00000100
--------------
3|4:  00000111 = 7

回答by Mike Daniels

| is the "bitwise or" operator. in a|b, if nth bit of a and/or b is 1, the nth bit of the result will be 1. 3 is 11 in binary. 4 is 100 in binary.

| 是“按位或”运算符。在 a|b 中,如果 a 和/或 b 的第 n 位为 1,则结果的第 n 位将为 1。3 是二进制的 11。4 是二进制的 100。

0  1  1
or or or
1  0  0
=  =  =
1  1  1

And 111 happens to be the binary representation of 7.

而 111 恰好是 7 的二进制表示。

回答by davbryn

Binary representation:

二进制表示:

 3 = 00000011
 4 = 00000100

| is bitwise OR operator

when you OR two numbers, you take the binary representation and the OR result is 1 IFF for that column at least one column is set true (1)

当您对两个数字进行 OR 运算时,您采用二进制表示形式,并且该列的 OR 结果为 1 IFF 至少有一列设置为 true (1)

So

所以

00000011
00000100
--------
00000111

then, columns tell you the value at that position:

然后,列会告诉您该位置的值:

128, 64, 32, 16, 8, 4, 2, 1

so

所以

128, 64, 32, 16, 8, 4, 2, 1
 0 ,  0,  0,  0, 0, 1, 1, 1  

any column with a 1 means you add that column's value:

任何带有 1 的列表示您添加该列的值:

4 + 2 + 1 = 7

回答by Jimmy Hoffa

As bitwise operators can be a little confusing without something to correlate them to, the way I've explained their function to non-programmers even is that you simply subtitute 1 for true and 0 for false, and then they behave identically to the operators in the english language:

由于按位运算符可能会有点混乱,没有将它们关联起来,我向非程序员解释它们的功能的方式甚至是你简单地用 1 代替真,用 0 代替假,然后它们的行为与 中的运算符相同英语这门语言:

the moon is blue AND the sky is blue, is false

月亮是蓝色的,天空是蓝色的,是假的

0 and 1 is 0

0 和 1 是 0

the moon is blue OR the sky is blue, is true

月亮是蓝色的或天空是蓝色的,这是真的

0 or 1 is 1

0 或 1 是 1

but the analogy breaks down when I get to:

但是当我到达时,这个类比就不成立了:

the ocean is blue XOR the trees are green, is false

海洋是蓝色的异或树是绿色的,是假的

回答by Aaron Anodide

It's useful to realize there is a generalized system for counting underlying this. Binary is base-2. Familiar decimal is base-10. Linux permission octal is base 8.

意识到在此基础上有一个通用的计数系统是很有用的。二进制是基数为 2。熟悉的十进制是基数为 10。Linux 权限八进制是基数 8。

A number's value is obtained by adding together the individual values of each of its digits. For any digit, the value is derived from a simple formula.

数字的值是通过将每个数字的各个值加在一起而获得的。对于任何数字,该值都来自一个简单的公式。

(digit) * (base) ^ (number of places to the left of the decimal point)

(digit) * (base) ^ (小数点左边的位数)

123 = one hundred and twenty three = (1 * 10^2) + (2 * 10^1) + (3 * 10^0) = 100 + 20 + 3

123 = 123 = (1 * 10^2) + (2 * 10^1) + (3 * 10^0) = 100 + 20 + 3

I learned that in CS211 (not bragging, just remembering)

我在 CS211 中了解到(不是吹牛,只是记住)