zoukankan      html  css  js  c++  java
  • 转贴一篇关于BitVector32的Blog

    看了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()
    {
    // Create the sections
    // AA BBBBBBB CCCCCCC DDDDDDD EEEEEEEE
    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)
    {
    // Create an instance of BitVector32 using
    // the data passed to this method.
    BitVector32 word = new BitVector32(data);

    // Pull the data out of the BitVector32
    // using the sections created in the
    // constructor.
    int A = word[sectionA];
    int B = word[sectionB];
    int C = word[sectionC];
    int D = word[sectionD];
    int E = word[sectionE];

    // Do something with the data here...
    }
    }
    }

    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

  • 相关阅读:
    操作正则表达式的公共类
    验证码类
    强制转化常用函数
    实现Base64编码与其它编码转换的类
    关于任务管理器踩过的坑,程序员必看!
    每次找Internet选项感到抓狂?一键打开!
    教你避过安装TensorFlow的两个坑
    HTML中id与name的通俗区别
    VS code代码对齐快捷键
    form和table的区别
  • 原文地址:https://www.cnblogs.com/light/p/35834.html
Copyright © 2011-2022 走看看