0

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.

DwB
  • 37,124
  • 11
  • 56
  • 82
Jezer Crespo
  • 2,152
  • 3
  • 24
  • 27

5 Answers5

3

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
3

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.

Tom McClure
  • 6,699
  • 1
  • 21
  • 21
  • Even better explanation for including on how exactly to read the values from them. Perhaps add a line to reference back to the actual constants requested. – Bart Enkelaar Dec 16 '13 at 17:37
  • Does the 0010 and 0100 added up to make 6? why use the term "or". I'm confused, I thought it's somehow comparing two bits. – Jezer Crespo Dec 16 '13 at 17:41
  • if the inputs are all distinct powers of 2, then the result is the same as addition, but the actual operation here is to set the "on" bit of the answer if the first input *OR* the second input has that bit "on" (ie 1). for example, 6 | 4 == 6 (not ten) because 0110 | 0100 == 0110 – Tom McClure Dec 16 '13 at 18:16
  • Very good of you pointing out that gotcha with the distinct powers of 2. – user1048839 Jan 23 '15 at 11:34
1

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.

Sitansu
  • 3,225
  • 8
  • 34
  • 61
Bart Enkelaar
  • 695
  • 9
  • 21
1

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

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.

dimoniy
  • 5,820
  • 2
  • 24
  • 22