Is my understanding of this piece of code correct?

I am reviewing the 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 figure it out, so can someone please confirm with me if I succeeded or not?

  • Do we have a userList instance?
  • Yes. Get the number of elements in the userLists object.
  • if (cardinality is 1 (yes / true) AND 1 is 1 (yes / true)) And this result is 1 (yes / true) and then adds an empty list object to the list.
  • Else: nothing.

If so (WTFFFFFFFFFFFFFFFFFFFF !!!!!!!!!!), which can be refactored to

if (numberOfItems == 1)
{
..
}

      

but even that crap because I don't need a list with "empty" items.

So, did I read this piece of code correctly?

Oh, one more sigh when using Int32

vs Int

:( (but I digress).

+1


a source to share


9 replies


&

is what is called the bitwise operator. Whereas the operator &&

checks for two booleans:

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

      

operator &

can work with integer values:

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

      

Each bit performs logical operation (s) performed on it. So, in the case of your example code, it asks, "Is the last bit 1?" - that is, "is it weird?" For example, if the number is 23:



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

      

So it gets added to the list because 1 == 1. But if the number is 22:

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

      

Therefore, it is not added to the list.

+6


a source


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



+6


a source


Checking odd, can also be done i % 2 != 0

I would suggest looking at BitMasks , they can be very handy, but not in the code of your question, I would prefer you had 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 that, here are 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;
        }
    }

      

+3


a source


Since we are stupid ...

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

      

Let you do ...

numberOfItems.IsEven()

      

+3


a source


(numberOfItems and 1) == 1

more like numberOfItems% 2! = 0

0


a source


for (numberOfItems & 1) == 1, which is a bitwise AND. Seems to check if numberOfItems is odd and add an empty list if there is one.

0


a source


((numberOfItems & 1) == 1)

      

& with 1 test 0th bit. For integer data types, all odd values ​​have bit 0 and all even values ​​are cleared. The above code effectively checks that the value is odd.

0


a source


May be refactored to be more understandable / maintainable:

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

      

0


a source


Are the zero elements ok?

x = (0 and 1), then (x == 1) is false ...

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

0


a source







All Articles