0

What are binary shift operators used for in practice?

I am not really used to low level programming. Mostly do functional things to solve complex system models for work. I am trying to understand the use of bitwise operations, but fail to see for what you use binary shift operators (like << and >> in python or c++, for example). I understand that they move the bits by the amount specified, but fail to see when this is useful. Can someone explain when this is practically useful, and preferably give a short and simple example that helps understanding. Thankyou.

26th Jan 2017, 10:10 AM
nicolito
nicolito - avatar
2 Antworten
+ 2
I believe bitwise operators have many uses. However, I'm only aware of one major advantage and that is speed. Let's say you're making a game with 32*32 tiles and a map 100*100. To draw the graphics, you'll probably have several loops nested in each other being updated many times per second. Now, instead of using *32, a bitshift of << 5 is faster and when you're performing so many calculations, it makes a big difference. Why bitshifts are faster though, you'll have to learn about CPUs and Assembly.
26th Jan 2017, 11:18 AM
Division by Zero
0
Thankyou Joshua. That is a good example, and explanation.
26th Jan 2017, 11:40 AM
nicolito
nicolito - avatar