15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Bit arrays ❘ 261<br />

The BitVector32 has an overridden ToString() method that not only displays the name of the class but<br />

also 1 or 0 if the bits are set or not, respectively:<br />

BitVector32{00000000000000000000000000011101}<br />

Instead of creating a mask with the CreateMask() method, you can define the mask yourself; you can also<br />

set multiple bits at once. The hexadecimal value abcdef is the same as the binary value 1010 1011 1100<br />

1101 1110 1111. All the bits defined with this value are set:<br />

bits1[0xabcdef] = true;<br />

Console.WriteLine(bits1);<br />

With the output shown you can verify the bits that are set:<br />

BitVector32{00000000101010111100110111101111}<br />

Separating the 32 bits to different sections can be extremely useful. For example, an IPv4 address is defined<br />

as a 4-byte number that is stored inside an integer. You can split the integer by defining four sections.<br />

With a multicast IP message, several 32-bit values are used. One of these 32-bit values is separated in these<br />

sections: 16 bits for the number of sources, 8 bits for a querier’s query interval code, 3 bits for a querier’s<br />

robustness variable, a 1-bit suppress flag, <strong>and</strong> 4 bits that are reserved. You can also define your own bit<br />

meanings to save memory.<br />

The example simulates receiving the value 0x79abcdef <strong>and</strong> passes this value to the constructor of<br />

BitVector32, so that the bits are set accordingly:<br />

int received = 0x79abcdef;<br />

BitVector32 bits2 = new BitVector32(received);<br />

Console.WriteLine(bits2);<br />

The bits are shown on the console as initialized:<br />

BitVector32{01111001101010111100110111101111}<br />

Then six sections are created. The first section requires 12 bits, as defined by the hexadecimal value 0xfff<br />

(12 bits are set); section B requires 8 bits; section C, 4 bits; section D <strong>and</strong> E, 3 bits; <strong>and</strong> section F, 2 bits.<br />

The first call to CreateSection() just receives 0xfff to allocate the first 12 bits. With the second call to<br />

CreateSection(), the first section is passed as an argument, so that the next section continues where the<br />

first section ended. CreateSection() returns a value of type BitVector32.Section that contains the offset<br />

<strong>and</strong> the mask for the section.<br />

// sections: FF EEE DDD CCCC BBBBBBBB<br />

// AAAAAAAAAAAA<br />

BitVector32.Section sectionA = BitVector32.CreateSection(0xfff);<br />

BitVector32.Section sectionB = BitVector32.CreateSection(0xff, sectionA);<br />

BitVector32.Section sectionC = BitVector32.CreateSection(0xf, sectionB);<br />

BitVector32.Section sectionD = BitVector32.CreateSection(0x7, sectionC);<br />

BitVector32.Section sectionE = BitVector32.CreateSection(0x7, sectionD);<br />

BitVector32.Section sectionF = BitVector32.CreateSection(0x3, sectionE);<br />

Passing a BitVector32.Section to the indexer of the BitVector32 returns an int just mapped to the section<br />

of the bit vector. Here, a helper method, IntToBinaryString(), retrieves a string representation of the<br />

int number:<br />

Console.WriteLine("Section A: {0}",<br />

IntToBinaryString(bits2[sectionA], true));<br />

Console.WriteLine("Section B: {0}",<br />

IntToBinaryString(bits2[sectionB], true));<br />

Console.WriteLine("Section C: {0}",<br />

IntToBinaryString(bits2[sectionC], true));<br />

Console.WriteLine("Section D: {0}",<br />

IntToBinaryString(bits2[sectionD], true));<br />

Console.WriteLine("Section E: {0}",<br />

IntToBinaryString(bits2[sectionE], true));<br />

Console.WriteLine("Section F: {0}",<br />

IntToBinaryString(bits2[sectionF], true));<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!