4

I am storing all my equipped guns using a bit flag, and was just wondering if it was possible to check how many numbers are contained in a bit flag.

e.g. :

13 would contain 1, 4, and 8

NOTE: I am new to bit flags so my question might not make too much sense or I might have used the wrong terminology, if so just let me know and I will be happy to change it.

Peyton
  • 123
  • 1
  • 2
  • 8
  • Why does 13 contain 1, 4 and 8? – rene Dec 11 '16 at 17:41
  • 1+4+8 = 13..... – Peyton Dec 11 '16 at 17:42
  • 10+3 = 13 why doesn't that count? – rene Dec 11 '16 at 17:43
  • 1
    OP meant bitmask @rene – M.kazem Akhgary Dec 11 '16 at 17:43
  • I'm trying to teach some fishing here @M.kazemAkhgary but thanks for giving that. From here it is easy. – rene Dec 11 '16 at 17:44
  • 1
    no because it goes 1,2,4,8,16 and so on – Peyton Dec 11 '16 at 17:45
  • https://msdn.microsoft.com/en-us/library/17zwb64t.aspx - this is exactly what a bitwise and between your value and the "contained" number is for – Preston Guillot Dec 11 '16 at 17:45
  • 2
    Ok, why does it go like that? Because if you answered that you're at the solution. – rene Dec 11 '16 at 17:46
  • I don't know, like I said I'm new to bit masks, I honestly only know how and not why, thats my problem – Peyton Dec 11 '16 at 17:47
  • 1
    @Peyton if you're newbie, then try to comprehend [what bit masking is](http://stackoverflow.com/questions/10493411) – Orkhan Alikhanov Dec 11 '16 at 17:50
  • ok thank you, but can someone please answer my question? – Peyton Dec 11 '16 at 17:54
  • Do you understand binary numbers and how they are relevant to the question at hand? If you don't, study them and then ask again if you are still encountering problems. This isn't a place where people will simply solve your problems. You've received a few pointers of where you should look to learn to solve your problem. Now go ahead and try. – InBetween Dec 11 '16 at 17:57
  • Given that "guns" are probably something you want to enumerate or iterate over from time to time, and enumerations in C# are not exactly iteration-friendly, are you sure you want to store that value as a set of bit flags? – O. R. Mapper Dec 11 '16 at 21:14

2 Answers2

2

Since you are asking:

How many numbers are contained in a bit flag?

This should work:

int CountBits(int n)
{
    int count = 0;
    do
    {
        int has = n & 1;
        if (has == 1) 
        {
            count ++ ;
        }

    } while((n >>= 1) != 0);

    return count;
}
Orkhan Alikhanov
  • 9,122
  • 3
  • 39
  • 60
0

I quickly wrote a method that does exactly what you want, certainly not the best though:

public static List<int> DecomposeBitFlag(int flag) {
    var bitStr = Convert.ToString(flag, 2);
    var returnValue = new List<int>();
    for(var i = 0 ; i < bitStr.Length ; i++) {
        if (bitStr[bitStr.Length - i - 1] == '1') {
            returnValue.Add((int)Math.Pow(2, i));
        }
    }
    return returnValue;
}

How it works:

I first convert the integer parameter to a binary string. For each of the "1"s in the string, add 2 to the power i to the return value list. i is the index of the "1" in the reversed string.

EDIT:

If you only want to know the number of bits, this will do:

public static int BitFlagBitCount(int flag) {
    var bitStr = Convert.ToString(flag, 2);
    return bitStr.Count(c => c == '1');
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313