Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

1--Reverse Bits Algo


// JavaScript program  to
// reverse bits of a number
 
    // function to reverse bits of a number
    
    function reverseBits(n)
    {
        let rev = 0;
   
        // traversing bits of 'n'
        // from the right
        while (n > 0)
        {
            // bitwise left shift
            // 'rev' by 1
            rev <<= 1;
   
            // if current bit is '1'
            if ((n & 1) == 1)
                rev ^= 1;
   
            // bitwise right shift
            //'n' by 1
            n >>= 1;
        }
        // required number
        return rev;
    }
 
console.log(reverseBits(1000));
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #Bits #Algo
ADD COMMENT
Topic
Name
4+5 =