Skip to content

Instantly share code, notes, and snippets.

@koonix
Last active January 7, 2025 23:44
Show Gist options
  • Save koonix/b2067fe699ec87be44a1081f22235773 to your computer and use it in GitHub Desktop.
Save koonix/b2067fe699ec87be44a1081f22235773 to your computer and use it in GitHub Desktop.
Go 64-bit multiplication and division without overflow.
import "math/bits"
func mulDiv(a, b, c int64) int64 {
hi, lo := bits.Mul64(uint64(a), uint64(b))
quo, _ := bits.Div64(hi, lo, uint64(c))
return int64(quo)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment