Return the value of the reverse of a number's binary representation


  • 0
    S

    example

    argument: 11
    binary: 1011
    reverse: 1101
    return: 13


  • 0
    I
    extension Int{
        public func toBinary() ->String
        {
            return String(self,radix:2)
        }
    }
    
    extension String{
        public func binaryToInt() -> Int {
            return Int(strtoul(self, nil, 2))
        }
    }
    
    func valueOfNumberReverseBinaryValue(n:Int) ->Int
    {
        let binaryNum = n.toBinary()
        let reverseBinary = String(binaryNum.characters.reverse())
        return reverseBinary.binaryToInt()
    }
    

  • 0
    T
    int Solve(int n)
    {
      int result = 0;
    	
      for (int i = 1; i <= n; i = i << 1)
      {
         result = result << 1;
         if((n & i) > 0) result++;
      }	
    
      return result;
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.