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

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

We can set several input types in the layout:

android:inputType = "textAutoCorrect|textAutoComplete" 

We can set several flags in an intent as follows:

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

We can also set some properties as follows:

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

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

So my question is: does the operator work | like a bitwise OR operator or just a concatenated result or something else?
If it acts as a bitwise OR operator, then how does it make the expected result correct? Can anyone enlighten me on this?

+5
source share
2 answers

Yes, this is a bitwise inclusive OR operation, mainly used to set flags ( documentation ). Consider the following flags:

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

If we use the operator | The two flags are combined:

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

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

Please note that the flag should have only one β€œactive” bit, i.e. have a value equal to 2 ^ n. This is how we know which flags were set when we go to check the holder variable of the union flag using the bitwise AND operator, for example.

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

A pipe in java is a bitwise OR.
When used in some Android properties, it conceptually does the same.
This means that options are separated by a channel.

0
source

Source: https://habr.com/ru/post/1275865/


All Articles