Pure.Krome
Pure.Krome

Reputation: 87047

Is my understanding of this piece of code correct?

I'm reviewing some code from one of our contractors:

if (userLists != null)
{
    Int32 numberOfItems = userLists.Count;
    if ((numberOfItems & 1) == 1)
    {
        var emptyList = new tblList();
        userLists.Add(emptyList);
    }
}

Now, I'm trying to understand this, so, can someone please confirm with me if I have got this right or not?

  1. Do we have an instance of a userList?
  2. Yes. Get a the number of items in the userLists object.
  3. if (the number of items equals 1 (Yes/true) AND 1 equals 1 (yes/True) ) AND that result equals 1 (Yes/True), then add an empty list object to the list.
  4. Else: nothing.

If so (W.T.FFFFFFFFFFFFFFFFFFFF !!!!!!!!!!), that can be refactored to

if (numberOfItems == 1)
{
..
}

but even that's crap because I don't want a list with 'empty' items.

So did I read that piece of code correctly?

Oh, one more sigh at the use of Int32 vs Int :( (but I digress).

Upvotes: 1

Views: 252

Answers (9)

Chad Grant
Chad Grant

Reputation: 45422

Checking for odd's, Can also do i % 2 != 0

I would suggest looking at BitMasks they can be pretty handy, but not in the code in your question, I would prefer the modulous if you needed to do even/odd.

    static void Main(string[] args)
    {
        for (int i = 0; i < 100; i++ )
            Console.WriteLine( i & 1);
        Console.ReadLine();
    }


1
0
1
0
1
0

And for the heck of it, here's some extension methods

   class Program
    {
        static void Main(string[] args)
        {
            List<int> ints = new List<int>();

            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine("Mod: {0}", i % 2 );
                Console.WriteLine("BitWise: {0}", i & 1 );

                ints.Add(i);
                Console.WriteLine("Extension: {0}", ints.IsEven() );
            }
            Console.ReadLine();
        }
   }

    public static class ListExtensions
    {
        public static bool IsEven<T>(this ICollection<T> collection)
        {
            return (collection.Count%2) == 0;
        }

        public static bool IsOdd<T>(this ICollection<T> collection)
        {
            return (collection.Count%2) != 0;
        }
    }

Upvotes: 3

Smashery
Smashery

Reputation: 59703

The & is what is called a bitwise operator. Whereas the operator && tests two boolean values:

TRUE && FALSE => FALSE
TRUE && TRUE => TRUE

the & operator can work on integer values:

  00101101 (45)
& 01011011 (91)
---------------
= 00001001 (9)

Each bit has the boolean operation (and) done on it. So in the case of your code sample, it is asking "is the last bit a 1?" - that is to say "is it odd?" For instance, if the number is 23:

  00010111 (23)
& 00000001 (1)
---------------
= 00000001 (1)

So it adds to the list because 1 == 1. But if the number was 22:

  00010110 (22)
& 00000001 (1)
---------------
= 00000000 (0)

So it does not add to the list.

Upvotes: 6

JP Alioto
JP Alioto

Reputation: 45127

Since we're being silly ...

public static class Extensions
{
    public static bool IsEven(this Int32 integer)
    {
        return (integer % 2 == 0);
    }
}

Let's you do ...

numberOfItems.IsEven()

Upvotes: 3

JH.
JH.

Reputation: 4219

Is zero elements ok?

x = (0 & 1) then (x == 1) is false...

I think you should ask your contractors to comment their code more.

Upvotes: 0

Mitch Wheat
Mitch Wheat

Reputation: 300759

Could be refactored to be more understandable/maintainable:

if (userLists != null)
{    
    EnsureListHasAnEvenNumberOfItems(userLists);
}

Upvotes: 0

sharptooth
sharptooth

Reputation: 170539

((numberOfItems & 1) == 1)

& with 1 tests the 0th bit. For integer data types all odd values have the 0th bit set and all the even values have it cleared. The code above effectively tests for the value being odd.

Upvotes: 0

T.R.
T.R.

Reputation: 7592

for the (numberOfItems &1)==1, that's a bitwise AND. It seems to be checking if numberOfItems is odd, and adding an empty list if it is.

Upvotes: 0

leiz
leiz

Reputation: 4052

(numberOfItems & 1) == 1

is more like numberOfItems%2 != 0

Upvotes: 0

Paige Ruten
Paige Ruten

Reputation: 176783

Bitwise-ANDing a number with 1 checks whether the number is odd or even (returns 1 if odd). What this code is doing is making sure the list has an even number of items by adding another item if there is an odd number of items.

Upvotes: 6

Related Questions