


















I needed to parse some data coming from an embedded system. This system had crammed lots of data into 32 bit integers. I needed a way of pulling out the individual values, and the BitVector32 fit the bill perfectly. Since the documentation was lacking (as usual), I've provided an example here.
The data coming from this embedded system was crammed into 32 bit integers. The format was defined as so many bits for one value, then the next so many bits for the next, and so on.
AABBBBBBBCCCCCCCDDDDDDDEEEEEEEE
Section A : 2 bits
Section B : 7 bits
Section C : 7 bits
Section D : 7 bits
Section E : 8 bits
In the constructor of my class, I created several BitVector32.Section instances. The first argument determines how many bits to use. If I wanted to create a section that used 4 bits, I would convert binary 1111 to decimal, 15. Notice that after creating the first section, the ones following use the previous section as the second argument. This second argument causes the section to start where the previous one left off.
sectionA = BitVector32.CreateSection(3);
sectionB = BitVector32.CreateSection(127, sectionA);
sectionC = BitVector32.CreateSection(127, sectionB);
sectionD = BitVector32.CreateSection(127, sectionC);
sectionE = BitVector32.CreateSection(255, sectionD);
Then in a public method of my class, I used the BitVector32.Section instances I had created to pull the data out of the integer.
BitVector32 word = new BitVector32(data);
int A = word[sectionA];
int B = word[sectionB];
int C = word[sectionC];
int D = word[sectionD];
int E = word[sectionE];
Here is the full listing of the code.
using System;
using System.Collections.Specialized;namespace aspZone
{public class BitVector32Demo
{
private BitVector32.Section sectionA;
private BitVector32.Section sectionB;
private BitVector32.Section sectionC;
private BitVector32.Section sectionD;
private BitVector32.Section sectionE;public BitVector32Demo()
{
sectionA = BitVector32.CreateSection(3);
sectionB = BitVector32.CreateSection(127, sectionA);
sectionC = BitVector32.CreateSection(127, sectionB);
sectionD = BitVector32.CreateSection(127, sectionC);
sectionE = BitVector32.CreateSection(255, sectionD);
}public void ParseData(int data)
{
BitVector32 word = new BitVector32(data);
int A = word[sectionA];
int B = word[sectionB];
int C = word[sectionC];
int D = word[sectionD];
int E = word[sectionE];
}
}
}
I am sure there are other ways of accomplishing the same goal. But the BitVector32 class did the job, and the code is easy to read and maintain. If you know a better way of doing the same thing, please add your comment below.
Apparently, there is a bug in the class as reported by Mattias Sjögren on Roy's blog:
FYI, the BitVector32 has a bug in that the indexer always returns false for bit #31, even if it's set. The following code should reproduce it.
class BitVectorBugDemo
{
static void Main()
{
BitVector32 bv = new BitVector32( unchecked((int)0xFFFFFFFF) );
int mask = BitVector32.CreateMask();for ( int bit = 0; bit < 32; bit++ ) {
Console.WriteLine( "Bit: {0}, Mask: {1:X}, value: {2}", bit, mask, bv[mask] );
if ( bit < 31 ) mask = BitVector32.CreateMask( mask );
}
}
}
posted on Friday, July 18, 2003 10:32 AM
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。