<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FormatException &#187; Operators</title>
	<atom:link href="http://www.formatexception.com/tag/operators/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.formatexception.com</link>
	<description>Ramblings on developing in the Windows World</description>
	<lastBuildDate>Mon, 24 May 2010 15:43:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Often Unused Operators: &#124;= (or equals)</title>
		<link>http://www.formatexception.com/2009/01/often-unused-operators-or-equals/</link>
		<comments>http://www.formatexception.com/2009/01/often-unused-operators-or-equals/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 15:31:34 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Operators]]></category>
		<category><![CDATA[|=]]></category>

		<guid isPermaLink="false">http://www.formatexception.com/?p=174</guid>
		<description><![CDATA[I&#8217;ve debated before the usefulness of the &#124; and &#038; operators.  The use of the &#124; operator depends on the context in which it is used. 
In the case of bools a boolean operation is performed.  In the case of ints a bitwise OR is performed.  
For boolean operations when the &#124; [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve debated before the usefulness of the <a href="http://www.formatexception.com/2008/09/be-be/">| and &#038; operators</a>.  The use of the | operator depends on the context in which it is used. </p>
<p>In the case of bools a boolean operation is performed.  In the case of ints a bitwise OR is performed.  </p>
<p>For boolean operations when the | operator is used both sides of the operator are evaluated.  In the example below both VerifyStatus and SetDefaults are called regardless of the boolean returned by each of them.</p>
<pre name="code" class="csharp">public void TestTransaction(PaymentTransaction Transaction)
{
    if (Transaction != null &#038;&#038;
        (VerifyStatus(Transaction.State) | SetDefaults(Transaction)))
    {
        //do something with the code
    }
}
private bool VerifyStatus(ItemState State)
{
    if (State == null)
        return false;
    if (State.Status == Status.NotVerified)
        ValidateStatus(State);
    if (State.Status == Status.Valid)
        return true;
    //Status.Invalid
    return false;
}
private void ValidateStatus(ItemState State)
{
    //validate the status
}
private bool SetDefaults(PaymentTransaction Transaction)
{
    if (Transaction == null)
        return false;
    //no need to set the defaults if the state is already valid
    if (Transaction.State.Status == Status.Valid)
        return true;
    //set all the default values for the transaction
    return true;
}</pre>
<p>Ok, I know it&#8217;s not a pillar of phenomenal code.  But you get the point, both of the sides of the | are run.  Now, what if you&#8217;ve all ready evaluated the status earlier in code?  No need to do it again.  But you may need to still set the defaults.  Well, that is where the |= comes into play.</p>
<pre name="code" class="csharp">bool transStatus = VerifyStatus(Transaction.State);
//do something with the code
transStatus |= SetDefaults(Transaction);
if(transStatus)
{
    //do something with the code
}</pre>
<p>In the case of an int a bitwise operation is performed.</p>
<pre name="code" class="csharp">int a = 0x0c;
a = a | 0x06;
//a results in 0x0000000e
//1100
//0110 results in
//1110 which is e</pre>
<p>Like the logical operation example above the | and assignment can be collapsed down to:</p>
<pre name="code" class="csharp">int a = 0x0c;
a |= 0x06;</pre>
<p>I think it&#8217;s very important to understand the difference between | and || as well as &#038; and &#038;&#038;.  Recently I came across a bunch of code where the original developer used | and || interchangeably.  They are not the same.  Using | in place of || overrides the fail fast nature of boolean operations most modern languages support.  They are not interchangeable.  If Transaction was null the following code would not work</p>
<pre name="code" class="csharp">if(Transaction == null | Transaction.State == null)</pre>
<p> simply because both sides of the operation are evaluated so Transaction.State would throw a null pointer exception.  Not only that but your code will run slower.</p>
<p>Next in this series will be checked and unchecked.</p>
<p>Later,<br />
Brian</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.formatexception.com%2F2009%2F01%2Foften-unused-operators-or-equals%2F&amp;title=Often%20Unused%20Operators%3A%20%7C%3D%20%28or%20equals%29&amp;bodytext=I%27ve%20debated%20before%20the%20usefulness%20of%20the%20%7C%20and%20%26%20operators.%20%20The%20use%20of%20the%20%7C%20operator%20depends%20on%20the%20context%20in%20which%20it%20is%20used.%20%0D%0A%0D%0AIn%20the%20case%20of%20bools%20a%20boolean%20operation%20is%20performed.%20%20In%20the%20case%20of%20ints%20a%20bitwise%20OR%20is%20performed.%20%20%0D%0A%0D%0AFor%20bo" title="Digg"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.formatexception.com%2F2009%2F01%2Foften-unused-operators-or-equals%2F&amp;t=Often%20Unused%20Operators%3A%20%7C%3D%20%28or%20equals%29" title="Facebook"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.dotnetkicks.com/kick/?url=http%3A%2F%2Fwww.formatexception.com%2F2009%2F01%2Foften-unused-operators-or-equals%2F&amp;title=Often%20Unused%20Operators%3A%20%7C%3D%20%28or%20equals%29" title="DotNetKicks"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/dotnetkicks.png" title="DotNetKicks" alt="DotNetKicks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.formatexception.com%2F2009%2F01%2Foften-unused-operators-or-equals%2F&amp;title=Often%20Unused%20Operators%3A%20%7C%3D%20%28or%20equals%29&amp;annotation=I%27ve%20debated%20before%20the%20usefulness%20of%20the%20%7C%20and%20%26%20operators.%20%20The%20use%20of%20the%20%7C%20operator%20depends%20on%20the%20context%20in%20which%20it%20is%20used.%20%0D%0A%0D%0AIn%20the%20case%20of%20bools%20a%20boolean%20operation%20is%20performed.%20%20In%20the%20case%20of%20ints%20a%20bitwise%20OR%20is%20performed.%20%20%0D%0A%0D%0AFor%20bo" title="Google Bookmarks"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.formatexception.com%2F2009%2F01%2Foften-unused-operators-or-equals%2F&amp;title=Often%20Unused%20Operators%3A%20%7C%3D%20%28or%20equals%29" title="Live"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Fwww.formatexception.com%2F2009%2F01%2Foften-unused-operators-or-equals%2F&amp;t=Often%20Unused%20Operators%3A%20%7C%3D%20%28or%20equals%29" title="HackerNews"><img src="http://www.formatexception.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.formatexception.com/2009/01/often-unused-operators-or-equals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
