`

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

    C# - 模拟开心农场

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

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

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

    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类库允许开发者直接与各种类型的输入设备(如键盘、鼠标、游戏控制器等)进行交互,而无需关心具体...

    C#学习的101个经典例子

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

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

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

    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# 7 and .NET Core Cookbook

    Easy-to-follow recipes to get you up-and-running with the new features of C# 7 and .NET Core 1.1 Practical solutions to assist you with microservices and serverless computing in C# Explore the new ...

    Hands-On Network Programming with C# and .NET Core.epub

    Hands-On Network Programming with C# and .NET Core: A comprehensive guide to understanding network architecture, communication protocols, and network analysis to build secure applications compatible ...

    C#-图书管理系统(带数据库)

    《C#图书管理系统(带数据库)》是一款基于C#编程语言和数据库技术开发的应用软件,主要功能是管理和维护图书馆的图书资源。系统涵盖了图书的入库、出库、借阅、归还、查询等一系列操作,旨在提高图书管理的效率和...

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

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

Global site tag (gtag.js) - Google Analytics