位操作(C ++) [英] Bit operations (C++)

查看:152
本文介绍了位操作(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在面试中遇到了一个问题 - 我被要求在效果方面比较按位操作。




我想这个问题可能是相当普遍和漂亮的机器 - 具体的,但我也认为应该有一些一般的规则,你必须提及(我没有:)。


那么 - 你会回答什么?



我可能还应该说,比较他们在C (或C ++,无论如何),因为我假设这些语言为编译器提供更多空间来执行位相关的优化。








好的,完整的问题上下文。 b $ b

面试有几个部分,其中一些是真的一块蛋糕,一些是一场噩梦。位相关部分是一种困难的问题,包括的问题:




  • 浮点数规格, float c>

  • / code> - > int 转换(如果您知道范围则更快)

    b $ b


这些不是很难,但作为这个位相关部分的最后一个问题,我被要求枚举位操作,我知道并比较它们的性能。



我回答了一些不像描述性的东西,像它的架构,编译器,...具体,它不会有什么影响,bitwise已经漂亮低级,但我想这个回答很烂。

解决方案



例如, a =(a >> 1) a =(a / 2)



在许多情况下,包括软件和硬件优化,流水线,缓存等)有效地占用1个CPU周期,因此在现代处理器上,您不太可能看到差异 - 但是如果他们通过多个ALU并行运行许多指令,那么您仍然可以从更好的利用如果您混合算术和按位操作,CPU的并行路径。如果你编写一个更高级的语言,那么编译器很可能优化你的代码使用任何更好的形式,所以你会看到相同的性能,无论你怎么写你的代码!



但是,有些情况下,逐位操作可以明显快于简单的算术/逻辑 - 例如,您可以对32位或64位值中的所有字节应用一些操作几个逐位操作(有效地同时处理所有字节),否则这将需要大量循环和比较逻辑进行增量。请参阅此处了解可以实现的一些重要示例。在这些情况下,通常需要获得显着的性能优势。 (虽然精确的增益可能取决于您所定位的CPU)



(或者,他们可能只是意味着移位操作比XOR更快 ,在这种情况下使用现代处理器在大多数情况下,答案可能是否 - 大多数按位操作是快速的,但这是一个相当无意义的问题...)


Recently I had a question on the interview - I was asked to compare bitwise operations in performance terms.

Like, give a brief description of the performance of different bit operations.

I guess this question could is pretty general and pretty machine-specific, but I also think there should be some general rules about this, which you have to mention (and I didn't :).

So - what would you answer?

I probably should also say, that it may be a good idea to compare their performance in C (or C++, whatever), because I assume that these languages give more space for the compiler to perform bit-related optimizations.

Thank you.


Okay, the full problem context.

The interview had several sections and some of them were really a piece of cake, some were a nightmare. Bit-related section was kind of tough and including the questions like:

  • Floating point number specifications, float, double

  • Fast float->int conversions (and even faster one if you know the range)

Those weren't very tough, but as the last question in this bit-related section I was asked to enumerate bit operations I know and compare their performance.

I answered something not really descriptive like "it's architecture, compiler, ... specific, it won't actually matter, bitwise is already pretty low-level", but I guess this answer sucks.

解决方案

I would imagine they mean comparing bit operations to arithmetic equivalents.

For example, "is a = (a>>1) faster than a = (a / 2)?"

In many cases, simple operations like this will (for various reasons including software and hardware optimisations, pipelining, caching, etc) effectively take 1 CPU cycle, so on modern processors you are unlikely to see a difference - but if they run many instructions in parallel through multiple ALUs then you may still benefit from better utilisation of the CPU's parallel pathways if you mix arithmetic and bitwise operations. If you write in a higher level language then the compiler is very likely to optimise your code to use whatever is the better form, so you'll see the same performance regardless of which way you write your code!

However, there are cases where bitwise operations can be significantly faster than simple arithmetic/logic - for example, you can apply some operations to all the bytes in a 32-bit or 64-bit value with a few bitwise operations (that effectively process all the bytes simultaneously), which would otherwise take a lot of looping and comparing logic to do incrementally. See here for some great examples of what can be achieved. In these cases there is often a significant performance benefit to be gained. (although the exact gain can depend a lot on the CPU you're targeting)

(Alternatively, they may have just meant "is a shift operation faster than an XOR", in which case with modern processors in most cases the answer is probably "no" - most bitwise operations are quick. But that would be a pretty pointless question to ask...)

这篇关于位操作(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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