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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:40:33+00:00 2026-05-23T11:40:33+00:00

Low level bit manipulation has never been my strong point. I will appreciate some

  • 0

Low level bit manipulation has never been my strong point. I will appreciate some help in understanding the following use case of bitwise operators.Consider…

int age, gender, height, packed_info;

. . .   // Assign values 

// Pack as AAAAAAA G HHHHHHH using shifts and "or"
packed_info = (age << 8) | (gender << 7) | height;

// Unpack with shifts and masking using "and"
height = packed_info & 0x7F;   // This constant is binary ...01111111
gender = (packed_info >> 7) & 1;
age    = (packed_info >> 8);

I am not sure what this code is accomplishing and how? Why use the magic number 0x7F ? How is the packing and unpacking accomplished?

Source

  • 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-23T11:40:34+00:00Added an answer on May 23, 2026 at 11:40 am

    As the comment says, we’re going to pack the age, gender and height into 15 bits, of the format:

    AAAAAAAGHHHHHHH
    

    Let’s start with this part:

    (age << 8)
    

    To start with, age has this format:

    age           = 00000000AAAAAAA
    

    where each A can be 0 or 1.

    << 8 moves the bits 8 places to the left, and fills in the gaps with zeroes. So you get:

    (age << 8)    = AAAAAAA00000000
    

    Similarly:

    gender        = 00000000000000G
    (gender << 7) = 0000000G0000000
    height        = 00000000HHHHHHH
    

    Now we want to combine these into one variable. The | operator works by looking at each bit, and returning 1 if the bit is 1 in either of the inputs. So:

    0011 | 0101 = 0111
    

    If a bit is 0 in one input, then you get the bit from the other input. Looking at (age << 8), (gender << 7) and height, you’ll see that, if a bit is 1 for one of these, it’s 0 for the others. So:

    packed_info = (age << 8) | (gender << 7) | height = AAAAAAAGHHHHHHH
    

    Now we want to unpack the bits. Let’s start with the height. We want to get the last 7 bits, and ignore the first 8. To do this, we use the & operator, which returns 1 only if both of the input bits are 1. So:

    0011 & 0101 = 0001
    

    So:

    packed_info          = AAAAAAAGHHHHHHH
    0x7F                 = 000000001111111
    (packed_info & 0x7F) = 00000000HHHHHHH = height
    

    To get the age, we can just push everything 8 places to the right, and we’re left with 0000000AAAAAAAA. So age = (packed_info >> 8).

    Finally, to get the gender, we push everything 7 places to the right to get rid of the height. We then only care about the last bit:

    packed_info            = AAAAAAAGHHHHHHH
    (packed_info >> 7)     = 0000000AAAAAAAG
    1                      = 000000000000001
    (packed_info >> 7) & 1 = 00000000000000G
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have some low level image/texture operations where 32-bit colors are stored as UInt32
I have just been getting into low level programming (reading/writing to memory that sort
i'm writing some low level window code for a window in x (in c++),
I would like to know some low-level capabilities of high-level languages. Off the top
I'm interested in some low-level details on how disk seeks and writes works when
I'm having a little bit of confusion regarding how low-level winsock is? I am
This is a somewhat low-level question. In x86 assembly there are two SSE instructions:
I'm creating a low level keyboard hook in c# using SetWindowsHookEx, question is how
I'm writing a low level image processing algorithm which needs to do alot of
Is there any really low level programming language that can get access the memory

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.