Pipe (|) Operator in Java

Pipe (|) operator in Java

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.

How does the pipe (|) operator work in Android while setting some properties?

Yes, it is a bitwise inclusive OR operation, primarily used for setting flags (documentation). Consider the following flags:

byte flagA = 0b00000001;
byte flagB = 0b00000100;

If we use the | operator, these two flags are combined:

byte flags = flagA | flagB; // = 0b00000101

Which allows us to set properties or other small bits of status information in a small amount of memory (typically an Integer with most Android flags).

Note that a flag should only ever have one bit "active", i.e. have a value equal to 2^n. This is how we know what flags have been set when we go to check the combined flag holder variable using a bitwise AND operator, e.g.

if ((flags & flagA) == flagA) {
// Flag A has been set
...
}

How to use Pipe Symbol through exec in Java

The pipe is a shell feature - you're not using a shell, you're exec'ing a process (ps).

But really, why would you want to do this? What you're saying is:

"execute ps, then pipe its output to another program (grep) and have it extract what I need"

You just need to extract what you want from the output of ps. Use a Matcher and only pay attention to the lines that include java from your InputStream

http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html

What does the pipe character do in a Java method call?

The pipe in 3 | 2 is the bitwise inclusive OR operator, which returns 3 in your case (11 | 10 == 11 in binary).

What does |= mean? (pipe equal operator)

|= reads the same way as +=.

notification.defaults |= Notification.DEFAULT_SOUND;

is the same as

notification.defaults = notification.defaults | Notification.DEFAULT_SOUND;

where | is the bit-wise OR operator.

All operators are referenced here.

A bit-wise operator is used because, as is frequent, those constants enable an int to carry flags.

If you look at those constants, you'll see that they're in powers of two :

public static final int DEFAULT_SOUND = 1;
public static final int DEFAULT_VIBRATE = 2; // is the same than 1<<1 or 10 in binary
public static final int DEFAULT_LIGHTS = 4; // is the same than 1<<2 or 100 in binary

So you can use bit-wise OR to add flags

int myFlags = DEFAULT_SOUND | DEFAULT_VIBRATE; // same as 001 | 010, producing 011

so

myFlags |= DEFAULT_LIGHTS;

simply means we add a flag.

And symmetrically, we test a flag is set using & :

boolean hasVibrate = (DEFAULT_VIBRATE & myFlags) != 0;

Pipe operator in java 7

The | operator is the bitwise-or operator in Java.

The result of a bitwise-or is a value with bits set in it if the corresponding bit was set in either of the operands (or both).

Here, this operation uses the value of JFrame.MAXIMIZED_BOTH (in binary, 0000 0110) to ensure that the second to last and third to last bits are turned on, one for horizontal and one for vertical. This leaves all other bits from f.getExtendedState() intact.

Multiple values separated by a pipe in Java

The | character in Java is a bitwise OR (as mentioned in the comments). This is often used to combine flags, as in the example you gave.

In this case, the individual values are powers of two, which means that only one bit of the value will be 1.

For example, given code like this:

static final int FEATURE_1 = 1;  // Binary 00000001
static final int FEATURE_2 = 2; // Binary 00000010
static final int FEATURE_3 = 4; // Binary 00000100
static final int FEATURE_4 = 8; // Binary 00001000

int selectedOptions = FEATURE_1 | FEATURE_3; // Binary 00000101

then FEATURE_1 and FEATURE_2 are set in the selectedOptions variable.

Then to use the selectedOptions variable later, the application would use the bitwise AND operation & and there would be code like:

if ((selectedOptions & FEATURE_1) == FEATURE_1) {
// Implement feature 1
}
if ((selectedOptions & FEATURE_2) == FEATURE_2) {
// Implement feature 2
}
if ((selectedOptions & FEATURE_3) == FEATURE_3) {
// Implement feature 3
}
if ((selectedOptions & FEATURE_4) == FEATURE_4) {
// Implement feature 4
}

This is a common coding pattern.

Splitting a Java String by the pipe symbol using split(|)

You need

test.split("\\|");

split uses regular expression and in regex | is a metacharacter representing the OR operator. You need to escape that character using \ (written in String as "\\" since \ is also a metacharacter in String literals and require another \ to escape it).

You can also use

test.split(Pattern.quote("|"));

and let Pattern.quote create the escaped version of the regex representing |.



Related Topics



Leave a reply



Submit