Skip to content

Instantly share code, notes, and snippets.

@caelifer
Created October 20, 2016 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caelifer/7f8cba8969bcb52efcbd0ce541de7c55 to your computer and use it in GitHub Desktop.
Save caelifer/7f8cba8969bcb52efcbd0ce541de7c55 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func main() {
fmt.Println("Sz", "Pg", "Bn", "Pad")
pageSize := 1 << 5
for _, off := range []int{10, 15, 16, 31, 33} {
bound := alignBound(off, pageSize)
fmt.Println(off, pageSize, bound, bound-off)
}
}
func alignBound(off, page int) int {
return (off + page - 1) & ^(page - 1)
}
@caelifer
Copy link
Author

Live code - https://play.golang.org/p/I51R7D5Dnd
Output:

Sz Pg Bn Pad
10 32 32 22
15 32 32 17
16 32 32 16
31 32 32 1
33 32 64 31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment