Java | operator with integers;

java

Solution

The `|` operator calculates the "bit-wise OR" of its operands. To understand it you have to convert the operands to binary: it produces a "0" bit if the bit is not set in either numbers, and a "1" bit if it is set in either.

With your numbers, the result of `4|1` is 5 because:

  4 = 100
  1 = 001
4|1 = 101 = 5

The bit-wise OR operator is related to the "bit-wise AND" operator `&`, which produces a "0" if the bit is not set in one of the numbers and a "1" bit if it is set in both.

Since these operators work on the bit-wise representation of their arguments they can be hard to understand when you're used to working on decimal (base 10) numbers. The following relation holds, which makes it easy to derive the result of one when you have the other:

a + b = (a|b) + (a&b)

Problem

I do program with Java for about one year, but still found something I do not know. How does: ``` new Font(FontFamily.TIMES_ROMAN, 12, 1 | 4); ``` How | does work with integers? Thank You P.S. I googled a lot.

Original source

Related problems