Bit operations (C++)

前端 未结 2 1236
难免孤独
难免孤独 2021-02-03 12:02

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

相关标签:
2条回答
  • 2021-02-03 13:01

    This is a very strange question to be asked. First of all because the language should be irrelevant - as long as you're writing in a compiled language, any bitwise operation should be compiled down to a single assembly instruction even in the most simplistic assembly languages.

    Secondly, once that single assembly instruction hits the processor, it should be evaluated in a single cycle - bitwise stuff is just that simple. It's possible that a space-starved processor would implement the shift operation in a couple cycles (they require a lot more circuitry than the others to do quickly), but that seems pretty unlikely on any modern machine, so even shifts should be single-cycle instructions.

    Here's what Wikipedia has to say on the matter:

    A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. On most older microprocessors, bitwise operations are slightly faster than addition and subtraction operations and usually significantly faster than multiplication and division operations. On modern architectures, this is not the case bitwise operations are generally the same speed as addition (though still faster than multiplication).

    Hope this helps!

    0 讨论(0)
  • 2021-02-03 13:05

    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...)

    0 讨论(0)
提交回复
热议问题