]> Cypherpunks repositories - gostls13.git/commitdiff
math/bits: add examples for right rotation
authorTobias Klauser <tklauser@distanz.ch>
Thu, 2 Nov 2017 10:19:23 +0000 (11:19 +0100)
committerRobert Griesemer <gri@golang.org>
Fri, 3 Nov 2017 20:12:07 +0000 (20:12 +0000)
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: Robert Griesemer <gri@golang.org>
src/math/bits/example_test.go
src/math/bits/make_examples.go

index dd400da0fec5984a608db0dc5b89305d3b86c9ef..18e026b9b466525a410706686a734127d06bbe7f 100644 (file)
@@ -86,33 +86,41 @@ func ExampleOnesCount64() {
 func ExampleRotateLeft8() {
        fmt.Printf("%08b\n", 15)
        fmt.Printf("%08b\n", bits.RotateLeft8(15, 2))
+       fmt.Printf("%08b\n", bits.RotateLeft8(15, -2))
        // Output:
        // 00001111
        // 00111100
+       // 11000011
 }
 
 func ExampleRotateLeft16() {
        fmt.Printf("%016b\n", 15)
        fmt.Printf("%016b\n", bits.RotateLeft16(15, 2))
+       fmt.Printf("%016b\n", bits.RotateLeft16(15, -2))
        // Output:
        // 0000000000001111
        // 0000000000111100
+       // 1100000000000011
 }
 
 func ExampleRotateLeft32() {
        fmt.Printf("%032b\n", 15)
        fmt.Printf("%032b\n", bits.RotateLeft32(15, 2))
+       fmt.Printf("%032b\n", bits.RotateLeft32(15, -2))
        // Output:
        // 00000000000000000000000000001111
        // 00000000000000000000000000111100
+       // 11000000000000000000000000000011
 }
 
 func ExampleRotateLeft64() {
        fmt.Printf("%064b\n", 15)
        fmt.Printf("%064b\n", bits.RotateLeft64(15, 2))
+       fmt.Printf("%064b\n", bits.RotateLeft64(15, -2))
        // Output:
        // 0000000000000000000000000000000000000000000000000000000000001111
        // 0000000000000000000000000000000000000000000000000000000000111100
+       // 1100000000000000000000000000000000000000000000000000000000000011
 }
 
 func ExampleReverse8() {
index ac537d5778dd6a40438dc26c7b482ac2220e1674..cd81cd6c4dbf95fbe1a7c9aa025abca74f204528 100644 (file)
@@ -37,6 +37,7 @@ func main() {
                name string
                in   int
                out  [4]interface{}
+               out2 [4]interface{}
        }{
                {
                        name: "LeadingZeros",
@@ -57,6 +58,7 @@ func main() {
                        name: "RotateLeft",
                        in:   15,
                        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",
@@ -85,12 +87,16 @@ func main() {
                                fmt.Fprintf(w, "\tfmt.Printf(\"%%0%db\\n\", %d)\n", size, e.in)
                                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)
                                } else {
                                        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// %0*b\n", size, e.in)
                                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:
                                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")