Advertisement
satishfrontenddev5

Untitled

Jan 6th, 2024
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Reverse the bits of a given 32 bits unsigned integer.
  3.  
  4. Input format
  5. First line contains the number of test cases.
  6.  
  7. Each line contains a 32 bit unsigned integer.
  8.  
  9. Output format
  10. Output the Reversed unsigned integer.
  11.  
  12. Sample Input 1
  13. 1
  14.  
  15. 43261596
  16.  
  17. Sample Output 1
  18. 964176192
  19.  
  20. Explanation 1
  21. The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 whose binary representation is 00111001011110000010100101000000.
  22.  
  23.  
  24. Constraints
  25. 1 <= T <= 10^5
  26.  
  27. 0 <= N <= 2^32-1
  28. */
  29.  
  30. /*
  31. Certainly, let's break down the process/intuition/approach for the `reverseBits` function:
  32.  
  33. 1. **Input Validation**:
  34.    - Ensure that the input `n` is a positive integer.
  35.  
  36. 2. **Convert to Binary**:
  37.    - Use the `getBinary` function to convert the positive integer `n` into its binary representation.
  38.    - 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.
  39.    - 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.
  40.  
  41. 3. **Pad with Leading Zeros**:
  42.    - Ensure that the binary array has exactly 32 bits by padding it with leading zeros if necessary.
  43.    - This step ensures that the binary representation is a 32-bit representation.
  44.  
  45. 4. **Reverse the Binary Array**:
  46.    - Reverse the order of bits in the binary array. This effectively reverses the binary representation of the input number.
  47.  
  48. 5. **Convert Back to Decimal**:
  49.    - Use the `getDecimalForm` function to convert the reversed binary array back to a decimal integer.
  50.    - The `getDecimalForm` function iterates through the reversed binary array, multiplying each bit by the corresponding power of 2 and accumulating the result.
  51.  
  52. 6. **Return the Result**:
  53.    - Return the decimal integer obtained in step 5 as the result of reversing the bits of the input number.
  54.  
  55. This process ensures that the input number's binary representation is reversed correctly, and the result is returned as a positive integer.
  56.  
  57. You can memorize this process as a step-by-step guide for reversing bits in a positive integer in JavaScript.
  58. */
  59. function toUnsigned32Bits(value){
  60.     return (value>>>0);
  61. }
  62.  
  63. function getBinary(n){
  64.     let arr=[];
  65.     while(n){
  66.         arr.push(n&1);
  67.         n=n>>>1;
  68.     }
  69.     // arr.reverse();
  70.     while(arr.length<32){
  71.         arr.push(0);
  72.     }
  73.     return arr;
  74. }
  75.  
  76. function getDecimalForm(arr){
  77.     let num=0;
  78.     for(let i=0;i<32;i++){
  79.         num=toUnsigned32Bits(num+arr[i]*Math.pow(2,i));
  80.     }
  81.     return num;
  82. }
  83. /**
  84.  * @param {number} n - a positive integer
  85.  * @return {number} - a positive integer
  86.  */
  87. function reverseBits(n) {
  88.     // Implement here
  89.     let binaryForm=getBinary(n);
  90.     binaryForm.reverse();
  91.     return getDecimalForm(binaryForm);
  92. }
  93.  
  94. function main() {
  95.     let t = parseInt(readLine(), 10);
  96.     while(t--) {
  97.         let n = parseInt(readLine(), 10);
  98.         let answer = reverseBits(n);
  99.         print(answer + "\n");
  100.     }
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement