Commit 89bcbf40 authored by Tobias Klauser's avatar Tobias Klauser Committed by Robert Griesemer

math/bits: add examples for right rotation

Right rotation is achieved using negative k in RotateLeft*(x, k). Add
examples demonstrating that functionality.

Change-Id: I15dab159accd2937cb18d3fa8ca32da8501567d3
Reviewed-on: https://go-review.googlesource.com/75371
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 483e298d
...@@ -86,33 +86,41 @@ func ExampleOnesCount64() { ...@@ -86,33 +86,41 @@ func ExampleOnesCount64() {
func ExampleRotateLeft8() { func ExampleRotateLeft8() {
fmt.Printf("%08b\n", 15) fmt.Printf("%08b\n", 15)
fmt.Printf("%08b\n", bits.RotateLeft8(15, 2)) fmt.Printf("%08b\n", bits.RotateLeft8(15, 2))
fmt.Printf("%08b\n", bits.RotateLeft8(15, -2))
// Output: // Output:
// 00001111 // 00001111
// 00111100 // 00111100
// 11000011
} }
func ExampleRotateLeft16() { func ExampleRotateLeft16() {
fmt.Printf("%016b\n", 15) fmt.Printf("%016b\n", 15)
fmt.Printf("%016b\n", bits.RotateLeft16(15, 2)) fmt.Printf("%016b\n", bits.RotateLeft16(15, 2))
fmt.Printf("%016b\n", bits.RotateLeft16(15, -2))
// Output: // Output:
// 0000000000001111 // 0000000000001111
// 0000000000111100 // 0000000000111100
// 1100000000000011
} }
func ExampleRotateLeft32() { func ExampleRotateLeft32() {
fmt.Printf("%032b\n", 15) fmt.Printf("%032b\n", 15)
fmt.Printf("%032b\n", bits.RotateLeft32(15, 2)) fmt.Printf("%032b\n", bits.RotateLeft32(15, 2))
fmt.Printf("%032b\n", bits.RotateLeft32(15, -2))
// Output: // Output:
// 00000000000000000000000000001111 // 00000000000000000000000000001111
// 00000000000000000000000000111100 // 00000000000000000000000000111100
// 11000000000000000000000000000011
} }
func ExampleRotateLeft64() { func ExampleRotateLeft64() {
fmt.Printf("%064b\n", 15) fmt.Printf("%064b\n", 15)
fmt.Printf("%064b\n", bits.RotateLeft64(15, 2)) fmt.Printf("%064b\n", bits.RotateLeft64(15, 2))
fmt.Printf("%064b\n", bits.RotateLeft64(15, -2))
// Output: // Output:
// 0000000000000000000000000000000000000000000000000000000000001111 // 0000000000000000000000000000000000000000000000000000000000001111
// 0000000000000000000000000000000000000000000000000000000000111100 // 0000000000000000000000000000000000000000000000000000000000111100
// 1100000000000000000000000000000000000000000000000000000000000011
} }
func ExampleReverse8() { func ExampleReverse8() {
......
...@@ -37,6 +37,7 @@ func main() { ...@@ -37,6 +37,7 @@ func main() {
name string name string
in int in int
out [4]interface{} out [4]interface{}
out2 [4]interface{}
}{ }{
{ {
name: "LeadingZeros", name: "LeadingZeros",
...@@ -57,6 +58,7 @@ func main() { ...@@ -57,6 +58,7 @@ func main() {
name: "RotateLeft", name: "RotateLeft",
in: 15, in: 15,
out: [4]interface{}{bits.RotateLeft8(15, 2), bits.RotateLeft16(15, 2), bits.RotateLeft32(15, 2), bits.RotateLeft64(15, 2)}, out: [4]interface{}{bits.RotateLeft8(15, 2), bits.RotateLeft16(15, 2), bits.RotateLeft32(15, 2), bits.RotateLeft64(15, 2)},
out2: [4]interface{}{bits.RotateLeft8(15, -2), bits.RotateLeft16(15, -2), bits.RotateLeft32(15, -2), bits.RotateLeft64(15, -2)},
}, },
{ {
name: "Reverse", name: "Reverse",
...@@ -85,12 +87,16 @@ func main() { ...@@ -85,12 +87,16 @@ func main() {
fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", %d)\n", size, e.in) fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", %d)\n", size, e.in)
if e.name == "RotateLeft" { if e.name == "RotateLeft" {
fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", bits.%s(%d, 2))\n", size, f, e.in) fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", bits.%s(%d, 2))\n", size, f, e.in)
fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", bits.%s(%d, -2))\n", size, f, e.in)
} else { } else {
fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", bits.%s(%d))\n", size, f, e.in) fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", bits.%s(%d))\n", size, f, e.in)
} }
fmt.Fprintf(w, "\t// Output:\n") fmt.Fprintf(w, "\t// Output:\n")
fmt.Fprintf(w, "\t// %0*b\n", size, e.in) fmt.Fprintf(w, "\t// %0*b\n", size, e.in)
fmt.Fprintf(w, "\t// %0*b\n", size, e.out[i]) fmt.Fprintf(w, "\t// %0*b\n", size, e.out[i])
if e.name == "RotateLeft" && e.out2[i] != nil {
fmt.Fprintf(w, "\t// %0*b\n", size, e.out2[i])
}
default: default:
fmt.Fprintf(w, "\tfmt.Printf(\"%s(%%0%db) = %%d\\n\", %d, bits.%s(%d))\n", f, size, e.in, f, e.in) fmt.Fprintf(w, "\tfmt.Printf(\"%s(%%0%db) = %%d\\n\", %d, bits.%s(%d))\n", f, size, e.in, f, e.in)
fmt.Fprintf(w, "\t// Output:\n") fmt.Fprintf(w, "\t// Output:\n")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment