There's a style of programming that I stumbled across a while
ago. I don't remember hearing about it anywhere else before, so I
thought I'd write about it here.
I call it "context-based programming." Code is expressed using
terms that are defined by a context, and the user can control the
context and how the terms are defined.
Here's a simple example:
Continue reading "Context-based programming"
What happens if you apply a technique from stock-analysis to software projects?
Continue reading "Stock-analysis applied to software projects"
Let's say that you've got a lot of numbers that represent bitmasks of some kind, and you want to count how many times each bit is on or off across the entire set. Maybe you're analyzing game positions represented as bitboards for an AI, or trying to find certain types of weaknesses in random-number generators, like in Forge (a successor to crypto-js) or Cryptocat (at archive.org) (read the great write-up at Sophos).
So, you write some very straight-forward code to count the bits. It grabs one bitmask. If the lowest-order bit is set, it increments the counter for that bit position. Then, it right-shifts the bitmask and moves to the counter for the next bit. Repeat that for each bit in the mask, then repeat that for each bitmask:
const int N = 1000000;
unsigned long x[N]; // Assuming sizeof(unsigned long) == 8, or 64 bits.
int counts[64] = {0};
void count_simple(void) {
for(int i = 0; i < N; i++) {
int j = 0;
while(x[i] != 0) {
counts[j] += x[i] & 1;
x[i] >>= 1;
++j;
}
}
}
You run your program, and it works correctly, but it's too slow. I'll show you how to speed this up. The technique, which applies to languages like Python or Javascript as well as to C, is both crazy, and crazy-fast!
Continue reading "A crazy fast bit-counting technique"
This last Saturday, I gave a presentation on classification, starting with a basic linear classifier and building it up to k-class, n-dimensional classification. I've been asked for the material I used, so here are the links:
Continue reading "Material from my presentation on Classification"
Sometimes we need to work with numbers that wrap-around, such as
angles or indexes into a circular array. There are some tricks for
dealing with these cases.
Continue reading "Working with wrap-around"