`

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

 

 

分享到:
评论

相关推荐

    Head First C# -- Third Edition

    Head First C# -- Third Edition by Andrew Stellman and Jennifer Greene Copyright © 2013 Andrew Stellman and Jennifer Greene. All rights reserved.

    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 ...

    A-collection-of-snippets-for-C#,-C++-and-VB.-snippetica.zip

    A_collection_of_snippets_for_C#,_C++_and_VB._snippetica.zip A_collection_of_snippets_for_C#,_C++_and_VB._snippetica.zip A_collection_of_snippets_for_C#,_C++_and_VB._snippetica.zip A_collection_of_...

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

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

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

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

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

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

    C#版USB-HID范例

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

    Accelerated C# 2010 PDF 英文版

    Many books introduce C#, but very few also explain how to use it optimally with the .NET Common Language Runtime (CLR). This book teaches both core C# language concepts and how to wisely employ 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 ...

    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文件,特别是针对大量数据...

Global site tag (gtag.js) - Google Analytics