2009-07-31

Swap two variable values

That’s the typical example when we’re all learning how to code, swap the values of a and b. Well, here’s what they probably taught us:

  1: int a = 4;
  2: int b = 3;
  3: int temp;
  4: //Swap
  5: temp = a;
  6: a = b;
  7: b = temp;

And here’s the way to do it with out using the temporary variable, with just boolean logic, and the ever underestimated XOR (^):

  1: int a = 4;
  2: int b = 3;
  3: //Swap
  4: a = a ^ b;
  5: b = a ^ b;
  6: a = a ^ b;

Another way, with just pluses and minuses…

  1: int a = 4;
  2: int b = 3;
  3: a -= b;
  4: b += a;
  5: a = b - a;

I’ve actually seen many ways to do it, specially on this thread

2009-07-30

Difference between logical operators in C#

Yes, you’ve probably seen that the AND and OR logical operators have two ways of being written in C#: The first way is like this: && (AND), || (OR), and the second is this: & (AND), | (OR). In case you didn’t notice the difference, the first ones use a double operator, while the last ones use only one.

As you already might know, AND operator returns true if both sides of an expression are true, and OR operator returns true if at least one side of the expression is true.

Even though most of us programmers actually know that there are two “ways” to write the AND and OR operators, few know (that was my case before I looked it up) the difference between both. Well, here it is:

When you use double logical operators (which is probably most of the time), the compiler only evaluates the right part of the expression if the first one doesn’t already make the expression false. The opposite happens when you use single logical operators: both sides of the expression get evaluated no matter what. Let’s clear that out with an example:

  1: if(myDataSet.Tables.Count > 0 && myDataSet.Tables[0].Rows.Count > 0)
  2: {
  3:    //Do something
  4: }

Because we’re using the double AND operator (&&), the compiler will automatically mark the whole expression as false if it finds that the left side of it is already false, without even bothering to evaluate the right side. This is very useful in this specific case with the DataSet, because sometimes we need to evaluate both that the DataSet has a table, and that table has at least one row. That will save us having to use two nested if statements:

  1: if(myDataSet.Tables.Count > 0)
  2: {
  3:    if(myDataSet.Tables[0].Rows.Count > 0)
  4:    {
  5:       //Do something
  6:    }
  7: }

Now, if we use only the single AND operator, the compiler will evaluate both sides of the expression no matter what, so this code:

  1: if(myDataSet.Tables.Count > 0 & myDataSet.Tables[0].Rows.Count > 0)
  2: {
  3:    //Do something
  4: }

will result in an ArgumentNullException if our DataSet doesn’t have a table, caused by the myDataSet.Tables[0].Rows.Count > 0 comparison.

There’s one more logical operator, the XOR (^), which will return true only if both sides of the expression are different (one is true and the other is false). This, in practical terms, is the same as the DIFFERENT (!=) operator, and it doesn’t have a “double” counterpart, because obviously in this case, both sides of the expression have to always be evaluated.

How could you NOT blog?

Blogs are everywhere, and it’s so easy to create one that it’s impossible, specially if you’re in the programming world, not to get influenced by the hype. I actually did that a few years ago, but this is probably the first time I really want to make a serious blog, which might be helpful to people.

All my previous blogs were written in Spanish (I’m form Colombia, South America), but i think that a a blog in English could get to more people. Besides, this is a blog about programming (yet another one, I know, but what can you do?), and most people, even non-English speakers, would search the Web in English when it comes to those topics.

Anyway, I still have my Spanish programming blog, Puro Punto Net, and I’m sure planning on not quitting on it.

Hope you people find this blog interesting, and any comments or critiques are well appreciated.