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 9192243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:52:31+00:00 2026-06-17T20:52:31+00:00

I am trying to calculate the time complexity for a function return below- int

  • 0

I am trying to calculate the time complexity for a function return below-

int isPowerof2(unsigned int num)
{
     if(((~num)&1) == 1)
          return 1;
      return 0;
}

I think it should be O(1) but i am not sure the complexity for negation. Can somebody please help me in understanding how to identify the complexity for this. Thanks!

Edit- What if in case of a single number consider it as an n inputs and the functions checks for power of 2 , what would be the complexity in that case

  • 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-06-17T20:52:32+00:00Added an answer on June 17, 2026 at 8:52 pm

    A power of two, represented in binary, has exactly one bit set, all others are zero.

    Subtracting one will invert all bits right of of and including the rightmost one:

    110101100 - 1=> 110101011 (in the case of zero, all bits get inverted)
    

    We postulate that num & (num - 1) will evaluate to zero if and only if num is a power of two.

    If num is in fact a power of two, there is a single bit set in total, subtracting one will render that bit zero and set all bits to its right to one.

    It follows, that num and num - 1 cannot share any set bits. Thus, num & (num - 1) evaluates to 0.

    If num is not a power of two (and not zero), there are at least two bits set. When subtracting one, only the rightmost set bit is changed and those to its right, it follows, that the other ones will not be affected.

    Thus, num and num - 1 share at most one set bit. We conclude that num & (num - 1) cannot evaluate to zero for num not zero and not a power of two.

    It follows that the correct check is: num && !(num & (num - 1))

    Complexity: On a regular computer, all binary operations happen in constant time. Because there is a fixed amount of constant time operations, the entire function will return in constant time: O(1).

    When you perform n calls to that function, you do a constant time amount of work each time you call it. When n doubles, the amount of work doubles. It follows, that the complexity for that case is O(n).

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

Sidebar

Related Questions

I am trying to calculate a date time difference but I'm getting some strange
Friends,I am trying to calculate the difference between two date&time entries that the user
I am trying to calculate time with datejs and timejs but I am not
In fact i am trying to calculate the time a function takes to complete
I'm trying to calculate time blocks from given date range. Here's my attempt: <?php
I'm trying to calculate the time it takes to receive all data from a
hey there i'm trying to calculate execution time of a simple multithreaded programme on
I am trying to calculate the time between two times on the current date
I am trying to calculate the time it takes a query to run using
So, I am trying to calculate the time between two dates that fits certain

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.