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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:34:08+00:00 2026-05-24T02:34:08+00:00

I tried to write a function, which returns an integer with only the highest

  • 0

I tried to write a function, which returns an integer with only the highest bit of the input set, using a C++0x constexpr.

constexpr inline uint64_t
get_highest_bit(uint64_t p)
{
  return 
     (p|=(p>>1)),
     (p|=(p>>2)),
     (p|=(p>>4)),
     (p|=(p>>8)),
     (p|=(p>>16)),
     (p|=(p>>32)),
     (p-(p>>1));
}

This gives a compile-time failure using gcc 4.6.1.

error: expression ‘(p <unknown operator> ((p >> 1) | p))’ is not a constant-expression

Note that it works without the constexpr keyword.

My questions are:

Why does this not work?
I can see that operator|= is not a constexpr, but does it matter for built-in types?

Is there an easy way to write this function as a constexpr? I would like it to be reasonable efficient at runtime, and I care a bit about readibility.

  • 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-24T02:34:10+00:00Added an answer on May 24, 2026 at 2:34 am

    (Not tested on GCC because I don’t have 4.6, but I’ve verified that the algorithm is correct.)

    To use constexpr you must not have assignments. Therefore, you often have to write in functional form with recursion:

    #include <cstdint>
    #include <climits>
    
    constexpr inline uint64_t highestBit(uint64_t p, int n = 1) {
        return n < sizeof(p)*CHAR_BIT ? highestBit(p | p >> n, n * 2) : p - (p >> 1);
    }
    
    int main() {
        static_assert(highestBit(7) == 4);
        static_assert(highestBit(5) == 4);
        static_assert(highestBit(0x381283) == 0x200000);
        return 0;
    }
    

    You can check C++0x §[expr.const]/2 to see what expressions cannot be used in a constexpr function. In particular, the second to last item is “an assignment or a compound assignment”.

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

Sidebar

Related Questions

I've tried to write a string replace function in C, which works on a
I'm trying to write a program with a function which returns a matrix with
I need to write a function in JavaScript, which returns a state from calling
I have a function which will take student details as input and write their
I need to write a function which returns an list of sum functions, adding
I'm new to erlang. I wonder how to write a function which returns the
I tried to write a function to check key code with JavaScript. It's working
I tried to write a function that calculates a hamming distance between two codewords
Has anyone tried to dynamically select which properties they want to write to an
Ive got this function as a html helper which should write a javascript function

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.