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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:57:20+00:00 2026-05-12T08:57:20+00:00

An interesting problem I’ve been pondering the past few days is how to copy

  • 0

An interesting problem I’ve been pondering the past few days is how to copy one integer’s bits into another integer at a given position in the destination integer. So, for example, given the destination integer 0xdeadbeef and the source integer 0xabcd, the idea would be to get a result of 0xabcdbeef (given a destination position of 16 bits) or 0xdeabcdef (given a destination position of 8 bits).

With the arbitrary limitation of avoiding conditionals or loops (allowing myself to use just mathematical/bitwise operations), I developed the following function (C++)

int setbits(int destination, int source, int at, int numbits)
{
    int ones = ((1<<(numbits))-1)<<at;
    return (ones|destination)^((~source<<at)&ones);
}

where at is the place where the source bits should be copied into the destination number (0-31) and numbits is the number of bits being copied from source (1-32). As far as I can tell, this algorithm works for all values except for at = 0 and numbits = 32 (the case when the entire destination integer is being overwritten by the source integer) due to the fact that 1<<32 results in 1 (since the shift wraps around) as opposed to 0.

My questions are:

  1. How is this normally done? Are there any particularly notable algorithms used (by notable, I’m asking if there are any particularly efficient tricks that can be used to do this)?
  2. Does my algorithm work as well as I think it does (that is, works for all values except at = 0 and numbits = 32)?
  3. Related to 1), is there any way to do this only using mathematical/bitwise operators? The algorithm for all values is trivial using conditions or loops, so I’m not interested in that.

Algorithm design is usually a weak point for me, so I have no idea whether or not my algorithm is ‘as good as it gets’ when only using mathematical/bitwise operations. Thanks

  • 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-12T08:57:21+00:00Added an answer on May 12, 2026 at 8:57 am

    I don’t think it’s the case that 1<<32 wraps (otherwise, why doesn’t 2<<31 also wrap?), instead I think that internally modulus 32 is applied to the second operator, so that 1<<32 is actually equivalent to 1<<0. Also, consider changing the parameters types from “int” to “unsigned int”. To get the value of “ones” without running into the “1<<32” problem, you can do this:

    unsigned int ones = (0xffffffff >> (32-numbits)) << at;
    

    I don’t believe there are any “standard” methods for this kind of operation. I’m sure there are other ways of using bitwise operators in different ways to achieve the same outcome, but your algorithm is as good as any.

    Having said that, though, maintainability and documentation is also important. Your function would benefit from the algorithm being documented with a comment, especially to explain how you use the bitwise XOR — which is clever, but not easy to understand at first glance.

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

Sidebar

Related Questions

Interesting problem. In a project scope change, I broke one application into two applications.
I have an interesting problem which I've been looking into and would appreciate some
Interesting problem I ran into recently: I implemented a Stream class (a wrapping stream
I ran into an interesting problem at work today. I got a request to
I ran into an interesting problem. On my site, i swap out content dynamically
I have one interesting problem. I must parse mail body (regular expression), get some
An interesting problem I ran into today: what is the fastest way to count
I run into an interesting problem I want to understand. I have a table:
Have come against an interesting problem and I'm stumped. Basically on one of our
I ran into an interesting problem while debugging SWIG typemaps today. Anyone care to

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.