I quickly wrote a method that does exactly what you want, of course, not the best:
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:
. "1" s 2 . - "1" .
EDIT:
, :
public static int BitFlagBitCount(int flag) {
var bitStr = Convert.ToString(flag, 2);
return bitStr.Count(c => c == '1');
}