190. Reverse binary numbers

Problem solving ideas

Consistent with 7. Integer reversal idea, use% operation to get the lowest digit, and invert it to the highest digit by multiplying it each time. Note that the original number must be continuously divided by the weight.

Code

uint32_t reverseBits(uint32_t n) {
long result=0;
for(int i=0;i<32;i++){
result=result*2+n%2;
n/=2;
}
return result;
}

LeetCode link

Guess you like

Origin blog.csdn.net/qq_44722674/article/details/111539949