惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
MyScale Blog
MyScale Blog
博客园 - 【当耐特】
I
InfoQ
腾讯CDC
aimingoo的专栏
aimingoo的专栏
L
LangChain Blog
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
Vercel News
Vercel News
C
Check Point Blog
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
D
Docker
MongoDB | Blog
MongoDB | Blog
量子位
博客园_首页

博客园 - light's cafe

Queue和Stack的学习代码 BitVector32结构学习 依赖单元测试开发 调整过的书籍目录 今天晚上的遭遇 设计,UML,测试驱动开发 看牙记 我是LIGHT的LP,今天由我代笔 明天去买干粒猫粮 面向接口编程 关于测试驱动开发的文章 今天我只睡了三个小时 强命名程序集 我,我 庆祝一下 看兽医记 程序集文件中的版本号 AL链接器 怪事时时有不如昨天多
转贴一篇关于BitVector32的Blog
light's cafe · 2004-08-23 · via 博客园 - light's cafe

看了msdn中对BitVector32.CreateSection方法的讲解后根本就不知道它在说什么,于是便在网上找啊找,终于被我找到了,呵呵。

The joys of BitVector32

Abstract

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

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

Introducing BitVector32

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];

The Code

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];


}
}
}

Conclusion

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.

Update

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