Does bitwise inclusive OR with assignment operator have advantages over typical assignment in java?

70 Views Asked by At

When looking at some code on one or another git, sometimes I can see that devs use bitwise inclusive OR compound assignment operator (|=) where simple assignment would be enough. Unfortunately, I don't have any code with this solution at hand, so I'll try to describe it as best I can.

Let's say, we have the following code in java:

boolean found = false;
for (String s : stringSet) {
    if (s == null || s.equals("")) {
        found |= true; // <== this line
    }
}
if (!found) {
    System.out.println("No interesting string found");
}

I ask only about the pointed line. Why people do it this way? I understand that we can have a really great amount of data, billions or trillions to iterate over. But does the pointed line changes the efficiency so dramatically? Would it be noticeably slower for a lot of data, if I change it to simple assignment: found = true;?

I don't exclude the possibility that not a speed is the main argument, but it seemed most meaningful to me.

And yes, I know this code can be converted to method or streams, but it's only a simplification of a code where it would be far more complicated etc. We can assume that before the pointed line (or even before if), there are tons of code that do something meaningful. Please, don't suggest something like "use streams instead", because I know how to java advanced already. I'd like to understand the phenomenon of this somehow enigmatic solution using bitwise inclusive OR.

0

There are 0 best solutions below