Determine whether a number is 2 raised to the Nth power (Alibaba interview question)

        illustrate:

        The solution to this problem requires bitwise comparison , which is also basic knowledge in the principles of computer composition. In computers, numerical values ​​are usually represented and processed in binary form. Bitwise comparison is the operation of comparing the corresponding bits of the binary representation of two numbers. In the principles of computer composition, bitwise comparisons are usually implemented through bitwise logical operations , such as bitwise AND, bitwise OR, bitwise XOR, etc. These bit logic operations can be implemented through logic gate circuits. The underlying processors and logic circuits of computers use bitwise comparisons to perform various operations and judgments.

        In programming, you can also use bitwise operators to perform bitwise comparisons. For example, the bitwise AND operator &can perform a bitwise AND operation on the corresponding bits of the binary representation of two numbers. The result is a new number that represents the result of the logical AND operation on the corresponding bits of the two numbers.

        Therefore, bitwise comparison is an important concept in the principles of computer composition and one of the commonly used operations in programming. It can provide efficient solutions in situations such as determining whether a number is 2 raised to the nth power.

        Key code:
if(n&(n-1) == 0)
        Analysis:

        Using if (n & (n - 1) == 0) to determine whether a number n is 2 raised to the nth power is based on the following principle: If a number is 2 raised to the nth power, then there is only one digit in its binary representation. is 1, and the remaining bits are all 0. For example, 2^3 = 8 is represented as 1000 in binary. When we subtract 1 from this number n, we will find that the rightmost 1 becomes 0, and all the bits to the right of that bit become 1. For example, 8 - 1 = 7 is represented in binary as 0111. If we perform a bitwise AND operation on these two binary numbers, the result should be 0. This is because the result of the bitwise AND operation will be 1 only if the corresponding bits of both numbers are 1. In this case, since the rightmost 1's in the binary representations of n and (n - 1) are different bits, the result of the bitwise AND operation should be 0. Therefore, if n & (n - 1) evaluates to 0, then n is 2 raised to the nth power. The time complexity of this judgment method is O(1), because only one bitwise AND operation is required. This is very efficient for determining whether a number is 2 raised to the nth power.

        Review lower bit logic operations:

        Bit logic operations mainly include operations such as bitwise AND (AND), bitwise OR (OR), bitwise exclusive OR (XOR), and bitwise negation (NOT).

        Bitwise AND (AND) :&implemented using the operator. For the binary representation of two numbers, the bitwise AND operation will cause the result to be 1 when both numbers on the corresponding bits are 1; otherwise, it is 0.

        Bitwise OR (OR) : implemented using the `|` operator. For the binary representation of two numbers, the bitwise OR operation will result in 1 if at least one of the two numbers on the corresponding bit is 1; otherwise, it will be 0.

        Bitwise exclusive OR (XOR) : implemented using the `^` operator. For the binary representation of two numbers, the bitwise XOR operation will cause the result to be 1 when the two numbers on the corresponding bits are different; when they are the same, the result will be 0.

        Bitwise negation (NOT) : implemented using the `~` operator. For the binary representation of a number, the bitwise inversion operation changes a 0 on each bit to a 1 and a 1 to a 0.

Guess you like

Origin blog.csdn.net/qq_67801847/article/details/132226587