23/06/2009
Last week I showed you the radix sort on simple linked lists. This week, I will present a version of QuickSort modified to sort simply linked lists.
Read the rest of this entry »
2 Comments |
algorithms, bit twiddling, C, C99, data structures, hacks, Mathematics, programming, theoretical computer science, Uncategorized | Tagged: address transformation, box plot, box-and-whiskers, boxplot, comparison sort, gettimeofday, graphs, median, microsecond, quartile, quartiles, Quick Sort, QuickSort, radix, Radix Sort, tests, timer, trials |
Permalink
Posted by Steven Pigeon
09/06/2009
It is not uncommon that for large-scale simulations you need a large volume of high-quality (pseudo)random numbers. The relatively short period of the C standard library’s rand function may make it impractical for your purpose; and you might need to resort to stronger generators such as the Linux-provided /dev/random and /dev/urandom pseudo-devices. But calling those is a relatively expensive process as /dev/random may stall until its “entropy pool” is repleted.
The /dev/urandom is the unblocked version of /dev/random, but it is also demonstrably less random. Moreover, one has to access either pseudo-devices through the file system, which in certain case introduces a noticeable impact on performance. Using other sources of randomness such as a hardware random number generator may also incur significant delays.
To avoid being stalled, I propose a simple method that will allow you to recycle bits from an expensive pseudo-random source without polling it too often.
Read the rest of this entry »
2 Comments |
algorithms, bit twiddling, hacks, Mathematics, theoretical computer science | Tagged: /dev/random, /dev/urandom, bit recycling, Fisher-Yates, hash, md5, pseudo-random, pseudorandom, random, shuffling, urandom |
Permalink
Posted by Steven Pigeon
02/06/2009
A few days ago (again at the time of writing, but since I accumulate and schedule posts for a weekly release, that may already mean a few months ago) a friend was a bit nonplussed by the fact that an expression such as:
int x;
unsigned int y;
if (x+y<0)
{
...
}
was simply never true. At first, you’re thinking “how can this be?” because you’re trying to find values of x and y that, summed together, are obviously negative. That is, without counting on the surprising integer promotion/integral conversion system of C and C++.
Let us see what’s going on exactly.
Read the rest of this entry »
3 Comments |
bit twiddling, C, C99, embedded programming, hacks, Life in the workplace, Portable Code, programming | Tagged: C, C99, integer conversion, integer promotion, integral types, ISO/IEC 14822 |
Permalink
Posted by Steven Pigeon
26/05/2009
Cutting corners is generally thought of as a bad thing. It generally is, I agree. But in some occasions, optimally cutting corners is the right thing to do. I can show you what I mean. Using the Logo programming language (precisely the KTurtle implementation), I devised the following experiment. Consider the following image:

Two circles drawn with KTurtle
Read the rest of this entry »
Leave a Comment » |
algorithms, bit twiddling, hacks, Life in the workplace, Mathematics, programming | Tagged: accuracy, anti-aliasing, antialiasing, approximation, Bézier, Bézier Curves, camera, camera path, de Casteljau, de Casteljau's algorithm, error propagation, KTurtle, Logo, precision, roller-coaster, subpixel, subpixel rendering, Turtle |
Permalink
Posted by Steven Pigeon
19/05/2009
My first true computer, a TI 99/4a (which I talk about also in a previous entry), had 16 or 48 KB of RAM, depending whether or not you had one of the memory expansion cartridges, and that limited quantity of memory severely curbed the complexity of the programs one could write on the machine. However, the limited complexity of the programs, the relative crudeness of the development environment (a BASIC shell) and the slow execution speeds weren’t very obvious to me back then. They were somewhat mitigated by the novelty of the computer itself as a machine, and by the perpetual intense excitement of discovery. The arduous design of programs to save memory, fit more graphics or more code, or even getting our programs to work at all was less about constraints than challenge.
The same kind of constraints—or challenge—followed me over the years as I moved on to different computers. Despite their being more powerful, both faster and sporting more memory, the challenge remained there because while the computers got better, so did I at programming. I kept asking more out of the machine, writing increasingly complex programs needing either more memory or more speed, often both. That meant better algorithms, better data structures, and better implementations1.
Read the rest of this entry »
Leave a Comment » |
algorithms, bit twiddling, data structures, embedded programming, hacks, Life in the workplace, programming, Zen | Tagged: bad design, computer, Daisy Owl, frugal programming, laziness, memory usage, novelty, optimization, TI-99/4A |
Permalink
Posted by Steven Pigeon
27/01/2009
This week, two “quickies”: rounding up and down to the next power of two, and converting efficiently a value to exactly 0 or 1.
Read the rest of this entry »
Leave a Comment » |
algorithms, assembly language, bit twiddling, C, C99, embedded programming, hacks | Tagged: ANSI C, bitwise and, bitwise negation, bitwise operations, rounding down, rounding up, sex, truncating |
Permalink
Posted by Steven Pigeon
02/12/2008
In C and C++, the behavior of integer promotions is not the same whether we use signed integers or unsigned integers. In bit twiddling hacks, it is not immediately apparent that it is a problem that may lead to unexpected result whenever code is ported to a different architecture. This is a recurring topic whenever I discuss portability with friends. Very often, they argue that if it works on Windows and 32 bits x86 Linux, that’s pretty much all there is to it.
Of course, I couldn’t disagree more. I have learned the hard way.
Read the rest of this entry »
2 Comments |
algorithms, bit twiddling, C, C99, hacks, programming | Tagged: C, C99, Constants, Safe Types |
Permalink
Posted by Steven Pigeon
25/11/2008
This week, we’ll be following last week’s post, where we looked at type-safe integer constants, with floating point constants, that is, float and double.
Read the rest of this entry »
Leave a Comment » |
bit twiddling, C, hacks, programming | Tagged: C, C99, double, float, floating point, floating point values |
Permalink
Posted by Steven Pigeon
18/11/2008
Consider the following short C (or C++) program:
const int thingy=123123123123;
Depending on your compiler, the above code may succeed, fail, produce a warning, or be accepted quietly and result in undefined behavior, which is extremely bad.
Read the rest of this entry »
6 Comments |
bit twiddling, C, hacks, programming | Tagged: C, C99, Constants, Safe Types |
Permalink
Posted by Steven Pigeon
12/08/2008
Efficient bit manipulation is often found at the lower levels of many applications, let it be data compression, game programming, graphics manipulations, embedded control, or even efficient data representations, including databases bitmaps and indexes.
In some cases, one might rely on the often limited features of one’s favorite language—C++ in my case—to manipulate bits. In C++ (and in C), there are bit-fields that allow you to define how a piece of memory will be chopped up in regions each having a specific number of bits. Bit-fields can bring you only so far as to describe how bits are mapped onto a chunk of memory; they are of little or no help when you need to efficiently move bits around in a somewhat complicated fashion.
Read the rest of this entry »
8 Comments |
algorithms, bit twiddling, C, hacks, programming | Tagged: bit twiddling, deep arithmetic |
Permalink
Posted by Steven Pigeon