Skip to content

Instantly share code, notes, and snippets.

@jprupp
jprupp / double.cpp
Created May 15, 2012 12:24
Double precision floating point disassembly/reassembly analyser
#include <cassert>
#include <cmath>
#include <iostream>
using namespace std;
void printbytes(double d) {
unsigned long long int* pi = (unsigned long long int*) &d;
unsigned long long int i = *pi;
short int e = (i >> 52 & 0x7ff) - 1023; // Exponent
unsigned long long int m = (i << 12 >> 12) + pow(2, 52); // Mantissa
@jprupp
jprupp / fib.hs
Created May 5, 2012 12:28
Fibonacci in Haskell
fib :: Int -> Int
fib x
| x < 0 = error ("Can't calculate fib of negative number " ++ show x)
| x == 0 = 0
| x > 0 = accum (x-1) 0 1
where
accum :: Int -> Int -> Int -> Int
accum x y z
| x > 0 = accum (x-1) z (y+z)
| otherwise = z
@jprupp
jprupp / quicksort.hs
Created May 5, 2012 08:37
Haskell Quicksort
split :: Ord a => a -> [a] -> ([a],[a])
split x ys = accum x ys ([],[])
where
accum :: Ord a => a -> [a] -> ([a],[a]) -> ([a],[a])
accum _ [] result = result
accum x (y:ys) (ls,rs)
| y > x = accum x ys (ls,y:rs)
| otherwise = accum x ys (y:ls,rs)
sort :: Ord a => [a] -> [a]
@jprupp
jprupp / popcount.pl
Created March 15, 2012 15:32
popcount() in Perl
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use List::Util qw(reduce);
my @masks = map {
my $group_bin = 0 x $_ . 1 x $_;
my $bin_length = $_ * 2;
my $mask_bin = $group_bin x ( 32 / $bin_length );