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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:47:55+00:00 2026-05-20T09:47:55+00:00

In this question, assume all integers are unsigned for simplicity. Suppose I would like

  • 0

In this question, assume all integers are unsigned for simplicity.

Suppose I would like to write 2 functions, pack and unpack, which let you pack integers of smaller width into, say, a 64-bit integer. However, the location and width of the integers is given at runtime, so I can’t use C bitfields.

Quickest is to explain with an example. For simplicity, I’ll illustrate with 8-bit integers:

             * *
bit #    8 7 6 5 4 3 2 1
myint    0 1 1 0 0 0 1 1

Suppose I want to “unpack” at location 5, an integer of width 2. These are the two bits marked with an asterisk. The result of that operation should be 0b01. Similarly, If I unpack at location 2, of width 6, I would get 0b100011.

I can write the unpack function easily with a bitshift-left followed by a bitshift right.

But I can’t think of a clear way to write an equivalent “pack” function, which will do the opposite.

Say given an integer 0b11, packing it into myint (from above) at location 5 and width 2 would yield

             * *
bit #    8 7 6 5 4 3 2 1
myint    0 1 1 1 0 0 1 1

Best I came up with involves a lot of concatinating bit-strings with OR, << and >>. Before I implement and test it, maybe somebody sees a clever quick solution?

  • 1 1 Answer
  • 3 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-20T09:47:55+00:00Added an answer on May 20, 2026 at 9:47 am

    Off the top of my head, untested.

    int pack(int oldPackedInteger, int bitOffset, int bitCount, int value) {
        int mask = (1 << bitCount) -1;
        mask <<= bitOffset;
        oldPackedInteger &= ~mask;
        oldPackedInteger |= value << bitOffset;
        return oldPackedInteger;
    }
    

    In your example:

    int value = 0x63;
    value = pack(value, 4, 2, 0x3);
    

    To write the value “3” at an offset of 4 (with two bits available) when 0x63 is the current value.

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

Sidebar

Related Questions

For this question, let us assume that we will want to show the face
(For the purposes of this question, let us assume that one is intentionally not
This is a question about generic c++ event driven applications design. Lets assume that
This is as equally a drupal question as a PHP question, I assume. I
I was asked this question: You are given two lists of integers, each of
I assume that there is probably no satisfactory answer to this question, but I
It was hard to phrase this question. I'll assume most of you are aware
Well, this is not at all an optimization question. I am writing a (for
This seems like a simple question on its surface but I thought I'd reach
this question has been answered several times and i have seen almost all related

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.