某些运算符之间的差异“|”,“^”,“&”,“& ^”。 Golang [英] Difference between some operators "|", "^", "&", "&^". Golang

查看:181
本文介绍了某些运算符之间的差异“|”,“^”,“&”,“& ^”。 Golang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我读了golang规范,并面对一些有趣的操作符:

 &按位与整数
|按位或整数
^按位XOR整数
& ^位清零(AND NOT)整数

我尝试过使用它,但我唯一了解的是|添加整数和+运算符还可以与浮点数,字符串等一起工作。



它们在实践中用于什么?任何人都可以对上述4个运算符进行一些解释吗?

解决方案

当您必须使用字节或比特级数据。



这里我列举了一些使用位操作和代码示例的示例(没有特定的顺序):






1 它们是 中常见的并且是众多算法的一部分, 散列函数(例如 MD5 )。

如果你想节省空间,而且你把多个bool变量压缩成一个 int
,它们也经常被使用。 / code>例如,您为每个bool变量分配一个位。您必须使用按位运算符来单独更改/读取位。



例如,将8位/ bools打包成一个 int < code $:

  flags:= 0x00 //所有标记都是0 
flags | = 0x02 //将第二位转为1(保持不变)
flags | = 0xff //将8位(0..7)转换为1
flags& = 0xfe //将最低位设置为0保持不变)

istrue:= flags& 0x04!= 0 //测试第三位是1

3。另一个区域是压缩数据,您希望从字节并使用它的所有位来存储/检索一些信息(有一点是计算和数字通信中的基本信息单元)。

4。 与压缩类似但不完全相同: bitstreams 。它也用于通过不发送完整字节,而是具有任意位长的字段来节省数据流中的空间。



我已经编写并发布了一个高度优化的位-level Reader和Writer软件包,可在此处找到: github.com/icza/bitio 。您会在其源代码中看到广泛使用各种位操作。



5。另一个实际用法:测试某些属性一个(整数)数字。知道整数的二进制表示(二补),它们的二进制表示中存在数字的某些特征。例如,如果最低位为0,则整数(2的补码)是偶数(可以除以2):

  func isEven(i int)bool {
return i& 0x01 == 0
}

通过测试一个整数的位数,你也可以判断它是否是2的幂。例如,如果一个正数只包含一个 1 位,那么它是2的幂(例如 2 = 0x02 = 00000010b 16 = 0x10 = 00010000 但例如 17 = 0x11 = 00010001 不是2的幂)。
$ b 6。许多编码/解码程序也使用位操作。最微不足道的是 UTF-8编码,它使用可变长度编码来表示unicode代码点(<$ Go中的c $ c> rune
)作为字节序列。

变长编码的简单变体可以是使用字节的最高位(第8或第7个if 0索引)来表示是否需要更多字节来解码数字,其余7位始终是有用数据。您可以测试最高位并分离这7个有用位:

  b:= readOneByte()
usefulBits:= b& 0x7f
hasMoreBytes:= b& 0x80!= 0

使用这种可变长度编码的好处是,即使您使用 uint64 在Go中输入8个字节的内存,小数字仍然可以用较少的字节表示( 0..127 只需要1个字节!)。如果要存储或传输的样本具有许多较小的值,则这可以将数据压缩到1/8 = 12.5%。缺点是大数字(即使在最高字节中也有位)将使用多于8个字节。是否值得这取决于样本的启发性。



X。 然后列表继续...






您可以在Go中(以及其他许多编程语言)不知道/使用按位运算符吗?答案是肯定的。但如果你了解他们,有时他们可以让你的生活更轻松,并且你的程序更有效率。

如果你想了解更多的话题,请阅读维基百科的文章:一个href =http://en.wikipedia.org/wiki/Bitwise_operation>按位运算和谷歌术语按位运算符教程,有很多好的文章。


Recently I read golang specification and faced with some interesting operators:

&    bitwise AND            integers
|    bitwise OR             integers
^    bitwise XOR            integers
&^   bit clear (AND NOT)    integers

I've tried to play with it, but the only one I have understood is that "|" add integers and "+" operator additionally work with floats, strings etc.

What they are used for in practice? Could anyone give some explanation about these 4 operators above?

解决方案

Bitwise operators come into play when you have to work with byte- or bit-level data.

Here I list a handful of examples using bit operations with code samples (in no particular order):


1. They are common and part of many algorithms in cryptography and hash functions (e.g. MD5).

2. They are also often used if you want to "save" space and you pack multiple "bool" variables into one int for example, you assign a bit to each bool variable. You have to use bitwise operators to be able to individually change/read the bits.

For example packing 8 bits/bools into one int:

flags := 0x00  // All flags are 0
flags |= 0x02  // Turn the 2nd bit to 1 (leaving rest unchanged)
flags |= 0xff  // Turn 8 bits (0..7) to 1
flags &= 0xfe  // Set the lowest bit to 0 (leaving rest unchanged)

istrue := flags&0x04 != 0 // Test if 3rd bit is 1

3. Another area is compressing data where you want to get the most out of a byte and use all its bits to store/retreive some info (a bit is the basic unit of information in computing and digital communications).

4. Similar to compression but not quite the same: bitstreams. It is also used to save space in a data stream by not sending complete bytes but rather fields having arbitrary bit-length.

I've written and published a highly optimized bit-level Reader and Writer package, open sourced here: github.com/icza/bitio. You will see extensive usage of all kinds of bit operations in its sources.

5. Another practical usage: testing certain properties of an (integer) number. Knowing the binary representation of integer numbers (Two's complement) there are certain characteristics of numbers in their binary representation. For example an integer number (in 2's complement) is even (can be divided by 2) if the lowest bit is 0:

func isEven(i int) bool {
    return i&0x01 == 0
}

By testing the bits of an integer you can also tell if it's a power of 2. For example if a positive number only contains one 1 bit, then it is a power of 2 (e.g. 2 = 0x02 = 00000010b, 16 = 0x10 = 00010000 but for example 17 = 0x11 = 00010001 not power of 2).

6. Many encoding/decoding procedures also use bit operations. The most trivial is the UTF-8 encoding which uses a variable-length encoding for representing unicode code points (rune in Go) as byte sequences.
A simple variation of a variable-length encoding could be to use the highest bit of a byte (8th or 7th if 0-indexed) to signal if more bytes are required to decode a number, and the remaining 7 bits are always the "useful" data. You can test the highest bit and "separate" the 7 useful bits like this:

b := readOneByte()
usefulBits := b & 0x7f
hasMoreBytes := b & 0x80 != 0

The profit of using such a variable-length encoding is that even if you use uint64 type in Go which is 8 bytes in memory, small numbers can still be represented using less bytes (numbers in the range 0..127 only require 1 byte!). If the samples you want to store or transfer have many small values, this alone can compress the data to 1/8th = 12.5 %. The down side is that big numbers (which have bits even in the highest byte) will use more than 8 bytes. Whether it's worth it depends on the heuristic of the samples.

X. And the list goes on...


Can you live without knowing/using bitwise operators in Go (and in many other programming languages)? The answer is Yes. But if you know them, sometimes they can make your life easier and your programs more efficient.

If you want to learn more on the topic, read the Wikipedia article: Bitwise operation and google the term "Bitwise Operators Tutorial", there are many good articles.

这篇关于某些运算符之间的差异“|”,“^”,“&amp;”,“&amp; ^”。 Golang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆