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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pipe Symbol '|'? 3

Status
Not open for further replies.

Skittle

ISP
Sep 10, 2002
1,528
US
Hi,

I know very little about Java.
Can somebody explain to me the function of a single pipe symbol '|'?

I have found reference to a pair of the symbols representing 'OR' but cannot find a good definition
of a single pipe. At first I thought it represented concatonation but after trying a line in an HTML document I don't think it is.


Example I want to convert to VB :-

cwbRC_SetParm( hPgm,
CWBRC_INOUT | CWBRC_TEXT_CONVERT_OUTPUT,
bufferSize,
buffer );

Example I tested to see what '|' does:-

<SCRIPT LANGUAGE="JavaScript">
alert("test1" | "test2")
</SCRIPT>)

Which produces a message box with '0'in it?

Many thanks.

Dazed and confused
 
the pipe operator does not behave like in Linux. it's a boolean operator and it means OR

actually your alert verifies if "test1" OR "test2" is true
 
>> I know very little about Java.

Java and Javascript are as different as Coke and Pepsi - do not confuse the two.

The double pipe operator in JavaScript will test for the boolean OR condition, so:

alert(3 || 6 || 12); would return 3 (as the first condition, 3, is non-0)

alert(0 || 6 || 12); would return 6 (as the first condition, 3, is 0)

The single pipe operator is used to logically OR boolean values together, so:

alert(3 || 6 || 12); would return 15 (as the boolean values for 3, 6, and 12 ORed together produce 15)

Hope this helps,
Dan
 
Vongrunt

I do seem to recall the phrase 'bitwise' popping up in the API I am trying to call so it may well be 'bitwise'.
Its a phrase I don't understand so any info you can give would be helpful. In fact in the context of the API I think it must be the concept I need to understand.

Dazzled & BillyRayPreacherSon

Thanks for you definitions.

Dazed and confused
 
it means that it will compare bit by bit the two values (will compare the bitwise representation of the values)
 

Back to basics ;o)

Take a nybble (4 bits, half a byte):

Code:
   8 4 2 1
   -------
   0 1 1 0 = 6  (4 + 2)
   1 0 1 0 = 10 (8 + 2)
   =======
   1 1 1 0 = 14 (8 + 4 + 2)

As you can see, bitwise ORing 6 and 10 will give you 14:

alert(6 | 10); // should show 14

Hope this helps,
Dan


 
Ahha!!!

I see.
Many thanks chaps.

Dazed and confused
 
This is probably not what you had seen in your code previously, but another function for the single '|' is the OR logical operator in regular expressions (as opposed to '||'):
Code:
(/^((http)[red]|[/red](https))(:\/\/)/).test(str)
This code would check a string to see if it starts with http:// or https://

-kaht

banghead.gif
 
One special thing to note about the single-bar OR ( | ) vs. the double-bar OR ( || )...

The evaluation of a double-bar OR expression (for example "if(functionOne() || functionTwo())") will stop when the value of the function is KNOWN. In other words, if functionOne() returns TRUE, functionTwo() will not be called since the condition will be true regardless of what functionTwo() returns. However, sometimes you might WANT functionTwo() to be called, even if functionOne() returns TRUE. In that case, the single-bar OR will get you there: if(functionOne() | functionTwo()). Expect similar behavior from '&&' and '&' for AND.

Now, if you specifically DON'T want functionTwo() to be called if functionOne() returns TRUE, then use the double-bar OR.

Also, technically, the evaluation of two arguments joined by a double-bar OR returns TRUE or FALSE while that of the single-bar OR returns 1 or 0 (which in JavaScript, are seen as TRUE or FALSE anyway).

Finally, I think saying that Java and JavaScript are as different as Coke and Pepsi is not exactly true. I'd say more like Coke and Sprite. :)

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top