Skip to content

Commit 8ca2456

Browse files
authored
Merge pull request #252 from devlights/add-bitop-example
Add examples/basic/bitop directory & Add example
2 parents 20963bb + 1a23a13 commit 8ca2456

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

examples/basic/bitop/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# サンプルリスト
2+
3+
このディレクトリには以下のサンプルがあります。
4+
5+
|file|example name|note|
6+
|----|------------|----|
7+
|bitop\_basic.go|bitop\_basic|基本的なビット操作のサンプルです.|
8+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package bitop
2+
3+
import (
4+
"github.com/devlights/gomy/output"
5+
)
6+
7+
// Basic -- 基本的なビット操作のサンプルです.
8+
//
9+
// REFERENCES:
10+
// - 書籍「プログラミング言語 Go」-- P.60
11+
// - https://yourbasic.org/golang/bitwise-operator-cheat-sheet/
12+
func Basic() error {
13+
// -----------------------------------------------------
14+
// ビット演算は集合演算と思うと理解しやすい.
15+
//
16+
// & -- AND , 共通部分
17+
// | -- OR , 和
18+
// ^ -- XOR , 対称差 (どちらか一方しか持っていないもの)
19+
// &^ -- AND NOT, 差 (自分しか持っていないもの)、ビットクリア
20+
// -----------------------------------------------------
21+
22+
var (
23+
x uint8 = 1<<0 | 1<<1 | 1<<5 // 00100011
24+
y uint8 = 1<<1 | 1<<3 // 00001010
25+
)
26+
27+
// フォーマットのverbに %b を指定すると2進数表記となる
28+
// さらに 08 を付与して、8桁表示でゼロ埋めするように指示
29+
//
30+
// 補足) %#08b とすると, 先頭に 0b を付与して出力してくれる
31+
output.Stdoutf("[x]", "%08b\n", x)
32+
output.Stdoutf("[y]", "%08b\n", y)
33+
34+
output.StdoutHr()
35+
36+
// & は共通部分を取り出す。両方 1 のものが残る
37+
output.Stdoutf("[x&y ][共通部分]", "%08b\n", x&y)
38+
39+
// | は和を取り出す。どちらか 1 のものが残る
40+
output.Stdoutf("[x|y ][和   ]", "%08b\n", x|y)
41+
42+
// ^ は対称差を取り出す。どちらか片方しか 1 のものが残る.
43+
output.Stdoutf("[x^y ][対称差 ]", "%08b\n", x^y)
44+
45+
// &^ は差を取り出す。 &^ はビットクリアともいう.
46+
// x&^yは、xの個々のビットは対応するyのビットが1の場合に0となる。
47+
// それ以外は、対応するxのビットがそのまま残る.
48+
//
49+
// 例) logパッケージの、デフォルトのフラグ設定をクリアする際に
50+
// log.SetFlags(log.Flags &^ log.LstdFlags)
51+
// としてビットクリアすると、一発でクリアできる
52+
// (デフォルトで設定されているフラグ LstdFlags なので)
53+
output.Stdoutf("[x&^y][差   ]", "%08b\n", x&^y)
54+
55+
output.StdoutHr()
56+
57+
// 特定のビットが立っているか検査
58+
if x&(1<<5) != 0 {
59+
output.Stdoutl("[x&(1<<5)]", "ON")
60+
}
61+
62+
output.StdoutHr()
63+
64+
// 特定のビットを落とす
65+
x &^= (1 << 5)
66+
output.Stdoutf("[x &^= (1 << 5)]", "%08b\n", x)
67+
68+
// 特定のビットを立てる
69+
x |= (1 << 5)
70+
output.Stdoutf("[x != (1 << 5)]", "%08b\n", x)
71+
72+
output.StdoutHr()
73+
74+
// 特定のビットをトグル
75+
x ^= (1 << 7)
76+
output.Stdoutf("[x ^= (1 << 7)]", "%08b\n", x)
77+
x ^= (1 << 7)
78+
output.Stdoutf("[x ^= (1 << 7)]", "%08b\n", x)
79+
80+
output.StdoutHr()
81+
82+
// 自分の全ビットを反転
83+
output.Stdoutf("[^x]", "%08b\n", ^x)
84+
85+
return nil
86+
}

examples/basic/bitop/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Package bitop -- ビット操作についてのサンプルが配置されています.
3+
*/
4+
package bitop

examples/basic/bitop/examples.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package bitop
2+
3+
import (
4+
"github.com/devlights/try-golang/mappings"
5+
)
6+
7+
type (
8+
register struct{}
9+
)
10+
11+
// NewRegister -- このパッケージ用のサンプルを登録する mappings.Register を生成します。
12+
func NewRegister() mappings.Register {
13+
return new(register)
14+
}
15+
16+
// Regist -- 登録します.
17+
func (r *register) Regist(m mappings.ExampleMapping) {
18+
m["bitop_basic"] = Basic
19+
}

examples/basic/examples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package basic
33
import (
44
"github.com/devlights/try-golang/examples/basic/array"
55
"github.com/devlights/try-golang/examples/basic/binaries"
6+
"github.com/devlights/try-golang/examples/basic/bitop"
67
"github.com/devlights/try-golang/examples/basic/builtins"
78
"github.com/devlights/try-golang/examples/basic/byteop"
89
"github.com/devlights/try-golang/examples/basic/comments"
@@ -56,6 +57,7 @@ func (r *register) Regist(m mappings.ExampleMapping) {
5657

5758
array.NewRegister().Regist(m)
5859
binaries.NewRegister().Regist(m)
60+
bitop.NewRegister().Regist(m)
5961
builtins.NewRegister().Regist(m)
6062
byteop.NewRegister().Regist(m)
6163
comments.NewRegister().Regist(m)

0 commit comments

Comments
 (0)