For your case, you can use the BitVector32 structure.
For convenience, let's set this auxiliary class:
class SectionHelper { BitVector32.Section? currSection = null; public BitVector32.Section AllocatedSection(int nbits) { // проверка if (nbits <= 0 || nbits > sizeof(short) * 8 - 1) throw new ArgumentException("wrong number of bits"); var max = checked((short)((1 << nbits) - 1)); currSection = currSection == null ? BitVector32.CreateSection(max) : BitVector32.CreateSection(max, currSection.Value); return currSection.Value; } }
We try:
class Program { static void Main(string[] args) { // определяем битовые маски SectionHelper helper = new SectionHelper(); var dsl_s = helper.AllocatedSection(1); var ppp_s = helper.AllocatedSection(1); var link_s = helper.AllocatedSection(1); var param_04_s = helper.AllocatedSection(2); var param_05_s = helper.AllocatedSection(5); var param_06_s = helper.AllocatedSection(1); var param_07_s = helper.AllocatedSection(1); var param_08_s = helper.AllocatedSection(2); // пользуемся var bv = new BitVector32(0x341a); Console.WriteLine(bv[dsl_s]); Console.WriteLine(bv[ppp_s]); Console.WriteLine(bv[link_s]); Console.WriteLine(bv[param_04_s]); Console.WriteLine(bv[param_05_s]); Console.WriteLine(bv[param_06_s]); Console.WriteLine(bv[param_07_s]); Console.WriteLine(bv[param_08_s]); Console.WriteLine(); bv = new BitVector32(0xcbe5); Console.WriteLine(bv[dsl_s]); Console.WriteLine(bv[ppp_s]); Console.WriteLine(bv[link_s]); Console.WriteLine(bv[param_04_s]); Console.WriteLine(bv[param_05_s]); Console.WriteLine(bv[param_06_s]); Console.WriteLine(bv[param_07_s]); Console.WriteLine(bv[param_08_s]); } }