Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

A typical curly brace programming lang has two types of AND and OR: logical and bitwise. && and || for logical ops and & and | for bitwise ops. Logical ops are more commonly used than bitwise ops, why logical ops are longer to type? Do you think they should be switched?

share|improve this question
    
What is perhaps a more interesting question is why have two operators at all now? In C#, for example, & or && will both work equally well on Boolean type arguments. The main distinction is that && is short-circuited. If the bool type treats & as logical and, then why have both operators? –  CodexArcanum Mar 13 '12 at 18:20
add comment

6 Answers 6

up vote 4 down vote accepted

Probably a legacy thing. Bitwise operations may not be very common nowadays, but when coding on very low level you use them all the time. So when C was deviced in the 70's or whenever it was created, bitwise OPs were probably more common than logical OPs. And since C has it that way, I take it many other languages (such as Java, indirectly from C++) followed the same convention.

Now that I've used the double notation (&& and ||) for so long, I'd only be confused if they were switched. But for completely new programmers, it would probably be a nice thing: you save a whopping 50% per logical operation! Imagine the productivity! ^^

EDIT: Provided these changed were done in a new language, of course, not in any existing languages.

share|improve this answer
    
Originally, C didn't have logical operations. Boolean operations were done with bitwise. The logical operators were added later, although still very early in the language's development. –  David Thornley Nov 16 '10 at 21:09
add comment

Answering the last part of your question: Do you think they should be switched? I have to assume that because you didn't ask "if I'm creating a new language..." that means for existing languages.

NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO

If this was switched in an existing language, I wouldn't even want to begin to think of the bugs that would occur... it would mean every line of every app that is ported needs to be looked at explicitly by a developer.

IF you are building a language want do get rid of && and ||, then please use completely different symbols for bitwise and don't switch them. Even still, I'd leave it alone or use difft symbols altogether for all of them. Some of us need to go between languages ;)

share|improve this answer
add comment

I think that's because processors don't have boolean values (thus, no notion of logical operations), so in early implementations bitwise operator may have been implemented before logical operators.

share|improve this answer
add comment

Bitwise op's were around before high-level languages, i.e. they are part of most CPU's basic instruction sets (as AND or OR instructions on registers or memory). & and | just became shorthand for them in C and some other high-level languages.

There is a sequence point after the logical operators && and || (i.e., the left operand is completely evaluated before the right operand). They are not inherently part of the CPU's instruction set. So I think it is appropriate they have the double symbol.

share|improve this answer
add comment

All the discussion of which one came first is besides the point. They are completely different operations. Just because the language does not have a named Boolean does not mean the designers didn't get the concept. '&' is for operating on bits, '&&' is for operating on true/false states ( where true is defined as non-zero). There is a good reason that 1&2 != 1&&2.

share|improve this answer
add comment

If designing a language from scratch nowadays and without any legacy baggage to worry about then, yes, it would probably make more sense to use & and | for logical operations and && and | for bitwise operations. But trying to switch them now for existing languages would be crazy (as it would break all existing software) and it would probably be too confusing to use them for new languages since existing programmers have gotten used to their current meaning.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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