Advertisement
exmkg

2-3

Jan 14th, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.40 KB | None | 0 0
  1. public class Solution {
  2.     // you need treat n as an unsigned value
  3.     public int reverseBits(int n) {
  4.         int result = 0;
  5.         for (int i = 0; i <= 31; i++) {
  6.             int bit = (n >> i) & 1;
  7.             if (bit == 1) { // If n's i-th bit is true, then
  8.                 result |= 1 << (31 - i); // set true the result's (31-i)-th bit.
  9.             }
  10.         }
  11.         return result;
  12.     }
  13. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement