Didn't think it could be done in Ruby?


  • -1
    Y

    Here you go :)

    LEN = 32 # bits
    
    def reverse_bits(num)
      return if num >= 2**32
      
      num_b = num.to_s(2)
      num_b = "0" * (LEN - num_b.length) + num_b # pad 0's
      
      num_b.reverse.to_i(2)
    end
    

Log in to reply
 

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