Interview Question

    32 Bit Integer Reversal

With an input of a 32 bit integer, reverse the bits and return the number.

Input: 123
# AKA 0000 0000 0000 0000 0000 0000 0000 0111 1011
Output: 57713623040
# AKA 1101 0111 0000 0000 0000 0000 0000 0000 0000

Your code should fit into the following pseudocode:

def convert_to_bits(n) {
   return '{0:08b}'.format(n)
}
def bitwise_reverse(num) {
   #Your Code Here
}

print(convert_to_bits(123))
# 1111011
print(bitwise_reverse(123))
# 57713623040
print(convert_to_bits(bitwise_reverse(123))
# 110101110000000000000000000000000000




Answers


  • 768

    def bitwise_reverse(num):
        bitstring = '{0:032b}'.format(num)
        ret = ''
        for i in bitstring:
            ret = i + ret
        return int(ret, 2)


  • 261
    @abdullahidris1436

    Def convert_32 to 32 bits(n) {
      return '{0:08b}' . format(n)
    }
    def betwise_reverse(num) {
    #AKA 0000 0000 0000 0000 0000 0000 0000
    0111 1011
    print (57713623049(123))


  • 256

    print (1111011)


  • 128
    @ggetsin

     func(int32 t) {
      int32_t val = 0;
      for (int i < 32)
      val << i;
      return val;
    }


  • 128

    j

Interview Questions

This question was recently asked at a major tech company (ie Google, Apple, Facebook, Amazon)
We're compiling a definitive list of interview questions.

Take a practice interview and score yourself.

<< Return to Index