Skip to content

Instantly share code, notes, and snippets.

@edmccard
Created May 13, 2015 15:54
Show Gist options
  • Save edmccard/a7fe5726a25961104068 to your computer and use it in GitHub Desktop.
Save edmccard/a7fe5726a25961104068 to your computer and use it in GitHub Desktop.
func checkOverflow(result, op1, op2 uint8) bool {
// (op1 ^ op2) & 0x80: zero if both operands had the same sign bit
// (op1 ^ result) & 0x80: non-zero if the result is a different sign
// from the operands
// So it returns true if either:
// both operands are positive but the result is negative
// or vice-versa
return (((op1 ^ op2) & 0x80) == 0) && (((op1 ^ result) & 0x80) != 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment