去二进制位元补印 [英] Go binary bitwise complement printing
本文介绍了去二进制位元补印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有关位元补码( ^
),我的预期:
NOT 010101
----------
101010
但我得到:
NOT 010101
----------
1111111111111111111111111111111111111111111111111111111111101010
http://play.golang.org/p/CGqiXbtQXe
反正是有,我不浪费这些额外的比特和刚刚从函数返回101010?
FUNC转换(ST字符串){UINT64
VAR NUM UINT64
如果是我,ERR:= strconv.ParseUint(ST,2,64);呃!= {为零
恐慌(ERR)
}其他{
NUM = I
}
返回NUM
}
解决方案
有没有6位整数类型(几乎没有任何编程语言有它)。多余位不是真正的浪费呢。
总之,只要使用XOR:
Z:= X ^ 0x3F的
https://play.golang.org/p/ENcSs020Ti
For bitwise complement (^
), I expected:
NOT 010101
----------
101010
But I got:
NOT 010101
----------
1111111111111111111111111111111111111111111111111111111111101010
http://play.golang.org/p/CGqiXbtQXe
Is there anyway that I do not waste these extra bits and just returns the 101010 from the function?
func convert(st string) uint64 {
var num uint64
if i, err := strconv.ParseUint(st, 2, 64); err != nil {
panic(err)
} else {
num = i
}
return num
}
解决方案
There is no 6 bit integer type (almost no programming language has it). The extra bits aren't really "waste" anyway.
Anyway, just use xor:
z := x ^ 0x3f
https://play.golang.org/p/ENcSs020Ti
这篇关于去二进制位元补印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文