go 运算符


//参考 https://blog.csdn.net/weixin_42100098/article/details/80142523


package main
 
import "fmt"
 
func main()  {
	/*
	 位运算符:将数值转为二进制,按位操作
	&:按位与,都为1才为1,有一个为0就为0
	|:按位或,都为0才为0,有衣蛾为1就为1
	^:异或操作,不同为1,相同为0
	<<:左移, A << B,将A转为二进制,向左移动B位
		相当于A*2的B次方
	>>:右移,A >> B,向右移动B位
	 */
	 a := 60
	 b := 13
 
	 /*
	 60=32+16+8+4
	 13=8+4+1
	 a: 0011 1100
	 b: 0000 1101
	 &: 0000 1100
	 |:0011 1101
	 ^:0011 0001
	  */
	res1 := a & b
	res2 := a | b
	res3 := a ^ b
	 fmt.Println(res1) // 12
	 fmt.Println(res2) // 61
	 fmt.Println(res3) // 49
	 fmt.Printf("%b,%b\n", a, b)
 
	 /*
	 b = 13
	 b:  0000 1101
	 <<2:00 110100  = 32+16+4
	 >>2:0000 0011
	  */
	  res4 := b << 2
	  b = 13
	  res5 := b >> 2
	  fmt.Println(res4, res5)



posted @ 2019-09-24 17:35  nanaindi  阅读(115)  评论(0编辑  收藏  举报