Leetcode Reverse Bits

Source: Internet
Author: User

Reverse bits of a given the unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represent Ed in binary as 00111001011110000010100101000000).

Follow up:
If This function is called many times, what would you optimize it?

Converted to a 2 binary string followed by a reversal, and the end of the number of less than 32 bits of the number of 0 (multiplied by the X-th square of 2).

def reverse_bits (n)     2** (32-n.to_s (2). Length) *n.to_s (2). Reverse.to_i (2) End

This method moves the left one at a time, adds the original last number at the end, and loops 32 times to get the number of flips.

uint32_t reversebits (uint32_t N) {    0;      for (int0 ; i++,n/=2)         = (m<<1) + (n%2);     return m;}

Leetcode Reverse Bits

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.