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

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

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