-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
54 lines (52 loc) · 889 Bytes
/
Copy pathmain_test.go
File metadata and controls
54 lines (52 loc) · 889 Bytes
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
// Other: How to print the binary representation of an integer
package main
import (
"fmt"
)
func Example() {
// The %b verb print the binary representation of an integer
for x := 0; x < 16; x++ {
fmt.Printf("%b\n", x)
}
// If you want the output to have the same length, 8 bits for example
// use the %08b notation which means:
// print binary, use 8 digits for the output, pad with leading zeros
fmt.Println("fixed length of 8 digits:")
for x := 0; x < 16; x++ {
fmt.Printf("%08b\n", x)
}
// Output:
// 0
// 1
// 10
// 11
// 100
// 101
// 110
// 111
// 1000
// 1001
// 1010
// 1011
// 1100
// 1101
// 1110
// 1111
// fixed length of 8 digits:
// 00000000
// 00000001
// 00000010
// 00000011
// 00000100
// 00000101
// 00000110
// 00000111
// 00001000
// 00001001
// 00001010
// 00001011
// 00001100
// 00001101
// 00001110
// 00001111
}