-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrevbytes.go
59 lines (51 loc) · 815 Bytes
/
revbytes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
)
func main() {
flag.Usage = usage
flag.Parse()
var r io.Reader
if flag.NArg() < 1 {
b, err := io.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
r = bytes.NewBuffer(b)
} else {
s := strings.Join(flag.Args(), " ")
r = strings.NewReader(s)
}
for {
var v uint64
_, err := fmt.Fscanf(r, "%v", &v)
if err != nil {
break
}
fmt.Printf("%#x\n", v)
for i := uint(8); i <= 64; i *= 2 {
fmt.Printf("rev%d %#x\n", i, rev(v, i))
}
fmt.Printf("\n")
}
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: <value> ...")
flag.PrintDefaults()
os.Exit(2)
}
func rev(v uint64, n uint) uint64 {
r := uint64(0)
for i := uint(0); i < n; i++ {
if v&(1<<(n-i-1)) != 0 {
r |= 1 << i
}
}
return r
}