Skip to content

Instantly share code, notes, and snippets.

@Measter
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save Measter/7d7578b4b72f3d23f33b to your computer and use it in GitHub Desktop.

Select an option

Save Measter/7d7578b4b72f3d23f33b to your computer and use it in GitHub Desktop.
BitCounter
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