In digital computer programming, a bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast, primitive action directly supported by the processor, and is used to manipulate values for comparisons and calculations.

On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition.

Bit shifting operations are, indeed, bitwise operations because they treat every value as a series of bits instead of a numerical quantity. With these operations you can “move” bits to the left or right.

What the f*** are you talking about?

Well, it is said that in software, exists 10% of code that is running 90% of time. So, what if in that 10% of code you are doing huge operations repeteadly? Maybe you want to consume less resources, so instead of doing operations with common “/, *” I recommend you to use bit shifting operations. I will tell you basic usages and how to implement it in Java.

Usages and explanation

Imagine that you want to divide 128 by 32. You usually will do it like so:

It’s working, but if you want to optimize it, just use bit shifting:

Magic? Nah, you can try it, both operations will return the same: 4.

What just happened?

Okay, 128 in binary is 100000000.

128

64

32

16

8

4

2

1

0

1

0

0

0

0

0

0

0

0

If you bit shift it to the right (division, ») 5 positions… What number do you get in binary? 1000, and 1000 is… 4!

0

1

2

3

4

5

128

64

32

16

8

4

2

1

0

0

0

0

0

0

1

0

0

0

As you can see, the basics are so easy to understand. You can do the same if you want to multiply values, instead of shifting bits to the right, you just have to shift them to the left («).

That operation will return 128.

5

4

3

2

1

0

128

64

32

16

8

4

2

1

0

1

0

0

0

0

0

0

0

0

Conclusion

Hope you have learnt a lot reading this and I encourage you to use bit shifting (you can do it in almost all languages and it’s the same!) in order to decrease resource consumption in your software.
Of course this is a very simple explanation of what is bit shifting, so I recommend you to keep learning about this topic.