Consider this following snippet:
input.setInputType(
InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
What does the |
do? I have never encountered that before in java.
Consider this following snippet:
input.setInputType(
InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
What does the |
do? I have never encountered that before in java.
It's a bitwise Or, if you have binary values it or's each of them together
00010110
10110000
---------
10110110
For each bit, the output will have a 0
, if both of the corresponding input's bits have a 0
. If either or both input's bits have a 1
, it will output 1
in that slot.
It's kinda like an ||
, except it checks on each individual bit, instead of the whole number.
In your case
The way your particular example what you're doing is encoding 2 conditions into a single value, has to do with masking.
Suppose InputType.TYPE_CLASS_TEXT
was equal to 8
or 00001000
And suppose InputType.TYPE_TEXT_VARIATION_PASSWORD
was equal to 32
or 00100000
InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
would be equal to 00101000
or 40
.
When you want to read them, you use bitwise anding. It's the exact opposite of oring. IT returns true only if both values are 1.
00010110
10110000
---------
00010000
To check if your value contains a result, you simply and
it to the type
00101000 - 40 - Your result
00001000 - 8 - InputType.TYPE_CLASS_TEXT
--------
00001000 - 8 - this means that your result contains `InputType.TYPE_CLASS_TEXT`
in code, the check might look like this.
if(myInputType & InputType.TYPE_CLASS_TEXT != 0)
//myinput type cointains TYPE_CLASS_TEXT
Let's say you have some integer constants, W=1,X=2,Y=4,Z=8
The bit representations of these are: 0001 0010 0100 1000
You can "or" them together to request any combination of W/X/Y/Z and pass them as a single argument. Let's say I want features X and Y but not W and Z:
0010 | 0100 == 0110 (6)
So I call the function with (X | Y) and the function can bitwise-and the argument with the individual options (and test if != 0) to see which ones were selected:
0110 & W == 0000 == 0
0110 & X == 0010 != 0
0110 & Y == 0100 != 0
0110 & Z == 0000 == 0
Notice how I carefully selected the inputs to be all distinct powers of 2. This means the binary representation of each feature will always have just one unique bit set to "on" - which is key for making the whole flag-passing system work.
It's a bitwise OR operator. It's used to combine flag values into a single integer value that bitwise contains the true/false values of all the different flags.
It's basically to use a single integer as a list of true/false values.
In this case, imagine that InputType.TYPE_CLASS_TEXT
has a value of 2 (or 10 in binary) and InputType.TYPE_TEXT_VARIATION_PASSWORD
has a value of 4 ( or 100 in binary ).
The bitwise OR then combines 10 and 100 into 110, or 6. From the value 6 the calling code can then uniquely determine that both the flags TYPE_CLASS_TEXT
and TYPE_TEXT_VARIATION_PASSWORD
were passed to it.
This is a bitwise or operator.
It's quite useful when you have a bunch of boolean flags you want to pass to a method to represent them as a single int
, and "light" options by bitwise-oring between them.
In this case:
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
You'd be passing InputType.TYPE_CLASS_TEXT
(=1) bitwise-or InputType.TYPE_TEXT_VARIATION_PASSWORD
(=128), for a total of 129.
Like it's already been mentioned, it's a bitwise OR operator. The idea is to save some memory and to store multiple flags within a single int.