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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:40:53+00:00 2026-06-10T21:40:53+00:00

I need to fetch last 6 bits of a integer or Uint32. For example

  • 0

I need to fetch last 6 bits of a integer or Uint32. For example if I have a value of 183, I need last six bits which will be 110 111 ie 55.

I have written a small piece of code, but it’s not behaving as expected. Could you guys please point out where I am making a mistake?

int compress8bitTolessBit( int value_to_compress, int no_of_bits_to_compress )
{
    int ret = 0;
    while(no_of_bits_to_compress--)
    {
        std::cout << " the value of bits "<< no_of_bits_to_compress << std::endl;
        ret >>= 1;
        ret |= ( value_to_compress%2 );
        value_to_compress /= 2;
    }
    return ret;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int val = compress8bitTolessBit( 183, 5 );

    std::cout <<" the value is "<< val << std::endl;
      system("pause>nul");
    return 0;
}
  • 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-10T21:40:55+00:00Added an answer on June 10, 2026 at 9:40 pm

    You have entered the realm of binary arithmetic. C++ has built-in operators for this kind of thing. The act of “getting certain bits” of an integer is done with an “AND” binary operator.

        0101 0101
    AND 0000 1111
        ---------
        0000 0101
    

    In C++ this is:

    int n = 0x55 & 0xF;
    // n = 0x5
    

    So to get the right-most 6 bits,

    int n = original_value & 0x3F;
    

    And to get the right-most N bits,

    int n = original_value & ((1 << N) - 1);
    

    Here is more information on

    • Binary arithmetic operators in C++
    • Binary operators in general
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with the schema below. I need to fetch the last
I have a doubt to be clarified. How can i fetch the last value
I need to fetch only first record (because I need last date) of resultset,
I need to fetch all email id's of contacts which is present in contact
Here's what I need to fetch: - posts that have comments - number of
I've got a HashMap and I need to fetch an item by its integer
Can any one please let me know, that, i need to fetch last 4
I need the very last valid entry in a database table which would be
I have 4 Tables (listed bellow) and need: get last 10 Chats from Room
I have this array which gets the last table from a database. However the

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.