* Mark Dickinson: >>> Any suggestions for improvements over this? >> >> Use -fwrapv and your first version. > > gcc isn't the only compiler that's going to have to compile > this code, so it still needs to be fixed to avoid undefined > behaviour Most compiler support something like -fwrapv. > On Sun, Nov 29, 2009 at 9:00 PM, me22 <me22.ca@xxxxxxxxx> wrote: >> What about using (long)((unsigned long)a + (unsigned long)b) or >> something to get around the UB? > > Yep, that looks like the solution. Thanks! You'd also have to compare against (1 << (sizeof(long) * CHAR_BITS - 1)) instead of 0, pessimizing the code somewhat.