All pastes #512272 Raw Edit

Miscellany

public text v1 · immutable
#512272 ·published 2007-05-26 20:32 UTC
rendered paste body
import java.util.Random;

class Foo
{
	private static int lookups[] = {
		0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
		1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
		3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
		4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
	};
	
	public static long countBits(byte[] daten) {
		
		long counter = 0;
		
		int len = daten.length;
		
		for (int i=0;i<len;i++) {
			counter += lookups[daten[i] & 0xFF];
		}
		
		return counter;
	}
	
	public static byte[] zufallszahlen(int n)  { 
	
		Random r = new Random();
		
		byte[] temp = new byte[n]; 
		
		for(int i=0; i<n; i++) {
			temp[i] = (byte)(r.nextInt(256)); 
		} 
		
		return temp; 
	}
	
	public static void main(String[] args) {
		
		byte[] megabyte = zufallszahlen(1024*1024*100);
		
		long timeStart = System.currentTimeMillis();
		
		System.out.println(countBits(megabyte));
		
		long timeEnd = System.currentTimeMillis();
		
		System.out.println(timeEnd - timeStart);
	}
}