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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:54:25+00:00 2026-05-25T16:54:25+00:00

Bit Reversal I found this code for reversing the bits in an integer x

  • 0

Bit Reversal

I found this code for reversing the bits in an integer x (assume a 32bit value):

unsigned int
reverse(register unsigned int x)
{
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return((x >> 16) | (x << 16));
}

I am unable to understand the logic/algorithm behind this code. What is the purpose of all the magic numbers?

  • 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-25T16:54:26+00:00Added an answer on May 25, 2026 at 4:54 pm

    Let’s look at how it’s done for an 8 bit value:

    The first line in the function takes every second bit and moves it left or right:

    12345678  -->  1-3-5-7-  -->  -1-3-5-7  -->  21436587
                   -2-4-6-8       2-4-6-8-
    

    The second line takes groups of two bits and moves left or right:

    21436587  -->  21--65--  -->  --21--65  -->  43218765
                   --43--87       43--87--
    

    The third line takes groups of four bits and moves left or right:

    43218765  -->  4321----  -->  ----4321  -->  87654321
                   ----8765       8765----
    

    Now the bits are reversed. For a 32 bit value you need two more steps that moves bits in groups of 8 and 16.

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

Sidebar

Related Questions

I am trying to do bit reversal in a byte. I use the code
Bit of a bizarre one, this, and I'm tempted to assume it may be
Bit of a strange one this. Please forgive the semi-pseudo code below. I have
Bit of an obscure one this. My setup is all running on my local
A bit of a neophyte haskell question, but I came across this example in
A bit of background first: I am using base code from a remote SVN
I try to define a 64 bits width integer using C language on Ubnutu
Bit of a weird one this. I'm rendering datasets on a map and need
Bit long title, comes down to this: <?php class error { public function error($errormsg
Bit tricky to communicate but hope this makes sense. I have 8 div containers

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.