Last active
August 29, 2015 14:15
-
-
Save Measter/7d7578b4b72f3d23f33b to your computer and use it in GitHub Desktop.
BitCounter
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.Generic; | |
| using System.IO; | |
| namespace ConsoleTestApp | |
| { | |
| class Program | |
| { | |
| private static Dictionary<int, Decimal> m_count = new Dictionary<int, Decimal>(); | |
| private static void Main( string[] args ) | |
| { | |
| m_count[0] = 0; | |
| m_count[1] = 0; | |
| byte[] buffer = new byte[1024]; | |
| int readLen; | |
| var files = Directory.GetFiles( Environment.CurrentDirectory ); | |
| foreach( var f in files ) | |
| { | |
| using( FileStream fs = new FileStream( f, FileMode.Open, FileAccess.Read ) ) | |
| { | |
| while ( (readLen = fs.Read( buffer, 0, buffer.Length )) > 0 ) | |
| { | |
| for ( int i = 0; i < readLen; i++ ) | |
| { | |
| CountBits( buffer[i] ); | |
| } | |
| } | |
| } | |
| } | |
| Decimal total = m_count[0] + m_count[1]; | |
| Console.WriteLine( "Percent 0: {0}", ( m_count[0] / total ) * 100 ); | |
| Console.WriteLine( "Percent 1: {0}", ( m_count[1] / total ) * 100 ); | |
| } | |
| private static void CountBits( int num ) | |
| { | |
| for( int i = 0; i < 8; i++ ) | |
| { | |
| int bit = num & 1; | |
| m_count[bit]++; | |
| num >>= 1; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment