1. 首页
  2. 主题
  3. Go语言

位运算符 &^ 能拆分其他位运算符表示吗?因为这个位运算符其他语言C还有rust都没有。

wandercn · · 1602 次点击
&^ 这个运算符最早是没有的 测试代码: ``` package main import "fmt" const ( hasMonotonic = 1 << 63 nsecMask = 1<<30 - 1 nsecShift = 30 ) func main() { sec := 1632376722 nsec := 2324512 ynsec := uint64(nsec) ysec := uint64(sec) << nsecShift wall := hasMonotonic | uint64(sec)<<nsecShift | uint64(nsec) fmt.Printf("%b\n", ysec) fmt.Printf("%b\n", ynsec) D := int64(3600 * 10e9) oldnsec := wall & nsecMask fmt.Printf("%v\n", oldnsec) newNsec := int32(oldnsec) + int32(D%10e9) fmt.Printf("%v\n", newNsec) fmt.Printf("wall :%b\n", wall) fmt.Printf("nsecMask. :%b\n", nsecMask) fmt.Printf("wall_move :%b\n", wall>>30<<30) fmt.Printf("wall&^nsecMask:%b\n", wall&^nsecMask) wall1 := wall&^nsecMask | uint64(nsec) fmt.Printf("wall1:%b\n", wall1) wall2 := wall>>30<<30 | uint64(nsec) fmt.Printf("wall2:%b\n", wall2) fmt.Printf("equal:%v\n", wall1 == wall2) } ``` 以上代码我是变通处理,因为wall1 := wall&^nsecMask | uint64(nsec) 中 nsecMask 二进制是全1的,我直接用位移运算>>30<<30代替了。计算结果也是一样的。 但是如果不是 比如 1000000011000000000,就没法处理了。有人知道怎么拆分用& | ^其他语言也有的位运算符来达到 &^一样的结果?
czyt
云在青天水在瓶
这个操作叫做 bit clear ,搜了下,希望对你有所帮助。 1、Setting a bit. Use the bitwise OR operator ( | ) to set a bit. number |= 1 &lt;&lt; x; That will set a bit x . 2、Clearing a bit. Use the bitwise AND operator ( &amp; ) to clear a bit. number &amp;= ~(1 &lt;&lt; x); That will clear bit x . ... 3、Toggling a bit. The XOR operator ( ^ ) can be used to toggle a bit. number ^= 1 &lt;&lt; x;
#1
更多评论
a &amp;^ b = a ^ (a &amp; b)
#2
wandercn
https://hotbuild.rustpub.com
<a href="/user/dagongrenzzZ" title="@dagongrenzzZ">@dagongrenzzZ</a> 谢谢,可以了
#3

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中