190. Reverse binary
Simple difficulty
Inverts the binary bits of a given 32bit unsigned integer.
Tips:
 Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be specified as signed integer types and should not affect your implementation, because the internal binary representation of an integer is the same whether it is signed or unsigned.
 In Java, the compiler uses Binary complement Notation to represent signed integers. Therefore, in example 2, the input represents a signed integer3 and the output represents a signed integer1073741825.
Example 1:
Input: n = 00000010100101000001111010011100 Output: 964176192 (00111001011110000010100101000000) Explanation: the input binary string 000000 1010010100000 1111010011100 represents an unsigned integer 43261596, Therefore, 964176192 is returned, and its binary representation is 001110010111100000010100101000000.
Example 2:
Input: n = 11111111111111111111111111111101 Output: 3221225471 (10111111111111111111111111111111) Explanation: the input binary string 11111111111111111111111101 represents an unsigned integer 4294967293, Therefore, 3221225471 is returned, and its binary representation is 10111111111111111111111111111.
Tips:
 The input is a binary string with a length of 32
Problem solution
Solution 1: bit by bit addition
At the beginning of reading this question, I didn't understand what problem to solve. I foolishly began to knock the code, because I used java (I'm not familiar with it. I thought it was binary. I was as fierce as a tiger. I submitted a wrong answer). Later, I'll study the example carefully. The original Java gave an integer. It becomes clear that it is through decimal – > binary – > inverting binary – > decimal.
The above idea is correct, but after thinking about it, the input length is 32. If we know that the original binary index is 1 in a certain bit, we can get the inverted index through 31 index to complete the inversion. This is also our common idea in flipping arrays.
 Decimal – > binary
So how to find a binary is 1? i use the traversal method. i shift i bit left with 1 every time, so that i can get binary 10 (decimal 2), 100 (decimal 4), 1000 (decimal 8)..., and then get these binary and the original number. If the result is not 0, then this bit is 1.
 Binary – > decimal
After getting the index, we can get the inverted index. The corresponding binary values are obtained by moving the binary values of index31 to the left, and then the corresponding binary values are obtained.
public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int ans = 0; for(int i = 0; i < 32; i++){//Maximum 32 bits if((n & (1 << i)) != 0){//Judge whether the ith binary is not 0 ans += 1 << (31i);//31i is inverted I } } return ans; } }
Solution 2: bit exchange
This is also the code given by the official answer. It's the first time to see..... What? After reading the comments, it suddenly opened up. One line at a time

n = n >> 1 & M1  (n & M1) << 1;

n >> 1 & M1
We know that n > > 1, which means that n shifts one bit to the right in the binary stored in the computer; If it is associated with M1, it retains the original 2468... Even bit binary

(n & M1) << 1
N & M1, which is the same as M1, retains the original binary of 13579... Odd digits; Then move one bit to the left.

n >> 1 & M1  (n & M1) << 1
The above realizes the exchange of 12, 34, 56, 78... Adjacent parity bits, and the result is 2 1 4 3 6 5 8 7


n = n >> 2 & M2  (n & M2) << 2;

n >> 2 & M2
n> > 2, this is n, which shifts two bits to the right in the binary stored in the computer; If it is combined with M2, the original binary of 3, 4, 7, 8, 11, 12... Bits is retained

(n & M2) << 2
N & M2 is the sum of and M2, which retains the original binary of 1, 2, 5, 6, 9, 10; Then move two bits to the left.

n >> 2 & M2  (n & M2) << 2;
The above realizes the exchange of 21 and 43, 65 and 87... And the result becomes 4 3 2 1 and 8 7 6 5


n = n >> 4 & M4  (n & M4) << 4;
Realize 4bit interchange
4 3 2 1 8 7 6 5 becomes 8 7 6 5 4 3 2 1

n = n >> 8 & M8  (n & M8) << 8;
Realize 8bit interchange

n >> 16  n << 16;
Realize 16 bit interchange
class Solution { private: const uint32_t M1 = 0x55555555; // 01010101010101010101010101010101 const uint32_t M2 = 0x33333333; // 00110011001100110011001100110011 const uint32_t M4 = 0x0f0f0f0f; // 00001111000011110000111100001111 const uint32_t M8 = 0x00ff00ff; // 00000000111111110000000011111111 public: uint32_t reverseBits(uint32_t n) { n = n >> 1 & M1  (n & M1) << 1; n = n >> 2 & M2  (n & M2) << 2; n = n >> 4 & M4  (n & M4) << 4; n = n >> 8 & M8  (n & M8) << 8; return n >> 16  n << 16; } }; Author: LeetCodeSolution Link: https://leetcodecn.com/problems/reversebits/solution/diandaoerjinzhiweibyleetcodesoluyhxz/ Source: force buckle( LeetCode) The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For noncommercial reprint, please indicate the source.