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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:07:09+00:00 2026-06-02T05:07:09+00:00

unsigned reverse_bits(unsigned input) { //works on 32-bit machine input = (input & 0x55555555) <<

  • 0
unsigned reverse_bits(unsigned input)
{  
     //works on 32-bit machine  
     input = (input & 0x55555555) <<  1 | (input & 0xAAAAAAAA) >>  1;
     input = (input & 0x33333333) <<  2 | (input & 0xCCCCCCCC) >>  2;
     input = (input & 0x0F0F0F0F) <<  4 | (input & 0xF0F0F0F0) >>  4;
     input = (input & 0x00FF00FF) <<  8 | (input & 0xFF00FF00) >>  8;
     input = (input & 0x0000FFFF) << 16 | (input & 0xFFFF0000) >> 16;

      return input;
}

how does this work?

  • 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-02T05:07:10+00:00Added an answer on June 2, 2026 at 5:07 am

    Suppose I have a hand of 8 cards:

    7 8 9 10 J Q K A
    

    How can we reverse them? One way is to swap adjacent pairs:

    8 7 10 9 Q J A K
    

    Then, swap adjacent groups of 2: exchange 8 7 and 10 9, etc:

    10 9 8 7 A K Q J
    

    Finally, exchange groups of four, which is half the size of 8:

    A K Q J 10 9 8 7
    

    Done.

    You can do this in different orders. Why? Because the exchanges are stable with respect to each other. When we exchange the upper half of the cards with the lower half, for instance, the pairs stay in the same order. Or when we exchange pairs, the halves stay in the same order.

    This is what the code is doing with the bit operations. For instance to swap pairs we can use the mask 01010101 to pick out the even bits, and 10101010 to pick out the odd bits, using the bitwise AND operation:

      ABCDEFGH     ABCDEFGH
    & 01010101   & 10101010
    ----------   ----------
    = 0B0D0F0H     A0C0E0G0
    

    Remember, the rule for and is that given some bit value X, X & 1 = X and X & 0 = 0. The 1 bits in the mask preserve the value, and the 0 bits in the mask produce 0. This is called masking because it looks exactly like a mask used in spray-painting, etc. The 1 bits “cover” the places you don’t want to “paint” with zero.

    Next, the left result is shifted left one bit, and the right result shifted right. This brings about the swap:

      B0D0F0H0     0A0C0E0G
    

    Finaly, the two are combined with logical OR. The rule for OR is that X or 0 is X. The two parts each have 0 where the other has nonzero, and so the bits simply merge:

      B0D0F0H0
    | 0A0C0E0G
    ----------
    = BADCFEHG
    

    And now the pairs are swapped.

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

Sidebar

Related Questions

When I convert an unsigned 8-bit int to string then I know the result
for (unsigned int i = 1; i <= 100; i++) { if (i &
. unsigned int fname_length = 0; //fname length equals 30 file.read((char*)&fname_length,sizeof(unsigned int)); //fname contains
float Calculate(const string &query) { std::cout << Query: << query << \n; unsigned int
Given an unsigned int A (32 bit), and another unsigned int B, where B's
unsigned int *pMessageLength, MessageLength; char *pszParsePos; ... //DATA into pszParsePos ... printf(\nMessage Length\nb1: %d\nb2:
Unsigned int into a short and back. Is this possible? How to do it
Given: unsigned int a, b, c, d; I want: d = a * b
I'm initializing an unsigned short int with a = 0xff (all bits are set).
I have unsigned char* , want to convert it to std::string . Can you

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.