5

I've been working with Java for Android development for sometime. However, only today did I notice that it is possible to do this:

int myInt = 1|3|4;

As far as I'm aware the variable myInt should only have one integer value. Could someone explain what's going on here?

Thanks!

5
  • 16
    It's a "bitwise or". The result will be 7 (001 | 011 | 100 -> 111).
    – mastov
    Jul 8, 2015 at 9:11
  • 2
  • 1
    I found this because it's used to set an integer property in Android. e.g. input.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); Does that mean that any combination of input properties that take this operation will produce a unique integer? Jul 8, 2015 at 9:17
  • 2
    @publicstaticvoid: It's not necessarily unique because some of those constants can be combinations of bits. But if there are only atomic constants (evaluating to powers of 2) then it will be unique and you can use "bitwise and" to determine which flags were set.
    – mastov
    Jul 8, 2015 at 9:18
  • 1
    Interesting. In the class you can see that sets of non-exclusive constants are powers of two. Jul 8, 2015 at 9:31

1 Answer 1

12

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.

6
  • That's really elegant. Thanks. Jul 9, 2015 at 15:07
  • uhm, in java should be [...] if ( (selectedOptions & FEATURE_1) == FEATURE_1) [...] nope? Jun 17, 2019 at 6:39
  • Hi @AndreaBori no, it's correct as written. To set multiple options, use OR (|) and to test if a single option is set, use &. Feel free to write the code and check the results. Jun 17, 2019 at 12:09
  • 1
    Hi @RichardNeish agreed with that, i was talking about "(" and ")". Feel free to write the code and check the error on IDE. "Operator '&' cannot be applied to 'byte', 'boolean'", just that! Jun 19, 2019 at 18:48
  • @AndreaBori I understand now, thank you for the fix! You can also edit an answer yourself to fix issues like this. Richard Jun 20, 2019 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.