Java Programming: & vs &&

Status
Not open for further replies.
I was in my computer science class today, and the prof was talking about logical operations when he came about using the & and && operators. He said that we should only use && in cases where we specifically do not want the right hand operand to be evaluated, and use & all other times. In high school I was taught to do just the opposite, since && would be quicker in some cases. Makes sense to me to use && most of the time.

So, what is the proper usage? && most of the time, & in certain occasions, or vice versa? Is my prof a bad programmer, or did high school lead me down the wrong path? Does it really matter? Help!

BTW, this also applies to | and ||.
 

Ruziel

Ars Praetorian
573
Subscriptor++
Umm.. that's not right... in Java (and C/C++ if I remember correctly) the single "|" and "&" are bit-wise operators, not logical operators, they are used for completely different things.

You use the "&&" and "||" for boolean statements:

true && true = true
true && falase = false
false && false = false

true || true = true
true || false = true
false || false = false

You use the "&" and "|" for bit operations, given the following are binary numbers:

1010 & 0101 = 0000
1010 & 1000 = 1000

1010 | 0101 = 1111
1010 | 0000 = 1111
 
Umm.. that's not right... in Java (and C/C++ if I remember correctly) the single "|" and "&" are bit-wise operators,
Since false == 0 and true == 1, bitwise and logical operators *probably* perform the same task 99% of the time (except for shortcut evaluation).

1 | 0 == 1 just like
true || false == true

That being said, your teacher is definitely a bad programmer. Using | and & so that both sides get evaluated is just PLAIN STUPID!!! Write better code that doesn't required both sides to evaluate instead.

&& and || are what you should use for logical expressions.
 
Are you guys sure about that?

From what I know, the difference is...
if(false & "joon") -> error
if(false && "joon") -> false
("joon" is not a proper boolean expression)

As in, if the second part of the condition is an illegal command, the "&&" operator will simply ignore it.

[EDIT: Oh... prodigy answered his own question, I guess]

-Huh

[This message was edited by Joon H. Huh on October 30, 2001 at 19:14.]
 

MrMills

Ars Praefectus
3,727
Moderator
Your prof is a bad programmer.

Short-circuit Boolean evaluation is a GOOD THING. It allows you to optimize your expressions to run in maximum time and also allows you to write expressions like

if((p != NULL) && (p->next != NULL)) doSomething();

Note that in that case, evaluating the second expression would cause an access violation if p was NULL.

The problem with using & as a Boolean operator is that True is usually, but not always, 1. So:

1001 &
0110 =
0000

Whoops! True and true equal false! Not good.

If you seriously want to always evaluate both sides, most compilers offer a "complete Boolean evaluation" option. Or you can assign partial results to two variables and then && those together:

b0 = p == NULL;
b1 = iterations < 21;
if(b0 && b1) ...

Anyway, you should always use short-circuit logic, and even purposely structure it to optimize speed. If you've got a bunch of stuff and'ed together, put the expression that's most likely to be false first. If your expressions are or'ed, then put the one most likely to be true first.
 
Everything said so far in this thread has been totally correct. And other people said what I was saying while I wrote it. Oh, well.

code:<HR>
Code:
&: bitwise and
&&: logical and

What's the difference? As long as you're operating on booleans, the only difference is that && won't always evaluate the right-hand argument. Working with integers, though, can generate some funny results. (This won't work in Java, but try it in C++).

code:<HR>
Code:
#include <iostream><P>int main(int argc, char* argv[])
{
        if (1 & 2)
                cout << "hi\n";
        if (1 && 2)
                cout << "hello\n";
        return 0;
}

Try running it; you'll just get the "hello", not the "hi". Because of Java's more restrictive type system, it's somewhat more safe in Java, but it's still really a bad idea. Your prof's on crack; write your code so that you use logical operators on boolean values, and bitwise operators only when you actually want to directly manipulate the bits.

floccinau
 

wb

Ars Legatus Legionis
25,208
Subscriptor
Since false == 0 and true == 1

However, this is not necessarily correct. For the anal retentive, this is how true and false are properly represented:

false == 0
true = !false

Which means that when programming, you can always count on false being zero, but you CANNOT count on true being equal to one. The only thing you know for sure is that true != 0.
 

OneFootsTheSame

Smack-Fu Master, in training
70
wb wrote:
For the anal retentive, this is how true and false are properly represented

false == 0
true = !false

And for the truely anal, in Java booleans are a primitive type, are either "true" or "false", and only booleans can be compared. So the statement "false == 0" is incorrect syntactically and also because a specific VM could use 0 for true and still correctly implement the language.
 
Status
Not open for further replies.