leetcode190-inverted binary bits

Problem Description:

Reverse the binary bits of a given 32-bit unsigned integer.

Example 1:

输入: 00000010100101000001111010011100
输出: 00111001011110000010100101000000
解释: 输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596,
     因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000

Example 2:

输入:11111111111111111111111111111101
输出:10111111111111111111111111111111
解释:输入的二进制串 11111111111111111111111111111101 表示无符号整数 4294967293,
     因此返回 3221225471 其二进制表示形式为 10111111111111111111111111111111

prompt:

Please note that in some languages ​​(such as Java), there is no unsigned integer type. In this case, both input and output will be designated as signed integer types and should not affect your implementation, because the internal binary representation is the same regardless of whether the integer is signed or unsigned.
In Java, the compiler uses two's complement notation to represent signed integers. Therefore, in Example 2 above, the input represents a signed integer -3, and the output represents a signed integer -1073741825.

Problem-solving ideas:

1. Flip a binary number, similar to an integer flip, you need to get the last bit of the binary number first. It can be realized by AND operation (n & 1).
The & operation satisfies two equal 1s, and the result is 1, otherwise it is 0. Here and 1 is used for the AND operation, and the final result is the same as the last bit of n.

2. After obtaining the last value, you need to get the second-to-last value, which can be achieved by shifting: n>>1, so that the second-to-last value is shifted to the last. We only need to shift 32 times to get all the binary bit values ​​of n.

3. Use res to save each binary bit.

Implementation code

public class Solution {
    
    
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
    
    
        int res=0;      //用res保存n的各个二进制位
        for(int i=0;i<32;i++){
    
    
            //这里让res先左移的原因是因为从最后一个位开始,每个位左移32-i位,则第1位左移31次,所以res左移放在最前面
            res=res<<1;      
            //如果当前n的末位是1,那么res++,保证左移完后,res的该位也为1
            if((n & 1)==1){
    
    
                res++;
            }
            //n右移,得到n的倒数第i位
            n=n>>1;
        }
        return res;
    }
}

important point:

There are no unsigned numbers in Java, only signed numbers. The shift of signed numbers is an
arithmetic shift. The object of arithmetic shift is a signed number. The sign bit remains unchanged during the shift.
For positive numbers, the vacancies appearing after shifting are all filled with 0.
For negative numbers, the original code value part of the negative number is the same as the true value, the sign bit remains the same when shifting, and the space is added to 0.
The inverse code bits of the negative number are opposite to the original code of the negative number except for the sign bit. Therefore, the code added after shifting It should be contrary to the original code, all add 1
so, the result of shifting the negative number to the right in Java is still a negative number.
Here, if the binary bits are reversed and filled from high to low, an error may occur.
Like the code written at the beginning of the title

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/113921619