Bits and maths interview questions
XOR, masks and shifts, counting 1 bits, and the modular arithmetic that keeps big counts manageable.
– interview-ready · 6 questions in this topic
– of 6 tried
Saved on this device. Interview-ready means you got it right on two different days. Guesses and hints count for less.
Questions
Practise this topic
Flashcards · 16
Flip, then say if you knew it. Needs JavaScript; every answer is also on its question page.
Quick quiz · 6 questions
One exercise per question, checked as you go. Needs JavaScript.
Mock interview · 1 set
Timed, out loud, then tick what you covered. Needs JavaScript.
Quick facts
Recall the answer first, then tap Show to check.
int.bit_count()(13).bit_count()returns 3, the count of 1 bits (Python 3.10+);bin(x).count("1")is the older spelling.int.bit_length()x.bit_length()is the number of bits needed to write x in binary, so 2 ** (x.bit_length() - 1) is its top bit.Three-argument
powpow(a, b, m)computes aᵇ mod m by repeated squaring in O(log b) multiplications.Negative numbers and
>>In Python,
-5 >> 1is -3: right shifts round towards minus infinity, like//.Kotlin bit operators
Kotlin uses infix functions:
x and y,x or y,x xor y,x shl 1,x ushr 1, andInteger.bitCount(x).
Printable cheat sheet · Review your weak spots in this topic
Share your progress
Sources: Topic outline informed by the Tech Interview Handbook by Yangshun Tay and the NeetCode 150 pattern grouping by neetcode-gh, both MIT licensed. Every problem, solution and explanation is written by Best Latest Apps. Credits and license