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

  • SEARCH
  • Home
  • 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 8399831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:24:35+00:00 2026-06-09T21:24:35+00:00

Here’s a riddle. Imagine I have the following C++ function: template<uint8_t MASK> uint8_t Foo(uint8_t

  • 0

Here’s a riddle.

Imagine I have the following C++ function:

template<uint8_t MASK>
uint8_t Foo(uint8_t val)
{
    uint8_t step = 0;
    uint8_t result = 0;
    if(MASK & 0x01) {result |= (val & 0x01) >> step; ++step;}
    if(MASK & 0x02) {result |= (val & 0x02) >> step; ++step;}
    //...etc...
    if(MASK & 0x80) {result |= (val & 0x80) >> step; ++step;}

    return result;
}

When I instantiate this function as such (all values below are just examples values):

uint8_t someval = Foo<0xAA>(44);

The compiler optimizes out the if statements in Foo() because it knows at compile time what the result of said if() statement are.

This is nice and well, but trying to do the same in C is problematic because of the creation of the local variable step.

If step wasn’t there, you could do a large #define like this:

#define Foo(MASK, val) (\
    ((MASK & 0x01) ? (val & 0x01) : 0) | \
    ((MASK & 0x02) ? (val & 0x02) : 0) | \
    ...etc...
    ((MASK & 0x80) ? (val & 0x80) : 0) | \
    )

But with step, I’m kind of at an impasse. What can I do to obtain the same functionality of C++ template in C for C++ template function with local variables?

Note that using inline C functions is not an answer, as the compiler will not know at compile time the value of MASK, and thus all the comparisons will not be optimized and will accordingly be part of the final compiled output.

Also note that changing the #define to include the result value is also not an answer, since this changes the “function’s” signature.

And lastly, I am fully aware that there may be no answer to this riddle.

  • 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-09T21:24:37+00:00Added an answer on June 9, 2026 at 9:24 pm

    Have your macro try to do what the template does; create an inline function–

    #define Foo(MASK, val) inline uint8_t Foo_##MASK(uint8_t val) \
    { \
       uint8_t step = 0; \
        uint8_t result = 0; \
        if(MASK & 0x01) {result |= (val & 0x01) >> step; ++step;} \
        if(MASK & 0x02) {result |= (val & 0x02) >> step; ++step;} \
        //...etc...
        if(MASK & 0x80) {result |= (val & 0x80) >> step; ++step;} \
    \
        return result;\
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is the code in a function I'm trying to revise. This example works
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here we go again, the old argument still arises... Would we better have a
Here is the problem that I am trying to solve. I have two folders
Here's the basic setup: I have a thin bar at the top of a
Here is my problem : I have a post controller with the action create.
Here is the problem I'm trying to solve for my game. I have this

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.