473,466 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Binary OR

I'm a bit baffled by this:

Console.WriteLine(true | false);

Surely that's binary, not logical, OR... right?

And then there's this:

Console.WriteLine(true || false);
Nov 15 '05 #1
2 1669
C# Learner,
I'm a bit baffled by this:

Console.WriteLine(true | false);

Surely that's binary, not logical, OR... right?

And then there's this:

Console.WriteLine(true || false);


From the MSDN help:

----------------------------------------------------------------------------
--

Binary | operators are predefined for the integral types and bool. For
integral types, | computes the bitwise OR of its operands. For bool
operands, | computes the logical OR of its operands; that is, the result is
false if and only if both its operands are false.

expr1 | expr2

Where: expr1 -- An expression.
expr2 -- An expression.

// cs_operator_OR.cs
using System;
class Test
{
public static void Main()
{
Console.WriteLine(true | false); // logical or
Console.WriteLine(false | false); // logical or
Console.WriteLine("0x{0:x}", 0xf8 | 0x3f); // bitwise or
}
}

Output:

True
False
0xff
----------------------------------------------------------------------------
--

and

----------------------------------------------------------------------------
--
The conditional-OR operator (||) performs a logical-OR of its bool operands,
but only evaluates its second operand if necessary.

expr1 || expr2

Where: expr1 -- An expression.
expr2 -- An expression.

// cs_operator_short_circuit_OR.cs
using System;
class Test
{
static bool fn1()
{
Console.WriteLine("fn1 called");
return true;
}

static bool fn2()
{
Console.WriteLine("fn2 called");
return false;
}

public static void Main()
{
Console.WriteLine("regular OR:");
Console.WriteLine("result is {0}", fn1() | fn2());
Console.WriteLine("short-circuit OR:");
Console.WriteLine("result is {0}", fn1() || fn2());
}
}
Output:

regular OR:
fn1 called
fn2 called
result is True

short-circuit OR:
fn1 called
result is True
-------------------------------------------------------------------

I hope that helps clear things up and doesn't get me into too much trouble
with the copyright people.

Regards,

Randy
Nov 15 '05 #2
"Randy A. Ynchausti" <ra*************@msn.com> wrote:
C# Learner,
I'm a bit baffled by this:

Console.WriteLine(true | false);

Surely that's binary, not logical, OR... right?

And then there's this:

Console.WriteLine(true || false);


From the MSDN help:


<snip>

Ah, so short-circuit evaluation is the difference.

P.S.: I did look on MSDN initially but didn't find this page.

Thanks.
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
20
by: Christian Stigen Larsen | last post by:
A signed int reserves one bit to signify whether a number is positive or negative. In light of this, a colleague asked me whether there existed an int in C++ that was -0, a zero with the negative...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
5
by: bwv539 | last post by:
I have to output data into a binary file, that will contain data coming from a four channel measurement instrument. Since those data have to be read from another C program somewhere else, the...
10
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
16
by: Erwin Moller | last post by:
Why is a binary file executable? Is any binary file executable? Is only binary file executable? Are all executable files binary? What is the connection between the attribute of binary and that of...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.