How to check if a number is a power of 2 using logical operations[Technique Tuesdays]
A neat little trick for making your computations more effective-
To learn more about the newsletter, check our detailed About Page + FAQs
To help me understand you better, please fill out this anonymous, 2-min survey. If you liked this post, make sure you hit the heart icon in this email.
Get a free Weekly Summary of the important updates in AI and Machine Learning here
Recommend this publication to Substack over here
Take the next step by subscribing here
Most of you know how to use AND, OR, and NOT operations,
However, very few of you have mastered these operations. Which is a bummer, because these are some super-powerful techniques. Learning to master these will skyrocket your skills, whether it’s in Coding Interviews or just being a better Programmer. Today’s post/email will get you one step closer to that enviable state.
Important Points
What is the technique- Simply put, you can check if a number x is a power of 2 by doing the following comparison-
def isPowerOfTwo(x): return (x and (not(x & (x - 1))))
Why does this work- Every power of 2 can be represented in binary by the following regex- 1(0)n [this means 1 followed by n zeroes]. That number-1 is going to be (1)^(n-1) [(n-1) 1s]. So, if a number n is a power of 2 then bitwise & of n and n-1 will be zero. However, x & (x-1) could be 0 if x is 0 (which is not a power of 2). To account for that, we add in the x and portion.
Why this technique is amazing- This is a very important technique because it uses logical operations and bit shifting, which are very computationally efficient. Thus having this technique in your arsenal is a great way to stand out in your interviews/be an effective software engineer.
I created Technology Made Simple using new techniques discovered through mentoring multiple people in top tech firms. The newsletter is designed to help you succeed, saving you from hours wasted on going through substandard resources, the Leetcode grind, or multi-hour-long lectures. I have a 100% satisfaction policy, so you can try it out at no risk to you. You can read the FAQs and find out more here. Use the button below to get 20% off for up to a whole year. Using this discount will drop the prices-
800 INR (10 USD) → 533 INR (8 USD) per Month
8000 INR (100 USD) → 6400INR (80 USD) per year
Before proceeding, if you have enjoyed this post so far, please make sure you like it (the little heart button in the email/post). I also have a special resource for you.
In the comments below, share what topic you want to focus on. I’d be interested in learning and will cover them. To learn more about the newsletter, check our detailed About Page + FAQs
If you liked this post, make sure you fill out this survey. It’s anonymous and will take 2 minutes of your time. It will help me understand you better, allowing for better content.
https://forms.gle/XfTXSjnC8W2wR9qT9
I see you living the dream.
Go kill all and Stay Woke,
Devansh <3
To make sure you get the most out of Technique Tuesdays, make sure you’re checking in the rest of the days as well. Leverage all the techniques I have discovered through my successful tutoring to easily succeed in your interviews and save your time and energy by joining the premium subscribers down below. Get a discount (for a whole year) using the button below
Reach out to me on:
Instagram: https://www.instagram.com/iseethings404/
Message me on Twitter: https://twitter.com/Machine01776819
My LinkedIn: https://www.linkedin.com/in/devansh-devansh-516004168/
My content:
Read my Machine Learning breakdowns: https://rb.gy/zn1aiu
My YouTube: https://rb.gy/88iwdd
Get a free stock on Robinhood. No risk to you, so not using the link is losing free money: https://join.robinhood.com/fnud75