TCS · Question 5
Reverse Bits of an Integer
Problem
Reverse all bits in an integer.
Approach
Shift bits from n into the result, LSB first, 32 times.
Time O(1)Space O(1)
How it works
- 1Pull the lowest bit of n into the result's bottom.
- 2Shift result left and n right, 32 rounds.
Sample input / output
Input: n = 43261596 Output: 964176192
Constraints
0 <= n < 2^32.
Solution — read it, then walk it
Time O(1)Space O(1)Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/8
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits