Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created July 19, 2011 14:05
Show Gist options
  • Save bnoordhuis/1092464 to your computer and use it in GitHub Desktop.
Save bnoordhuis/1092464 to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <limits.h>
uint64_t uint64_add(uint64_t a, uint64_t b, int* overflow) {
uint64_t c;
if (UINT64_MAX - a >= b) {
*overflow = 0;
return a + b;
}
c = a + b;
*overflow = 1 + (c > a);
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment