Bit-sized integers in C#
-
I have a byte[] that contains what in C++ would be this struct:
struct { unsigned type : 5; unsigned level : 2; unsigned bouncing : 1; unsigned emp : 1; unsigned isBomb : 1; unsigned shrapCount : 5; unsigned fireType : 1; };
Is there a way to make the byte[] into a class in C# without doing each member seperately with bitwise AND & shifting? -
I have a byte[] that contains what in C++ would be this struct:
struct { unsigned type : 5; unsigned level : 2; unsigned bouncing : 1; unsigned emp : 1; unsigned isBomb : 1; unsigned shrapCount : 5; unsigned fireType : 1; };
Is there a way to make the byte[] into a class in C# without doing each member seperately with bitwise AND & shifting?You might find the BitVector32 class useful to base your code off of. You would create references to your types with the following code:
BitVector32 bv = new BitVector32(0); BitVector32.Section type = BitVector32.CreateSection(31); BitVector32.Section level = BitVector32.CreateSection(3,type); BitVector32.Section bouncing = BitVector32.CreateSection(1, level); BitVector32.Section emp = BitVector32.CreateSection(1, bouncing); BitVector32.Section isBomb = BitVector32.CreateSection(1, emp); BitVector32.Section shrapCount = BitVector32.CreateSection(31, isBomb); BitVector32.Section fireType = BitVector32.CreateSection(1, shrapCount);
The values are accessed as follows:bv[type] = typeValue; bv[bouncing] = 1;
Access to your values with no &, |, or << -
You might find the BitVector32 class useful to base your code off of. You would create references to your types with the following code:
BitVector32 bv = new BitVector32(0); BitVector32.Section type = BitVector32.CreateSection(31); BitVector32.Section level = BitVector32.CreateSection(3,type); BitVector32.Section bouncing = BitVector32.CreateSection(1, level); BitVector32.Section emp = BitVector32.CreateSection(1, bouncing); BitVector32.Section isBomb = BitVector32.CreateSection(1, emp); BitVector32.Section shrapCount = BitVector32.CreateSection(31, isBomb); BitVector32.Section fireType = BitVector32.CreateSection(1, shrapCount);
The values are accessed as follows:bv[type] = typeValue; bv[bouncing] = 1;
Access to your values with no &, |, or <<