There is the following code in C ++:

#include <iostream> using namespace std; union { unsigned short X; struct { unsigned short param_01 :1; // DSL (младший бит числа X) unsigned short param_02 :1; // PPP unsigned short param_03 :1; // Link unsigned short param_04 :2; // битовое поле может содержать unsigned short param_05 :5; // более 1 бита unsigned short param_06 :1; unsigned short param_07 :1; unsigned short param_08 :2; } X_bit; } Device; int main() { cin >> hex >> Device.X; // вводим число // Выводим результаты cout << Device.X_bit.param_01; cout << Device.X_bit.param_02; // и так далее return 0; } 

How to get similar functionality in C #? Are there any bitfields, and if not, how best to work with such data?

  • 7
    ruSO is not a translation site. - Andrew Bystrov
  • 2
    Learn bit operations. You will need them, since there is no direct analogue to packed structures in C #. - Pavel Mayorov
  • five
    This question should be closed, because there is no translation agency here - Vladimir Martyanov
  • one
    To begin with, your code is not the correct C ++ code, since you are not accessing the union member that you assigned. - VladD
  • 3
    Well, okay, you close, the question is not trivial. Poorly formulated, yes. - VladD

1 answer 1

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]); } } 
  • If it doesn’t make it difficult, could you comment on the code, the main question is why it is necessary and why not use BitArray, not even in the context of this question, but in general, why pull the structure out, in my opinion, 32 bits? - Align
  • one
    BitArray is an array of bits, it has convenient access to individual bits, but there is no convenient access to fields larger than one bit. I don’t see any problems with the 32-bit structure. - VladD