Explain the meaning of the code ((N & (n-1)) = = 0)

Source: Internet
Author: User

Idea: Preliminary view it is difficult to analyze the meaning of the expression at a glance, we might like to analyze the example, assuming that n = 5, the binary is represented as 101, then n-1 = 4, the binary is expressed as, 5 & 4 = 101 & 100 = 100 = 4! = 0, let's look at more examples below

5 4 101  - 4 6 5  the 101 4 7 6 111  the 6 8 7  + 0111 0

We will find that to make the expression equal to 0,n-1, we must make the n-1 of n be 1 high, that is to say, satisfy the condition:

1 =0

Let's see if there are those numbers that can meet the requirements:

 10  & 01  = 0 -------2  ^1  100  & 011  = 0 -------2  ^2  1000  & 0111  = 0 -------2  ^3  01111  = 0 -------2  ^4   > ... 

The so-called code ((N & (n-1)) = = 0) means that n satisfies the n-th-square of 2

The problem is usually to examine the ability to analyze and solve problems.

Explain the meaning of the code ((N & (n-1)) = = 0)

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.