4

Rarely I bump into & and | logic operators in other's codes instead of || and &&. I made a quick research because I never used them and didn't know what is it for.
A && B means if A is false, B won't be evaluated and it will return false.
A & B means if A is false, B will be evaluated even if the form will return false as well.
Of course it is the same game with | and ||.

My question is: does it make sense anyways to evaluate the second member if the first determine the evaluation? I can imagine a code where the second member does something super important logic in clode and so it should be evaluated, but I suspect it is bad practice. Is it enough to make | and & exist?

from a comment, to make it more clear: I have the feeling that the program is not optimal and working well or better to say: designed well if all of the logical members HAS to be evaluated.

CsBalazsHungary
  • 803
  • 14
  • 30

5 Answers5

6

You forgot about bitwise operations

The bitwise & operator performs a bitwise AND operation.

The bitwise | operator performs a bitwise inclusive OR operation.

But here you can find example when unconditional AND operator is better:

What are the cases in which it is better to use unconditional AND (& instead of &&)

Community
  • 1
  • 1
bsiamionau
  • 8,099
  • 4
  • 46
  • 73
  • Thanks, I never used at this level, and the misunderstanding comes from some code of my current project, where people used it as "a!=null & blabla(a)" It made no sense for me. – CsBalazsHungary Feb 26 '13 at 13:09
5

The single & and | are not logical operators, they're bitwise and logical operators.

ddmps
  • 4,350
  • 1
  • 19
  • 34
  • 1
    They are bitwise and logical operators - [spec](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22) – McDowell Feb 26 '13 at 14:04
2

You can use the | and & operators as you said above. Some static code analyzers will warn you because it might mask a possible problem. In those cases where you really want both conditions evaluated, you can do something like:

boolean cond1=cond1();
boolean cond2=cond2();
boolean myCond=cond1 && cond2;

if you just use

boolean myCond=cond1()&cond2();

someone is bound to "correct" it for you. So yes, there are places where you would use & and |, but most likely it's not a good idea and you can get around it for the sake of clarity.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
  • yes this was my only guess when I posted my question. But I have the feeling that the program is not optimal and working well (better to say designed well) if all of the logical members HAS to be evaluated. – CsBalazsHungary Feb 26 '13 at 13:13
2

Imagine if B was a function that you wanted to execute all the time then it would be useful.

Hassan Voyeau
  • 3,383
  • 4
  • 22
  • 24
  • yes, but as I wrote in the edited question: wouldn't that be a sign that the program is not structured well? I would say it is pretty common if somebody changes some logical evaluation, and this can be easily a victim of that. – CsBalazsHungary Feb 26 '13 at 13:18
2

I think that might be quite good example:

if (initializeConnectionA() & initializeConnectionB() & initializeConnectionC()) {
    performOperation();
} else {
    logger.warn("Not all modules are working properly");
}

Where methods initializeConnection connects to i.e. some external servers that might not all be working. You might not require to initialize them all but you want to be warned if some of them are not working.

Of course it might not be the clearest solution for this problem, but this is example where & operator might be useful.

  • for me it is a little bit smelly even if the example is really good. I mean if you keep using this single & operator, there will surely be a new guy who has to debug the function and surely will make bug by rewrite them to &&. For my taste, I would clearly separate the initialization, and make a separate checker for status. So no vital program part will be missed if somebody touches this part of the program. – CsBalazsHungary Feb 26 '13 at 13:44
  • I totally agree, I wouldn't write it that way in my production code. – Marcin Pieciukiewicz Feb 26 '13 at 14:01
  • I think that | and & operators are less usefull than || and && operators but are more basic then the latter ones, which are optimized version of them. And I think that programming language should give us basic operators and if possible give us additional optimized operators. – Marcin Pieciukiewicz Feb 26 '13 at 14:05
  • right, just because it is basic, I wouldn't say it is good. I could get use to write separate if for 'a != null' and 'a.getId() != 0', but it is so... old school. – CsBalazsHungary Feb 26 '13 at 14:07