March 5, 2013
Numerical methods are generally rather hard to get right because of error propagation due to the limited precision of floats (or even doubles). This seems to be especially true with methods involving series, where a usually large number of ever diminishing terms must added to attain something that looks like convergence. Even fairly simple sequences such as

may be complex to evaluate. First,
is cumbersome, and
becomes small exceedingly rapidly.
Read the rest of this entry »
2 Comments |
algorithms, bit twiddling, hacks, Mathematics | Tagged: convergence, e, Euler, series |
Permalink
Posted by Steven Pigeon
July 31, 2012
64 bits address space lets us access tons more memory than 32 bits, but with a catch: the pointers themselves are … well, yes, 64 bits. 8 bytes. Which eventually pile up to make a whole lot of memory devoted to pointers if you use pointer-rich data structures. Can we do something about this?

Well, in ye goode olde dayes of 16 bits/32 bits computing, we had some compilers that could deal with near and far pointers; the near, 16-bit pointers being relative to one of the segments, possibly the stack segment, and the far, 32-bits pointers being absolute or relative to a segment. This, of course, made programming pointlessly complicated as each pointer was to be used in its correct context to point to the right thing.
Read the rest of this entry »
Leave a Comment » |
bit twiddling, C, C-plus-plus, hacks | Tagged: 32 bits, 64 bits, AMD64, Far Pointer, Near Pointer, optimization, Pointer, Pointer Arithmetic |
Permalink
Posted by Steven Pigeon
March 13, 2012
As part of an open-source project I’m working on (right now, we are still at the technical feasibility stage where we explore and eliminate technical risks, full disclosure will come later) we have to issue session numbers. They’re not session numbers in the usual sense, but they still need to be unique, and not amenable to simple attacks.

There are a couple of ways of generating unique session numbers. RFC 4122-compliant unique IDs is one possible way.
Read the rest of this entry »
2 Comments |
algorithms, bit twiddling, hacks, programming, Python | Tagged: RFC 4122, session, session id, SSL, urandom, UUID |
Permalink
Posted by Steven Pigeon
November 22, 2011
On a number of different occasions, I briefly discussed Hash Functions, saying that if a hash function needn’t be very strong if the intend is to use it for look-up, contrary to cryptographic applications. But, unsurprisingly, it’s rather difficult to get a good fast hash function.

Coming up with a very good hash function isn’t easy, but we can at least make an effort to build one by understanding what makes a better hash function, especially by explicitly testing them. Let us have a try at building a good hash function (for look-up) from scratch.
Read the rest of this entry »
Leave a Comment » |
algorithms, bit twiddling, C-plus-plus, data structures, Mathematics, programming | Tagged: hash, hash function, Hash Map, hashes for look-up, hashing, secure hashes |
Permalink
Posted by Steven Pigeon
November 8, 2011
Sometimes, you have a small bit of data, may something like a GUID (for which there are many possible solutions), that you may have to store in a plain-text file, nothing crucial, not sensitive, but that you don’t really want your users to poke with, even if they really mean to. In such cases, you could use encryption, but it may be that mild obfuscation is quite sufficient and dissuasive.

So, if you don’t really want strong encryption, what can you do to provide a machine-efficient encryptionnette?
Read the rest of this entry »
3 Comments |
algorithms, bit twiddling, C, C-plus-plus, hacks, Mathematics, programming, Python | Tagged: bit blenders, bit twiddling, Encryption, MAC address, Modular Arithmetic, xor |
Permalink
Posted by Steven Pigeon
November 1, 2011
Some time ago, I discussed Huffman codes, how they were generated, and how you could (de)code information with it. I also said that they were optimal under various conditions, one of which (that I may or may not have mentioned) is that you have an integer number of bits.

Coding with an non-integer number of bits is counter-intuitive, but it is entirely possible to do so. There are in fact many ways to do so, but let’s start easy and ignore the frequency of occurrence of symbols for now.
Read the rest of this entry »
Leave a Comment » |
algorithms, bit twiddling, data compression | Tagged: binary encoding, Entropy, entropy coding, Huffman Codes, phase-in codes |
Permalink
Posted by Steven Pigeon
September 27, 2011
Sometimes you have to encode reversibly two (or more) values onto a single one. The typical example of a pairing function that encodes two non-negative integers onto a single non-negative integer (therefore a function
) is the Cantor function, instrumental to the demonstration that, for example, the rational can be mapped onto the integers.

In a more pragmatic way, it may be necessary to encode two values onto one for data compression purposes, or to otherwise exploit a protocol that does not allow many distinct values to be sent separately. The simplest way of pairing two integer is to interleave their bits, for example, with something like:
Read the rest of this entry »
7 Comments |
algorithms, bit twiddling, C, hacks, Mathematics, programming, Python | Tagged: Gödel, Gödel Number, Gödel Numbering, Integers, pairing function |
Permalink
Posted by Steven Pigeon
August 30, 2011
Quite a while ago, I discussed map generation in the classic 80s-era game Tunnels of Doom. After a short correspondence with Kevin Kenney himself (who kindly answered my questions; and I hope he is aware that he contributed to the fascination of great many kids in computer science), I manage to, not exactly reproduce his algorithm, but create a number of fun variations.

Tunnels of Doom Combat Screen.
This raises the question as to how do we encode maps efficiently in the computer’s memory, not only for Tunnels of Doooooom but also for any number of other games.
Read the rest of this entry »
Leave a Comment » |
algorithms, bit twiddling, data compression, data structures, embedded programming, hacks, programming | Tagged: Dragon Warrior, Map, Map representation, Quad-Tree, Tunnels of Doom, Wumpus |
Permalink
Posted by Steven Pigeon
August 9, 2011
A few years ago, I posted on my personal web page a number of programming challenges for my friends. This week, I present one of the old challenges: computing luma from RGB triplets using integer arithmetic only.

Read the rest of this entry »
36 Comments |
algorithms, bit twiddling, C, embedded programming, hacks, programming | Tagged: Arithmetic, Integer, Luma, rgb |
Permalink
Posted by Steven Pigeon
July 5, 2011
I’m always playing around with data compression ideas, and I recently came across a particular problem that inspired this week’s entry about implicit coding.

The idea of implicit coding is to exploit some constraint in the data that lets you reconstruct one (or more) value(s) unambiguously from the others. The textbook example of implicit coding is to use a constraint such as a known sum. For example, If you have a right triangle you necessarily have that
Read the rest of this entry »
1 Comment |
algorithms, bit twiddling, data compression, data structures | Tagged: Constrained equations, implicit coding, Pythagoras, rematerialization, triangle |
Permalink
Posted by Steven Pigeon