开发者

Pipe (|) operator in Java

开发者 https://www.devze.com 2023-01-08 23:55 出处:网络
I开发者_Go百科\'ve got this statement in Java: System.out.println(3|4); Why is the output 7?It\'s a bitwise OR operation.It\'s modifying things at a binary level.

I开发者_Go百科've got this statement in Java:

System.out.println(3|4); 

Why is the output 7?


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.

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.


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.

Heres how 3|4 works:

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


It's doing a bitwise OR operation, and 3 OR 4 is 7.

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


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)

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:

4 + 2 + 1 = 7


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

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

And 111 happens to be the binary representation of 7.


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.

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)

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

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


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:

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

0 and 1 is 0

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

0 or 1 is 1

but the analogy breaks down when I get to:

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


Since question asks what is pipe operator (|) in Java, without specifying anything particularly about "OR" logic, it may be useful to note, that this operator is so to say - redefined, when we deal with exceptions.

Since Java SE 7, catching multiple, disjointtypes having no inheritance relation exceptions in one catch block, also involves | operator, which, in this case, serves as just piping/chaining logic, and has nothing to do with "OR", because in case of "OR", disjoint would have been allowed.

If, before Java 7, whenever you needed to catch multiple exceptions, you needed to write separate catch blocks, since Java 7, you can do:

try {
   ...
} catch (FileNotFoundException | SQLException e) {
   ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消