Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3672866
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:39:42+00:00 2026-05-19T02:39:42+00:00

Possible Duplicates: Query about working out whether number is a power of 2 What

  • 0

Possible Duplicates:
Query about working out whether number is a power of 2
What does this function do?

n & (n-1) – where can this expression be used ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:39:43+00:00Added an answer on May 19, 2026 at 2:39 am

    It’s figuring out if n is either 0 or an exact power of two.

    It works because a binary power of two is of the form 1000...000 and subtracting one will give you 111...111. Then, when you AND those together, you get zero, such as with:

      1000 0000 0000 0000
    &  111 1111 1111 1111
      ==== ==== ==== ====
    = 0000 0000 0000 0000
    

    Any non-power-of-two input value (other than zero) will not give you zero when you perform that operation.

    For example, let’s try all the 4-bit combinations:

         <----- binary ---->
     n      n    n-1   n&(n-1)
    --   ----   ----   -------
     0   0000   0111    0000 *
     1   0001   0000    0000 *
     2   0010   0001    0000 *
     3   0011   0010    0010
     4   0100   0011    0000 *
     5   0101   0100    0100
     6   0110   0101    0100
     7   0111   0110    0110
     8   1000   0111    0000 *
     9   1001   1000    1000
    10   1010   1001    1000
    11   1011   1010    1010
    12   1100   1011    1000
    13   1101   1100    1100
    14   1110   1101    1100
    15   1111   1110    1110
    

    You can see that only 0 and the powers of two (1, 2, 4 and 8) result in a 0000/false bit pattern, all others are non-zero or true.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Retrieve (or simulate) full query from PDO prepared statement I can't figure
Possible Duplicate: What does the colon sign “:” do in a SQL query? Simple
Possible Duplicates: Why is lock(this) {...} bad? In C# it is common to use
Possible Duplicate: Query about Html <input type=password> tag…? I want change the character displayed
Possible Duplicates: Simulating group_concat MySQL function in MS SQL Server 2005? Concat groups in
Possible Duplicates: Remove duplicates in large MySql table Can I extract the extract records
Possible Duplicates: Why would a sql query have “where 1 = 1” Why would
Possible Duplicates: JavaScript query string get querystring with jQuery Is there an object/method in
Possible Duplicate: SQL - How can I remove duplicate rows? SQL query to delete
Possible Duplicate: Query Microsoft Access MDB Database using LINQ and C# Im working with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.