Readable ruby code

  • 0

    Find the all "1" masking number which has the same "binary length" as the input number.

    For example, input = 0d5, masking number = 0b111; input = 0d2, masking number = 0b11

    def find_complement(num)
      num ^ mask(num)
    def mask(number)
      (1 << number.to_s(2).length) - 1

Log in to reply

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