INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Double Bar "||" bitwise operator?

Double Bar "||" bitwise operator?

(OP)
I am doing code archeology (digging up someone else's code) and I see the use of two vertical bars in what appears to be a bitwise operator. Can't find any explanation of this on the web and my experiemt below doesn't really explain it all. The code I dug up looks like this:

CODE --> javascript

answer = (ScriptThatMightReturnTRUE) || (ScriptThatReturnsString); 

My experiments:

CODE --> javascript

<!DOCTYPE html>
<html>
<body>
<p>difference between a single vs double "|"?.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
    document.getElementById("demo").innerHTML = true | 5;}  // prints 5
</script>
</body>
</html> 


CODE --> javascript

document.getElementById("demo").innerHTML = false | true;  // prints 1

document.getElementById("demo").innerHTML = "fred" | false; // prints 0

document.getElementById("demo").innerHTML = "fred" | true; // prints 1

document.getElementById("demo").innerHTML = true || 5;   // prints true

document.getElementById("demo").innerHTML = 5 || true ;   // prints 5

document.getElementById("demo").innerHTML = false || true ;  // prints true

document.getElementById("demo").innerHTML = true || false ;  // prints true 

RE: Double Bar "||" bitwise operator?

Double pipe is logical OR
Single pipe is bitwise OR

RE: Double Bar "||" bitwise operator?

(OP)
Right, but in this example what ends up in the var answer? If left operand is true, answer is assigned boolean true? If left operand is false the string from the right operand is assigned to answer?

CODE --> javascript

answer = (ScriptThatMightReturnTRUE) || (ScriptThatReturnsString); 

RE: Double Bar "||" bitwise operator?

(OP)
I found an answer: the right hand operand is evaluated only if the left one is false. So if ScriptThatMightReturnTRUE returns false then the value returned by ScriptThatReturnsString will be get assigned to answer.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close