BitArray originalBits = new BitArray(8); originalBits[0] = true; originalBits[2] = true; BitArray copiedBits = (BitArray)originalBits.Clone(); copiedBits[2] = false; Console.WriteLine("Original bits: {0}", String.Join(",", originalBits.Cast())); Console.WriteLine("Copied bits: {0}", String.Join(",", copiedBits.Cast ()));
BitArray originalBits = new BitArray(8); originalBits[0] = true; originalBits[2] = true; originalBits[4] = true; originalBits[6] = true; BitArray copiedBits = originalBits.Clone() as BitArray; copiedBits.Length = 4; Console.WriteLine("Original bits: {0}", String.Join(",", originalBits.CastThis code creates an 8-bit BitArray object and sets every other bit to true. It then creates a copy of the original object using the Clone() method, and sets the length of the copied object to 4 bits. Finally, it prints out the original and copied BitArrays to show only the first four bits were copied. The BitArray class is part of the System.Collections package library.())); Console.WriteLine("Copied bits: {0}", String.Join(",", copiedBits.Cast ()));