Leetcode brushing problem detailed explanation difficulty: simple Java implementation number 190, inverted binary bit operation detailed explanation

Source: LeetCode
Link: https://leetcode-cn.com/problems/reverse-bits
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Number 190, reverse the binary digits

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

示例 1:

Input: 00000010100101000001111010011100
Output: 00111001011110000010100101000000
Explanation: The input binary string 00000010100101000001111010011100 represents an unsigned integer 43261596,
so 964176192 is returned, and its binary representation is 00111001011110000010100101000000.
示例 2:
Input: 11111111111111111111111111111101
Output: 10111111111111111111111111111111
Explanation: The input binary string 11111111111111111111111111111101 represents an unsigned integer 4294967293,
so it returns 3221225471 whose binary representation is 10111111111111111111111111111111.

prompt:

  • 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. Directly use the string reversal and hexadecimal conversion that comes with java.
    Originally, I wanted to directly use the Integer.parseInt(str, radix) method to directly convert the binary string into an int integer, but the conversion function only supports MAX.VALUE Half (-65536/2 <i <65535/2). So finally Long.valueOf(str, radix) is used to convert the hexadecimal system, and finally the Long type is converted to the int type.
    This solution is less efficient!
	public int reverseBits(int n) {
    
    
        String bitstr = Integer.toBinaryString(n);
        while (bitstr.length()<32)
            bitstr = "0".concat(bitstr);
        return Long.valueOf(String.valueOf(new StringBuilder(bitstr).reverse()),2).intValue();
 
  1. Use bit operations
    Use a new int variable res to store the inverted binary.
    In the loop:
    res=res<<1, shift one bit to the left,The current rightmost bit of res is 0;
    res = res | n&1, n&1 can get to the rightmost digit 1 of binary 00001011, and the OR operation in res|n&1 can put the specified number on the rightmost digit of res (The prerequisite is that the rightmost bit of res is 0), such as 0110|1 =0111, 0110|0 = 0110;
    n = n>>1, then shift n to the right by one bit, and delete which bit has been transferred;
    each cycle only operates on the rightmost bit 0 or 1;
    this solution is more efficient!
public int reverseBits(int n) {
    
    
        int res = 0;
        for (int i = 0; i < 32; i++) {
    
    
            res=res<<1;  //左移一位,当前res最右边的位是0;
            res = res | n&1; // n&1能取到二进制 00001011最右边的位数 1,res|n&1中的或运算能把指定数放到res最右边的位上(前提条件是res最右边的位上是0),比如 0110|1 =0111, 0110|0 = 0110;
            n = n>>1;//再将n右移一位,将已经转移的哪一位删掉;
            //每次循环只操作最右边的位上的0或1;
        }
        return res;
    }

Guess you like

Origin blog.csdn.net/qq_37079157/article/details/109921068
Recommended