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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:06:55+00:00 2026-06-17T11:06:55+00:00

I have a strange problem in C++, I’m sure I’m missing something obvious but

  • 0

I have a strange problem in C++, I’m sure I’m missing something obvious but cannot figure out what.

The purpose of the function is to set some bits of a variable. From my tests, it all seems to work however it looks like the result of the operation is not assigned back to the variable.

uint16_t setBits(uint16_t target, unsigned int source, int offset, int bitCount) {
    unsigned int mask = ~0;
    mask = mask >> ((sizeof(mask) * 8) - bitCount);
    int rightShift = (sizeof(mask) * 8 - bitCount - offset);
    mask = mask << rightShift;
    source = source << rightShift;
    printHex(target);
    printHex((target & ~mask) | (source & mask));
    target = (target & ~mask) | (source & mask);
    printHex(target);
    return target;
}

uint16_t group;
group = 0xabcd;
unsigned int source = 0x12;
group = setBits(group, source, 4, 8);

This would print:

0x0000abcd
0x0120abcd
0x0000abcd

Which for me means that (target & ~mask) | (source & mask) gives the correct result (0x0120abcd), however this result is not assigned back to target (which is still set to 0x0000abcd). Any idea what I’m doing wrong?

  • 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-17T11:06:56+00:00Added an answer on June 17, 2026 at 11:06 am

    Look at the type for target:

    uint16_t target
    

    It’s only 16 bits, but you try to mask in bits higher than that.

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

Sidebar

Related Questions

I know that this question has already been asked, but I have strange problem
I have strange problem. I set a JProgressBar: private JProgressBar progressBar; public void foo()
I'm building a webapi and I have strange problem. When I do something like
I have strange problem with the delay function here using the HTML function with
I have a strange problem. I have created a simple function to convert from
I have strange problem, with calling Lua function from C++. I have in Lua:
I have strange problem .. i have a function to display the time with
I have a strange problem I cannot handle. I don't know what to do,
I have a strange problem. Background that was set by CSS for .article-container and
i have strange problem doing reporting: i have numerous clients with different issued invoices.

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.