Home
Manage Your Code
Snippet: BitArray Class with Console.SetCursorPosition(x,y) (C#)
Title: BitArray Class with Console.SetCursorPosition(x,y) Language: C#
Description: It is a displaying of BitArray data in specific postion at console windows. In addition, the method of C#'s Console.SetCursorPosition(x,y) is equivalent to C++ gotoxy(x,y). Views: 97
Author: Juan Rodrigo Villanueva Date Added: 5/14/2008
Copy Code  
1static void Main(string[] args)
2        {
3            //fixed size for BitArray

4            int bitSize = 4;
5
6            BitArray bits = new BitArray(bitSize);
7            bits[0] = true;
8            bits[1] = true;
9            bits[2] = false;
10            bits[3] = false;
11            //Display values for bits

12            PrintValues(bits, 1, "bit");
13
14            BitArray moreBits = new BitArray(bitSize);
15            moreBits[0] = true;
16            moreBits[1] = false;
17            moreBits[2] = true;
18            moreBits[3] = false;
19            //Display values for moreBits

20            PrintValues(moreBits, 10, "moreBit");
21
22            //Compare two BitArray with xor method

23            BitArray xorBits = bits.Xor(moreBits);
24
25            //Display the result of xor

26            PrintValues(bits, 25, "Result xor");
27            
28            Console.Read();
29        }
30
31        public static void PrintValues(IEnumerable myList, int myWidth, string bitName)
32        {          
33
34            //Starting position

35            int i = 0;
36            int w = myWidth;
37
38            Console.SetCursorPosition(w, i);
39            //Display header

40            Console.WriteLine("\"{0}\" \n", bitName);
41
42            //increment 1 to place in the next line

43            i++;
44
45            foreach (object obj in myList)
46            {     
47                //Cursor position after the header

48                Console.SetCursorPosition(w, i);
49                //Display data

50                Console.Write("{0}", obj);
51                //increment i to move in the next line

52                i++;
53            }
54        }