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

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 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