Skip to content

Instantly share code, notes, and snippets.

@tigercallme
Created August 12, 2016 12:32
Show Gist options
  • Save tigercallme/a69d96ecd9b641888af16782b9a4b096 to your computer and use it in GitHub Desktop.
Save tigercallme/a69d96ecd9b641888af16782b9a4b096 to your computer and use it in GitHub Desktop.
package main
//round n up to a multiple of a. a must be a power of 2.
func round(n, a int) int {
return (n + a - 1) &^ (a - 1)
}
func main() {
println(round(3, 4))
println(round(5, 4))
println(round(13, 4))
}
###################################
0110 => 6 1000 => 8 10000 => 16
0011 => 3 0011 => 3 00011 => 3
---------- ---------- -------
0100 => 4 1000 => 8 10000 =>16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment