「Go言語」演算子(論理積,論理和,ビット左にシフト,ビット右にシフト)のサンプル
書式
x & y 論理積(AND)
x | y 論理和(OR)
x ^ y 排他的論理和(XOR)
x &^ y x AND (NOT y)
x << y yビット左にシフト
x >> y yビット右にシフト
使用例
package main import "fmt" func main() { var a uint = 60 /* 60 = 0011 1100 */ var b uint = 13 /* 13 = 0000 1101 */ var c uint = 0 c = a & b /* 12 = 0000 1100 */ fmt.Printf("line 1 - c value is %d\n", c ) c = a | b /* 61 = 0011 1101 */ fmt.Printf("line 2 - c value is %d\n", c ) c = a ^ b /* 49 = 0011 0001 */ fmt.Printf("line 3 - c value is %d\n", c ) c = a << 2 /* 240 = 1111 0000 */ fmt.Printf("line 4 - c value is %d\n", c ) c = a >> 2 /* 15 = 0000 1111 */ fmt.Printf("line 5 - c value is %d\n", c ) }
実行結果
>go run test.go
line 1 – c value is 12
line 2 – c value is 61
line 3 – c value is 49
line 4 – c value is 240
line 5 – c value is 15