9

My question may be basic, but I am wondering how the pipe operator works in the following contexts in Android :

We can set multiple input types in layout:

android:inputType = "textAutoCorrect|textAutoComplete"

We can set multiple flags to an intent as follows:

intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION|Intent.FLAG_ACTIVITY_CLEAR_TOP);

Also we can set some properties as follows:

tvHide.setPaintFlags(tvHide.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

There are multiple instances where we can see such examples in Android.

So my question is, does the | operator act like the bitwise OR operator or does it just concat the results or something else?
If it acts like the bitwise OR operator, then how does it make the expected result correct? Can anybody enlighten me on this?

Boann
  • 48,794
  • 16
  • 117
  • 146
Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
  • yes this is a bitwise OR – Tim Mar 13 '18 at 12:46
  • 1
    Not directly related to the question, but this resource https://code.tutsplus.com/articles/understanding-bitwise-operators--active-11301 may be of use. It shows you different bitwise operators in "table form" so you can see what exactly happens and what the result is. – nbokmans Mar 13 '18 at 12:47
  • Java does not have a pipe operator. – M. Prokhorov Mar 13 '18 at 12:54
  • curious how you got so many upvotes in such a short amount of time for a question to which the answer can be found by googling the title – Tim Mar 13 '18 at 12:56
  • @TimCastelijns Probably because it is a well-asked and interesting question, if not entirely well-researched. – Michael Dodd Mar 13 '18 at 12:57
  • @MichaelDodd considering it was asked at least twice before, it is not that interesting, not to mention the existing questions popped up on the screen while OP was asking this one – Tim Mar 13 '18 at 12:59
  • @TimCastelijns By "interesting" I mean relative to most other questions that pop up in the [android] New Questions queue on a daily basis ;) – Michael Dodd Mar 13 '18 at 13:01
  • `curious how you got so many upvotes in such a short amount of time for a question`. Indeed. Very special. There should be more questions here like that that ask for the obvious to a lot. ;-). – greenapps Mar 13 '18 at 13:55

2 Answers2

6

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
    ...
}
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • 2
    The only thing to add is that for this mechanism to work the flags have to be powers of two so the bits don't overlap. – Juan Mar 13 '18 at 13:03
  • @Juan Good catch, was hoping that would be implicit from the way I wrote that answer out, but yes, each flag should have only have one bit "active" i.e. be 2^n – Michael Dodd Mar 13 '18 at 13:05
  • Yeah,, that the thing,, means How setFlag() know how to use combined results – Ichigo Kurosaki Mar 13 '18 at 13:06
  • @IchigoKurosaki Good to hear that it's helped. If I've answered your question sufficiently, please accept this answer by clicking the ✔ icon on the left. – Michael Dodd Mar 13 '18 at 13:17
  • `How setFlag() know how to use combined results ` setFlags() knows nothing about flags or of combining flags. The compiler adds the flags and then setFlags() gets a byte. Maybe an nteger. – greenapps Mar 13 '18 at 13:57
  • @greenapps Indeed, the function `setFlags()` in `Intent` just assigns the value to a variable called `mFlags` and returns the `Intent` instance. – Michael Dodd Mar 13 '18 at 14:22
  • It can also be applied to `int`, so it's not always for `byte`. for example `final int flag = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE` – beginner Jun 10 '22 at 05:28
1

The pipe in java is the bitwise OR.
When using it in some Android properties, it conceptually does the same thing.
This is that the options separated by the pipe are added.

Juan
  • 5,525
  • 2
  • 15
  • 26