Learning bit shifting to solve leetcode question

I am really excited about how much I have learned since I started my data science fellowship at The Knowledge House in September 2020. We focused on learning the fundamentals of python in the first phase of the program, and are now solving leetcode questions to prepare for technical interviews.

One of the leetcode questions we had to solve was the binary number with alternating bits. Given a positive integer, we had to check whether it has alternating bits. I learned about binary right shift to solve this question.

No alt text provided for this image
No alt text provided for this image

My code performs the logical AND operation on each bit of the given integer and binary number 1. The reason is that if an integer has alternating bits, then the result of the AND operation will alternate, too. To accomplish this, the code first performs the logical AND operation on the given integer and the binary number 1 outside the while loop, which results in either 0 or 1 since the AND operation is performed on each pair of corresponding bits. In this case, binary number 1 has only one bit, so the AND operation is performed with only one bit of the given integer - the rightmost bit. The result of this operation is saved in a variable called previous_digit. 

The logical AND operation needs to be performed with binary number 1 and the next bit of the given number. This is when bit shifting comes into place. A right binary shift pushes bits to the right by the specified number of places - one in this solution - and drops the rightmost bits. Right bit shifting ( n >>m) is the same as a floor division by a power of two where the power, m, represents the number of places the number n is shifted to the right. 

Once bit shifting is performed, the new value of n enters a while loop to repeat the process of performing AND operation between n and binary number 1. This value is saved in a variable called digit. If digit is equal to previous_digit, then it means that n has alternating bits, and, therefore, the function should return false. Once the while loop is exited, it returns true because it does not find any adjacent bits that have the same AND operation results. 

I would like to thank one of the TKH volunteers, Michael Lieberman, for introducing me to bit shifting and for guidance on how to solve this question. I would also like to thank everyone at The Knowledge House, especially our wonderful TA Malcolm Holliday, for teaching and supporting us in our journey to transition to the tech world.



Great work Janet! Representing Binghamton and the BX well!

Great especially that you shouted out Malcolm Holliday. He’s always giving back.

I love to see this leetcode explanation and walkthrough! This is technically inspiring!

Janet, the way you explain code is beyond great, always. I need more articles like this, keep up the good work girl!

To view or add a comment, sign in

Others also viewed

Explore content categories