Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AND, OR pipes - single & double useage 2

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
0
0
GB
Hallo.
I remember, way beck when, reading that using a single pipe '|' is different from using a double pipe '||'.
I've been looking around for info on this, but so far am drawing a blank.
I just think it might help optimise code.
Cheers,
Doug.

Common sense is what tells you the world is flat.
 
"|" is a bit-wise operator. It ORs bits (e.g., 1 | 0 = 1.

"||" is the OR operator you see in loops, etc.
Code:
if (Yup == "Yes" || Nope == "YES") . . .
James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
Cheers - but I seem to remember someone saying that the folowing would both be valid:

if(a || b)
if(a | b)

I think they said that the second example would only test b if a was false... Which doesn't make too much sense, at a face value.
I dunno, maybe I'm chasing a red herring...
DJL

Common sense is what tells you the world is flat.
 
if(a || b) tests if either (or both) a OR b is non-zero and if that's the usage it would be equally good to use (a | b) as the result would be the same.
If You however assumes the following:
a = 0x03;
b = 0x20;
X = a | b;
Y = a || b;
would render that X is 0x23 and Y is 1 (true or indeed any non-zero value).

Often the difference does not matter but now and then it REALLY matters and then it's a bummer not to have done it right from the start. Totte
 
I think I follow you - just about ;)
Cheers,
DJL

Common sense is what tells you the world is flat.
 
Logical OR (||) returns a boolean value of true or false, depending on two other boolean operands.

Bitwise OR (|) returns an integral value that is the bitwise OR of its two integral operands.

Essentially, Bitwise OR does a logical OR on every bit of a value.
 
"|" is the bit operator
"||" is the logical OR
I think that cheers said is this case
(a==0?a=1:a=2)
if a is equal to zero, then a =1 else a=2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top