Jens Gustedt's Blog

October 23, 2010

A generic swap implementation

Filed under: C99, integers, preprocessor — Jens Gustedt @ 11:13

Swapping the contents of two variable is an elementary task that is often met in daily programming. There are two generic strategies to do that for general types.


October 18, 2010

Detecting integer overflow II

Filed under: C99, integers — Jens Gustedt @ 22:21

In an earlier post we came up with a general solution to check for potential under- or overflow in an integer addition. On most modern architectures this can be done more efficiently, even when assuming that there are no special instructions that capture overflow bits or such.

October 16, 2010

Detecting integer overflow I

Filed under: C99, integers — Jens Gustedt @ 10:15

A recent discussion on stackoverflow has shown that detecting integer overflow without provoking undefined behavior need some reflection, and that the quick answers are not necessarily the best ones.

Blog at