`

c# - BitVecttor and its use

    博客分类:
  • C#
c# 
阅读更多

BitVector is something that provided by the .net Framework, which is provided to aim the development in the memory effecient way.

 

 

You can use the BitVector to create some bit flags construct; The BitVector also has the concept of Sectoin, whereas you can group several bit to a bit section, and operate on the bitsection as a whole.

 

 

The original code is copied and remodeld from the MSDN discussion, for more detail son the APIs, please see the origial site here: "BitVector Structure"

 

 

Use the BitVector to create bit flags

 

In the following code, it shows how to create the bit flags and the relevante bit mask with the help of BItVector.

 

public static void DemoBitVector32()
    {
      BitVector32 myBV = new BitVector32();


      int myBit1 = BitVector32.CreateMask(); // A mask that isolates the bit flag following the one that previous points to in System.Collections.Specialized.BitVector32.
      int myBit2 = BitVector32.CreateMask(myBit1); // it can accept the previous bit order 
      int myBit3 = BitVector32.CreateMask(myBit2); // it can accept the previous bit order 
      int myBit4 = BitVector32.CreateMask(myBit3); // it can accept the previous bit order 
      int myBit5 = BitVector32.CreateMask(myBit4); // it can accept the previous bit order 

      // set the alternating bit to TRUE:
      Console.WriteLine("Setting alternating bits to TRUE");
      Console.WriteLine("  Initial:    {0}", myBV.ToString());

      myBV[myBit1] = true;
      Console.WriteLine("Setting alternating bits to TRUE");
      Console.WriteLine("  Initial:    {0}", myBV.ToString());

      myBV[myBit3] = true;
      Console.WriteLine("Setting alternating bits to TRUE");
      Console.WriteLine("  Initial:    {0}", myBV.ToString());

      myBV[myBit5] = true;
      Console.WriteLine("Setting alternating bits to TRUE");
      Console.WriteLine("  Initial:    {0}", myBV.ToString());

    }
 

Use BitVector to create Bit Sections

 

The following code uses the BitVector to create bit Sections. 

 

public static void DemoBitVector32_v2()
    {
      BitVector32 myBV = new BitVector32(0);

      BitVector32.Section mySect1 = BitVector32.CreateSection(6);  
      BitVector32.Section mySect2 = BitVector32.CreateSection(3, mySect1);
      BitVector32.Section mySect3 = BitVector32.CreateSection(1, mySect2);
      BitVector32.Section mySect4 = BitVector32.CreateSection(15, mySect3);
      
      // Displays the values of the sections.
      Console.WriteLine("Initial values:");
      Console.WriteLine("\tmySect1: {0}", myBV[mySect1]);
      Console.WriteLine("\tmySect2: {0}", myBV[mySect2]);
      Console.WriteLine("\tmySect3: {0}", myBV[mySect3]);
      Console.WriteLine("\tmySect4: {0}", myBV[mySect4]);


      Console.WriteLine("Changing the value of each section\n");
      Console.WriteLine("\tInitial:   \t{0}", myBV.ToString());

      myBV[mySect1] = 5;
      Console.WriteLine("\tmySect1 = 5:\t{0}", myBV.ToString());

      myBV[mySect2] = 3;
      Console.WriteLine("\tmySect1 = 5:\t{0}", myBV.ToString());

      myBV[mySect3] = 1;
      Console.WriteLine("\tmySect1 = 5:\t{0}", myBV.ToString());

      myBV[mySect4] = 9;
      Console.WriteLine("\tmySect1 = 5:\t{0}", myBV.ToString());

      Console.WriteLine("New values:");
      Console.WriteLine("\tmySect1: {0}", myBV[mySect1]);
      Console.WriteLine("\tmySect2: {0}", myBV[mySect2]);
      Console.WriteLine("\tmySect3: {0}", myBV[mySect3]);
      Console.WriteLine("\tmySect4: {0}", myBV[mySect4]);
    }
 

 

 

分享到:
评论

相关推荐

    c#-ymodem-update

    标题"C#-ymodem-update"指的是一个使用C#编程语言开发的YMODEM协议实现,主要功能是用于设备固件的升级,特别是针对bootloader的更新。YMODEM是一种流行的串行通信协议,用于在计算机之间传输文件,尤其是在低带宽和...

    大恒-双相机开发-C#-多线程-项目开源

    《大恒双相机开发-C#-多线程项目开源解析》 在当今信息化时代,高效、稳定的图像处理系统成为许多领域不可或缺的技术支持。本项目"大恒-双相机开发-C#-多线程"正是这样的一个实例,它利用C#语言进行编程,实现了对...

    protobuf实例-C#-聊天服务器

    在本实例中,“protobuf实例-C#-聊天服务器”是基于C#语言实现的一个聊天服务项目,利用protobuf进行数据序列化和反序列化,以便于在网络通信中高效地传输聊天消息。 在C#环境下,protobuf提供了编译器工具,可以将...

    C# - CSharp 12 in a Nutshell The Definitive Reference

    ### C# 12 in a Nutshell:The Definitive Reference #### 1. Introducing C# and .NET ...examples, and practical guidance for intermediate and advanced programmers looking to master C# and .NET.

    C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition

    C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition by Mark J. Price English | 24 Mar. 2017 | ASIN: B01N8UG78Q | 594 Pages | AZW3 | 23.26 MB Modern Cross-Platform Development ...

    C# - 模拟开心农场

    名称: “C# - 模拟开心农场” 说明: C#模拟开心农场,经历播种、生长、开花、结果、收获的过程; 源代码分为两个版本: 一是手动实现成长收获的过程,另一版本是后台线程自动实现成长收获的过程 ^_^

    C#-WPF实现抽屉效果SplitView-炫酷漂亮的侧边菜单效果+MD主题重绘原生控件的美观效果-源码Demo下载

    在本文中,我们将深入探讨如何使用C#和WPF(Windows Presentation Foundation)技术来实现一个具有抽屉效果的SplitView控件,以及如何通过MD(Material Design)主题重绘原生控件,以达到美观的效果。这个源码Demo...

    C#版USB-HID范例

    【标题】"C#版USB-HID范例" 涉及的是在C#编程环境中与USB设备进行Human Interface Device (HID)通信的技术。USB-HID类库允许开发者直接与各种类型的输入设备(如键盘、鼠标、游戏控制器等)进行交互,而无需关心具体...

    C#编程 数据库操作应用 UseSelect(源码)(源码)

    C#编程 数据库操作应用 UseSelect(源码)(源码)C#编程 数据库操作应用 UseSelect(源码)(源码)C#编程 数据库操作应用 UseSelect(源码)(源码)C#编程 数据库操作应用 UseSelect(源码)(源码)C#编程 数据库操作应用 Use...

    C# - 简易聊天室实现网页

    在IT行业中,C#是一种广泛使用的编程语言,尤其在开发Web应用程序时,它与ASP.NET框架结合,可以创建高效、动态的网站。本教程将深入探讨如何使用C#和ASP.NET构建一个简易的聊天室网页。 首先,让我们理解标题"**C#...

    C#-升级版文本编辑器

    "C#-升级版文本编辑器"项目显然旨在创建一个增强版的文本编辑器,可能包含了更多特性、优化和自定义选项,以满足更广泛的用户需求。接下来,我们将深入探讨这个项目可能涉及到的C#编程知识和技术点。 1. **Windows ...

    最新3.3支付宝即时到账交易接口demo源码c#-gbk源码

    以上是对"最新3.3支付宝即时到账交易接口demo源码c#-gbk源码"所涉及的关键知识点的详细解析,通过这个源码,开发者可以学习如何在C#环境下实现与支付宝接口的集成,完成线上支付功能。在实际开发过程中,还需要结合...

    C#学习的101个经典例子

    #学习的101个经典例子,例子个个经典,涵盖C#的方方面面,带有说详尽的注释 Advanced - Multithreading - How-To Async Calls Advanced - Remoting - How-To TCP Remoting Advanced - Serialization - How-To ...

    C# 5.0 All-in-One For Dummies

    Crammed with 800+ pages of information, this all-in-one book helps you quickly create a C# program while learning C# and object-oriented programming fundamentals, Windows and Windows phone ...

    Professional C# 7 and .NET Core 2.0, 7th Edition

    With Microsoft speeding up its release cadence while offering more significant improvement with each update, it has never been more important to get a handle on new tools and features quickly....

    C#-WinForm(2种dataGridView导出Excel)批量导出,高效率,36列万行,15秒

    在C# WinForm开发中,数据展示和处理是常见的任务,而`dataGridView`控件是常用的界面元素,用于显示和编辑表格数据。本教程将详细讲解如何高效地将`dataGridView`中的数据批量导出到Excel文件,特别是针对大量数据...

    c#读取DLT645-2007电力协议,项目源码

    这个项目源码是使用C#编程语言实现的,旨在帮助开发者实现对多块电表的数据读取、解析以及统计功能。下面我们将详细探讨这个项目涉及的关键知识点。 1. C#编程语言:C#是由微软开发的一种面向对象的编程语言,广泛...

Global site tag (gtag.js) - Google Analytics