Created
April 30, 2012 08:01
-
-
Save AndrewBarfield/2556400 to your computer and use it in GitHub Desktop.
C#: System.Collections: Display binary values using BitArray class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
namespace BitArrayExample { | |
class Program { | |
static void Main(string[] args) { | |
bool le = BitConverter.IsLittleEndian; | |
if ( le ) { | |
Console.WriteLine( "Output is Little Endian" ); | |
} | |
else { | |
Console.WriteLine( "Output is Big Endian" ); | |
} | |
Console.WriteLine(); | |
// Creates and initializes several BitArrays. | |
byte[] myBytes = new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | |
BitArray bitArray = new BitArray( myBytes ); | |
// Displays the properties and values of the BitArrays. | |
Console.WriteLine( "bitArray Count: {0}", bitArray.Count ); | |
Console.WriteLine( "bitArray Length: {0}", bitArray.Length ); | |
Console.WriteLine(); | |
// Displays 1 to 10 in binary | |
Console.WriteLine( "1 to 10 in binary:" ); | |
int i = 8; | |
foreach ( Object obj in bitArray ) { | |
if ( i <= 0 ) { | |
i = 8; | |
Console.WriteLine(); | |
} | |
i--; | |
Console.Write( "{0}", (bool)obj == true ? "1" : "0" ); | |
} | |
// Allow the user to see the output | |
Console.Read(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment