Bit Manipulations in Python — Technical Interview (Part 1)

Uniqtech
Interview Buddy
Published in
5 min readDec 22, 2018

If you graduated from a coding bootcamp, it is possible that you never even heard of bit manipulation. It is a favorite in traditional technical interviews. It tests your technical knowhow and sometimes bit manipulation is a fast shortcut or alternative to solving difficult interview problems. It’s a bit strange to do it in Python. Many interviews are conducted in JAVA. Cracking the Coding Interview is written in JAVA for sure. But let’s take a look at Bit Manipulation in Python! This is beginner friendly, bootcamp graduate friendly, just like ALL of our articles. If you graduated from a bootcamp or Udacity or Coursera, this article may help you out tremendously! Subscribe, follow and clap for more!

In your terminal or command line type the follow after the dollar sign to start the interactive Python command line. I use the dollar sign whenever I want to demo command line code. Try this in any Python IDE if you don’t have a Mac and have python ready.

$ python

Try the following. You are in python now, so I don’t use the dollar sign to denote the code. Anything after the pound sign # is considered inline comments by python.

1 & 0 #returns --> 0
1 & 1 #returns --> 1
1 | 0 #returns --> 1
0 | 1 #returns --> 1
1 ^0 #returns --> 1
1^1 #returns --> 0

Congratulations! You just handled 6 operations on bits.

Really take a moment to read through each line and think what happened. Wait a second. What is & | ^ You may recall that Python does not let you use & as and in if else statements nor does it let you use | pipe as or in if else statements (quite a few languages do). Why is that? Because these three are used and reserved for bit manipulation in Python! They work with bits rather than symbols like variables or conditions.

# & is bit manipulation and
#| is bit manipulation or
# ^ is bit manipluation for XOR

Bear with me for a few more minutes. This is surely starting to be confusing. What are bits? What is bit manipulation? Why are there AND OR and XOR. These are logical operations you can use on bits of information. Shifting is another bit manipulation that you can do. But more on that later. AND OR XOR are logical operators. There are abstract representations of these logical gates in every textbook. Let’s not jump into the wormhole too early.

Recall a familiar concepts about true and false.

true and true is true
true and false is false
true or false is true
false or true is true
true xor true is false
true xor false is true

Don’t worry XOR is usually the most confusing. It warrants a separate article and tutorial. That’s exactly what we will do. We will explain tricks to understand XOR in a future post. Express your interest by commenting below to expedite this process.

AND is easy to understand. The AND operand returns true only if both the left side and right side are true! For example, you have to be admitted and also have paid your tuition to be able to attend college. true and true returns true. If you fail either, you cannot go to college.

OR is easy to understand too. The OR operand returns true if either the left side or the right side evaluates to True. You are attending a concert that accepts both electronic and paper ticket. You can enter the concert if either you show a valid QR code on your phone OR you have brought a valid print out of the QR code. If either is true, you can enter the concert. There’s no need to bring both copies of the ticket. OR evaluates to false only when both sides are false!

The important part was you just handled bit manipulation. It was short, it was fast and it wasn’t too hard to copy and paste. Intuitively the results even made sense. Now it is time for us to dive deeper to understand why we got these results.

You should also know that some values are truthy or and falsey. For example, 1 & True # returns True, 0 | False #returns False . That is to say 1 is truthy and is evaluated to true when compared in a condition, 0 is falsey and is evaluated to false when compared in a condition statement.

Now here’s something strange, when I use the logical operator on numbers that are not one or zero, I get strange results — results that are not intuitive.

1&4 # returns 0
1&5 # returns 1
1 ^ 4 # returns 5
1 ^ 5 # returns 4

This is why sometimes technical interview ask bit manipulation questions. If you don’t understand how computers store and compute numbers, there’s no intuitive explanation why the above results are true. Sorry, to understand bit manipulation, you have to understand bits first.

Boring! Boring! However, once you understand bits … let’s inject some motivation here… it’s like you understand the universe (cough cough may be just computers and programming) on a WHOLE NEW LEVEL. It will explain SO MUCH. So much that is missed out when we found alternative ways to learn computer science. Fun and creative lectures got us started in a career track that was originally too difficult or not inviting. The tradeoff is we missed out on the very foundation of how computers work.

Ever wondered why your computer has 64 bits or 32 bits windows? Why are there 32GB, 64GB then 128 GB memory cards, why not 30GB. Ever wondered why are 8 Bit Games 8 bits? Bits and bit manipulation are all around us. We just didn’t know we were dealing with it.

Shifting bits in Python and Counting in Binary Explained!

Read our detailed knowledge flash card : how to shift bits in Python, >> and << and how to count in binary. Read more here.

Be sure to check our binary numbers and hexadecimal flashcards.

Binary Numbers, Bit Manipulation, Hexadecimal

Memory units bit, nibble, byte [public]
https://ml.learn-to-code.co/skillView.html?skill=JtLrME3XnTCEiUXivICV

Bit manipulation overflow error, max integer example [public]
https://ml.learn-to-code.co/skillView.html?skill=7wLuRBPKjuZHbVBE04a1

What is Hexadecimal, base-16 numbers? (definition) Part 1 [public, pro tip, high quality]
https://ml.learn-to-code.co/skillView.html?skill=UpZzl8OpW1PiAIvsAmOT

What is Hexadecimal, base-16 numbers? (definition) Part 2 [public, pro tip, high quality]
https://ml.learn-to-code.co/skillView.html?skill=jS3ouYSNs8tZMgirvrgE

What is Hexadecimal, base-16 numbers? (definition) Part 3 Converting binary numbers to hexadecimals. [public, pro tip, high quality]
https://ml.learn-to-code.co/skillView.html?skill=vLGXEZq2XUXVTdiUURqd

Uniqtech Guide to Decimal, Binary Number and Hexadecimal [public, pro tip, high quality]
https://ml.learn-to-code.co/skillView.html?skill=eHE5d0U0gAAQMFpiu0sC

Coming soon … positive numbers and negative numbers in bits

Coming soon … XOR explained

--

--