Bitwise Operators: Ancient Alien technology ?

Sam Lesser
5 min readAug 15, 2020

--

I remember when I first stumbled upon these strange operators. It felt like some alien code written in a star system on the far side of the galaxy. I wasn’t really sure what they were or how I would even use them. After doing some research I found out there actually pretty important, and can give us all a bit more appreciation and insight to what a computer does on the lower levels of writing code.

Bitwise operators use “bits”, which you can manipulate in several different ways. Instead of using integers or floating numbers to make your code work. We can rely on bit manipulation to perform the useful tasks. This may seem strange, but can actually give us some great advantages when we build out a project.

What’s a bit ?

example of binary

A binary digit is the smallest increment of data on a computer. A bit can hold the value of either a 1 or 0. Which is just enough to do some pretty important things. Think of it as a very natural way for a machine to count. If we tap into machine style counting from the level of abstraction a human does when we write code , it gives use a very unique and powerful set of tools to use.

Base 10

A pretty common way to count. 10 based counting systems are a great way for humans to keep track of things numerically. After all, many of us have 10 fingers an 10 toes. There are many counting systems that exist in the world. Each have their own special purpose, including computers which count by 2.

Base 2

This is how machines and computers count. They can make decisions for us this way too. It’s strange but think about how we use the words in our own life and what they mean to us. Yes & No. True & False. On & Off. We use words to represent a binary relationship in things. Events that are pretty familiar, like using a light switch, taking a pop quiz, and even making an important life decision. A computer would handle these events by reducing it down to two simple values, zero and one.

Bitwise & (AND)

This operator compares 2 integers. Think of it as a conditional statement on a binary level. It’s output is a binary number, think almost like a tally. Each integer is represented in binary, then the bit values are compared at each place value. If they match that place value is one, if they don’t it’s zero.

Bitwise | (OR)

Similar to the Bitwise AND, each value is compared in binary. If either binary place value is one, that place value is one. If not, it’s zero.

About Bit shifting…

Think about it like you are rounding a number. If you round down, you’ve lost some decimal values, but you get a nice round number. We lost some of that number but it gives us a value we want to work with. If we don’t care about what we’ve lost, this technique known as truncation can be quite a handy technique.

The same principle can be said about shifting. We add a binary value to a representation of an integer in binary. When we sum these two values, all of our binary can shift to the left or right. This gives us an additional place value, but it’s not what we are interested in. Thus we round it off. It’s an interesting thing that happens with 2 based counting systems, and it’s useful. Thus we get left and right shift bitwise operators.

Shifting can be broken down further into Arithmetic and Logical. When you shift Artithmeticially you are either multiplying or dividing that value by 2. If you are logically shifting a number the output does not have that relationship. Thus it’s important to understand WHY you would shift left or right and WHAT value that would you be inserting. You do this with your input so make sure you understand the output when selecting a one or a zero !

Bitwise << (LEFT SHIFT)

Shifts the binary value to the left with a binary value. If your after an arithmetic output it’s like multiplying by 2.

Bitwise >> (RIGHT SHIFT)

Shifts the binary value to the right with a binary value. If your after an arithmetic output it’s like dividing by 2.

Conclusion

Commonly used in cryptography, network protocol and even graphics, these operators can be handy. It may take a bit of creativity and open-mindedness but, they can solve some pretty complex problems when used correctly. It is amazing what can happen once you tap into the low-levels of programming. These operators optimize your code as they are fast, elegant and great for storing code.

Check out these super cool links, they gave me the inspiration to write this blog. You can also find additional operators as well, happy reading !

1. https://en.wikipedia.org/wiki/Bitwise_operation

2.https://dev.to/logan/using-bitwise-operators-6c8

3.https://medium.com/rubycademy/ruby-bitwise-operators-da57763fa368

4.https://www.w3resource.com/ruby/ruby-bitwise-operators.php

--

--

Sam Lesser
Sam Lesser

Written by Sam Lesser

Career Changer, Software Engineer & Web Developer

No responses yet