Export (0) Print
Expand All

BitVector32.CreateSection Method (Int16)

Creates the first BitVector32.Section in a series of sections that contain small integers.

Namespace: System.Collections.Specialized
Assembly: System (in system.dll)

public static Section CreateSection (
	short maxValue
)
public static Section CreateSection (
	short maxValue
)
public static function CreateSection (
	maxValue : short
) : Section
Not applicable.

Parameters

maxValue

A 16-bit signed integer that specifies the maximum value for the new BitVector32.Section.

Return Value

A BitVector32.Section that can hold a number from zero to maxValue.

Exception typeCondition

ArgumentException

maxValue is less than 1.

A BitVector32.Section is a window into the BitVector32 and is composed of the smallest number of consecutive bits that can contain the maximum value specified in CreateSection. For example, a section with a maximum value of 1 is composed of only one bit, whereas a section with a maximum value of 5 is composed of three bits. You can create a BitVector32.Section with a maximum value of 1 to serve as a Boolean, thereby allowing you to store integers and Booleans in the same BitVector32.

If sections already exist in the BitVector32, those sections are still accessible; however, overlapping sections might cause unexpected results.

This method is an O(1) operation.

The following code example uses a BitVector32 as a collection of sections.

using System;
using System.Collections.Specialized;


public class SamplesBitVector32  {

   public static void Main()  {

      // Creates and initializes a BitVector32.
      BitVector32 myBV = new BitVector32( 0 );

      // Creates four sections in the BitVector32 with maximum values 6, 3, 1, and 15.
      // mySect3, which uses exactly one bit, can also be used as a bit flag.
      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] );

      // Sets each section to a new value and displays the value of the BitVector32 at each step.
      Console.WriteLine( "Changing the values of each section:" );
      Console.WriteLine( "\tInitial:    \t{0}", myBV.ToString() );
      myBV[mySect1] = 5;
      Console.WriteLine( "\tmySect1 = 5:\t{0}", myBV.ToString() );
      myBV[mySect2] = 3;
      Console.WriteLine( "\tmySect2 = 3:\t{0}", myBV.ToString() );
      myBV[mySect3] = 1;
      Console.WriteLine( "\tmySect3 = 1:\t{0}", myBV.ToString() );
      myBV[mySect4] = 9;
      Console.WriteLine( "\tmySect4 = 9:\t{0}", myBV.ToString() );

      // Displays the values of the sections.
      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] );

   }

}

/*
This code produces the following output.

Initial values:
        mySect1: 0
        mySect2: 0
        mySect3: 0
        mySect4: 0
Changing the values of each section:
        Initial:        BitVector32{00000000000000000000000000000000}
        mySect1 = 5:    BitVector32{00000000000000000000000000000101}
        mySect2 = 3:    BitVector32{00000000000000000000000000011101}
        mySect3 = 1:    BitVector32{00000000000000000000000000111101}
        mySect4 = 9:    BitVector32{00000000000000000000001001111101}
New values:
        mySect1: 5
        mySect2: 3
        mySect3: 1
        mySect4: 9

*/

import System.*;
import System.Collections.Specialized.*;

public class SamplesBitVector32
{
    public static void main(String[] args)
    {
        // Creates and initializes a BitVector32.
        BitVector32 myBV = new BitVector32(0);
      
        // Creates four sections in the BitVector32 with maximum values 
        // 6, 3, 1, and 15. mySect3, which uses exactly one bit, 
        // can also be used as a bit flag.
        BitVector32.Section mySect1 = BitVector32.CreateSection((short)6);
        BitVector32.Section mySect2 = 
            BitVector32.CreateSection((short)3, mySect1);
        BitVector32.Section mySect3 = 
            BitVector32.CreateSection((short)1, mySect2);
        BitVector32.Section mySect4 = 
            BitVector32.CreateSection((short)15, mySect3);
          
        // Displays the values of the sections.
        Console.WriteLine("Initial values:");
        Console.WriteLine("\tmySect1: {0}", 
            System.Convert.ToString(myBV .get_Item( mySect1)));
        Console.WriteLine("\tmySect2: {0}", 
            System.Convert.ToString(myBV.get_Item( mySect2)));
        Console.WriteLine("\tmySect3: {0}", 
            System.Convert.ToString(myBV.get_Item(mySect3)));
        Console.WriteLine("\tmySect4: {0}", 
            System.Convert.ToString(myBV.get_Item(mySect4)));
          
        // Sets each section to a new value and displays the value of the
        // BitVector32 at each step.
        Console.WriteLine("Changing the values of each section:");
        Console.WriteLine("\tInitial:    \t{0}", myBV.ToString());
        myBV.set_Item(mySect1 , 5);
        Console.WriteLine("\tmySect1 = 5:\t{0}", myBV.ToString());
        myBV.set_Item(mySect2 , 3);
        Console.WriteLine("\tmySect2 = 3:\t{0}", myBV.ToString());
        myBV.set_Item(mySect3 , 1);
        Console.WriteLine("\tmySect3 = 1:\t{0}", myBV.ToString());
        myBV.set_Item(mySect4 , 9);
        Console.WriteLine("\tmySect4 = 9:\t{0}", myBV.ToString());
          
        // Displays the values of the sections.
        Console.WriteLine("New values:");
        Console.WriteLine("\tmySect1: {0}", 
            System.Convert.ToString(myBV.get_Item( mySect1)));
        Console.WriteLine("\tmySect2: {0}", 
            System.Convert.ToString(myBV.get_Item(mySect2)));
        Console.WriteLine("\tmySect3: {0}", 
            System.Convert.ToString(myBV.get_Item(mySect3)));
        Console.WriteLine("\tmySect4: {0}", 
            System.Convert.ToString(myBV.get_Item(mySect4)));
    } //main
} //SamplesBitVector32

/*
This code produces the following output.

Initial values:
        mySect1: 0
        mySect2: 0
        mySect3: 0
        mySect4: 0
Changing the values of each section:
        Initial:        BitVector32{00000000000000000000000000000000}
        mySect1 = 5:    BitVector32{00000000000000000000000000000101}
        mySect2 = 3:    BitVector32{00000000000000000000000000011101}
        mySect3 = 1:    BitVector32{00000000000000000000000000111101}
        mySect4 = 9:    BitVector32{00000000000000000000001001111101}
New values:
        mySect1: 5
        mySect2: 3
        mySect3: 1
        mySect4: 9

*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2015 Microsoft