Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reverse the bits of a given 32 bits unsigned integer.
- Input format
- First line contains the number of test cases.
- Each line contains a 32 bit unsigned integer.
- Output format
- Output the Reversed unsigned integer.
- Sample Input 1
- 1
- 43261596
- Sample Output 1
- 964176192
- Explanation 1
- The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 whose binary representation is 00111001011110000010100101000000.
- Constraints
- 1 <= T <= 10^5
- 0 <= N <= 2^32-1
- */
- /*
- Certainly, let's break down the process/intuition/approach for the `reverseBits` function:
- 1. **Input Validation**:
- - Ensure that the input `n` is a positive integer.
- 2. **Convert to Binary**:
- - Use the `getBinary` function to convert the positive integer `n` into its binary representation.
- - The `getBinary` function iteratively divides `n` by 2 (using logical right shifts `n >>> 1`) and keeps track of the remainders (which are either 0 or 1) in an array.
- - The array is filled from right to left, so it will contain the least significant bit (LSB) first and the most significant bit (MSB) last.
- 3. **Pad with Leading Zeros**:
- - Ensure that the binary array has exactly 32 bits by padding it with leading zeros if necessary.
- - This step ensures that the binary representation is a 32-bit representation.
- 4. **Reverse the Binary Array**:
- - Reverse the order of bits in the binary array. This effectively reverses the binary representation of the input number.
- 5. **Convert Back to Decimal**:
- - Use the `getDecimalForm` function to convert the reversed binary array back to a decimal integer.
- - The `getDecimalForm` function iterates through the reversed binary array, multiplying each bit by the corresponding power of 2 and accumulating the result.
- 6. **Return the Result**:
- - Return the decimal integer obtained in step 5 as the result of reversing the bits of the input number.
- This process ensures that the input number's binary representation is reversed correctly, and the result is returned as a positive integer.
- You can memorize this process as a step-by-step guide for reversing bits in a positive integer in JavaScript.
- */
- function toUnsigned32Bits(value){
- return (value>>>0);
- }
- function getBinary(n){
- let arr=[];
- while(n){
- arr.push(n&1);
- n=n>>>1;
- }
- // arr.reverse();
- while(arr.length<32){
- arr.push(0);
- }
- return arr;
- }
- function getDecimalForm(arr){
- let num=0;
- for(let i=0;i<32;i++){
- num=toUnsigned32Bits(num+arr[i]*Math.pow(2,i));
- }
- return num;
- }
- /**
- * @param {number} n - a positive integer
- * @return {number} - a positive integer
- */
- function reverseBits(n) {
- // Implement here
- let binaryForm=getBinary(n);
- binaryForm.reverse();
- return getDecimalForm(binaryForm);
- }
- function main() {
- let t = parseInt(readLine(), 10);
- while(t--) {
- let n = parseInt(readLine(), 10);
- let answer = reverseBits(n);
- print(answer + "\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement