Killnine
Killnine

Reputation: 5900

Converting list of bools to a single uint

I have a class that is parsing an object to be sent to a another hardware device. The class contains a bunch of features that are either enabled or disabled (bools). However, we only write words to data to the piece of hardware (essentially a uint, or 16-bit integer).

Therefore, I need to translate

Feature1 (bool, true)
Feature2 (bool, false)
Feature3 (bool, false)
Feature4 (bool, false)
...
Feature16 (bool, false)

and parse it into a single word:

000000000001

The device sees a word as little-endian, fyi.

I was trying to bitwise-or against a uint, but the fact that the flags are bools makes this messy so I was wondering if anyone else had any slick suggestions ;)

EDIT: No sooner than I post this, I relaxed and thought of this solution...

private static ushort BooleanToUint(List<bool> bools)
        {
            ushort word = 0;
            for(int i = 0; i < bools.Count; i++)
            {
                if(bools[i])
                {
                    int twoToPower = (1 << i);
                    word = (ushort) (word + twoToPower);
                }
            }

            return word;
        }

Upvotes: 0

Views: 2121

Answers (4)

Cos Callis
Cos Callis

Reputation: 5084

Here is your "slick" suggestion:

create an extension method on the type of "UInt16[]" that returns the array of values to a 'single word' like this:

public static class UintArrayExtender
   {
      public static string ToWord(this UInt16[] array)
      {
         string val = string.Empty;
         if (array.Length > 0)
         {
            //works the array backwards
            for (int i = array.Length; i >= 0; i--)
            {
               val += array[i].ToString();

            }
         }
         return val;

      }

   }

now, anywhere you need to use it you can return an array of UInt16 to a single word like this:

      public string getTheWord()
      {
         UInt16[] myArray = new UInt16[15];
         myArray[0] = 0;
         myArray[1] = 0;
         myArray[2] = 0;
         myArray[3] = 0;
         myArray[4] = 0;
         myArray[5] = 0;
         myArray[6] = 0;
         myArray[7] = 0;
         myArray[8] = 0;
         myArray[9] = 0;
         myArray[10] = 0;
         myArray[11] = 0;
         myArray[12] = 0;
         myArray[13] = 0;
         myArray[14] = 0;
         myArray[15] = 1;

         return myArray.ToWord();

      }
   }

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 160952

If you have your boolean values in an array you can just use to index to set the corresponding bit (if set):

bool[] boolValues = Enumerable.Repeat(true, 16).ToArray(); //test
uint intValue = 0;
for (int i = 0; i < boolValues.Length; i++)
{
    if (boolValues[i])
        intValue += (uint)(1 << i);
}

Upvotes: 1

Marino Šimić
Marino Šimić

Reputation: 7342

I did not understand exactly the feature part but can something like this help you somehow?

public static bool getFeature(int featureid) {
    return true; // or whatever
}

static void Main(string[] args)
{
    int bitmask = 0;
    for (int i = 0; i < 16; i++) {
        bitmask += getFeature(i) ? 1 << i : 0;
    }
}

Upvotes: 0

Robbie Tapping
Robbie Tapping

Reputation: 2556

bool[] items;

items[1] = true;
items[2] = true;
items[3] = true;
....

items[15] = true;

string s = null;

for(var i = 15; i <= 15; i--)
{
 s += Int.Parse(items[i].ToString()); 
}

return s;

Upvotes: 0

Related Questions