Kieran
Kieran

Reputation: 306

How can I convert an int into its corresponding set of bitwise flags without any enum

Is it possible to get all possible bitwise values form an int without any enum?

The scenario is that I need to retrieve comments from a remote database. The database has a fixed comments table with a MASK field that identifies each comment.

1 = "comment one"
2 = "comment two"
4 = "comment three"
8 = "comment four"
.
.
.
etc

Then on another table, the selected comment combination is referenced using a bitwise int. These comments can be added to or changed by at the remote end via a web interface. My client app needs to just pull back the comments that were selected for a given record so I effectively need to reverse engineer a bitwise flag int into all its possible ints. As the comments table at the remote end is changeable, i cannot use an enum.

So can anyone tell me, Using c# how do I reverse engineer a bitwise int into its individual ints please?

Many thanks for any help

Upvotes: 0

Views: 1973

Answers (4)

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

Bitwise and (&) and or (|) are operations you are looking for. I.e. take bit corresponding to 8:

 var commentForFlag = value & 8;

Note that enums or named constants may make code more readable like value & CommentMask.Forth.

One more thing you may be looking for is bit-shifts <<:

for (var commentIndex = 0; commentIndex < 32; commentIndex)
{
   var isThere = (value & (1 << commentIndex)) != 0;
   Console.WriteLine("Comment {0} is present = {1}", commentIndex, isThere);
}

Upvotes: 3

Kieran
Kieran

Reputation: 306

I wonder am I on to something here

    private static IEnumerable<int> GetValues(int maskValue)
    {                        
        int max = 131072;

        for (int i = max; i > 0; i/=2)
        {
            int x = i & maskValue;
            if (x > 0)
            {
                yield return x;
            }
        }
    }

Upvotes: 1

ajawad987
ajawad987

Reputation: 4637

As others above have mentioned, you'd use the bit-wise operators (& and |) for determining the bits set for a given integer value. Below is a sample of how to use them:

namespace Playground2014.ConsoleStuff
{
    using System;

    internal static class Program
    {
        static void Main()
        {
            const int MaskForCommentOne = 1;
            const int MaskForCommentTwo = 2;
            const int MaskForCommentThree = 4;
            const int MaskForCommentFour = 8;
            const int MaskForCommentFive = 16;
            const int MaskForCommentSix = 32;
            const int MaskForCommentSeven = 64;
            const int MaskForCommentEight = 128;

            int myCommentNumber = 72;
            Console.WriteLine("My input number is: {0}", myCommentNumber);
            if(MaskForCommentOne == (myCommentNumber & MaskForCommentOne))
            {
                Console.WriteLine("Comment One");
            }

            if(MaskForCommentTwo == (myCommentNumber & MaskForCommentTwo))
            {
                Console.WriteLine("Comment Two");
            }

            if(MaskForCommentThree == (myCommentNumber & MaskForCommentThree))
            {
                Console.WriteLine("Comment Three");
            }

            if(MaskForCommentFour == (myCommentNumber & MaskForCommentFour))
            {
                Console.WriteLine("Comment Four");
            }

            if(MaskForCommentFive == (myCommentNumber & MaskForCommentFive))
            {
                Console.WriteLine("Comment Five");
            }

            if(MaskForCommentSix == (myCommentNumber & MaskForCommentSix))
            {
                Console.WriteLine("Comment Six");
            }

            if(MaskForCommentSeven == (myCommentNumber & MaskForCommentSeven))
            {
                Console.WriteLine("Comment Seven");
            }

            if(MaskForCommentEight == (myCommentNumber & MaskForCommentEight))
            {
                Console.WriteLine("Comment Eight");
            }
        }
    }
}

The output should be the following:

My input number is: 72
Comment Four
Comment Seven

Hope this helps.

Upvotes: 0

SirH
SirH

Reputation: 575

Use & or | operators

Sample for two int values 187 and 32.

  10111011  =  187
  00100000  =  32
  AND
  00100000  =  32

We could do this as

int a = 187, b = 32;
int result = a & b;

Upvotes: 0

Related Questions