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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:50:09+00:00 2026-05-10T15:50:09+00:00

Given an integer typedef: typedef unsigned int TYPE; or typedef unsigned long TYPE; I

  • 0

Given an integer typedef:

typedef unsigned int TYPE; 

or

typedef unsigned long TYPE; 

I have the following code to reverse the bits of an integer:

TYPE max_bit= (TYPE)-1;  void reverse_int_setup() {     TYPE bits= (TYPE)max_bit;      while (bits <<= 1)         max_bit= bits; }  TYPE reverse_int(TYPE arg) {     TYPE    bit_setter= 1, bit_tester= max_bit, result= 0;      for (result= 0; bit_tester; bit_tester>>= 1, bit_setter<<= 1)         if (arg & bit_tester)             result|= bit_setter;     return result; } 

One just needs first to run reverse_int_setup(), which stores an integer with the highest bit turned on, then any call to reverse_int(arg) returns arg with its bits reversed (to be used as a key to a binary tree, taken from an increasing counter, but that’s more or less irrelevant).

Is there a platform-agnostic way to have in compile-time the correct value for max_int after the call to reverse_int_setup(); Otherwise, is there an algorithm you consider better/leaner than the one I have for reverse_int()?

Thanks.

  • 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. 2026-05-10T15:50:10+00:00Added an answer on May 10, 2026 at 3:50 pm
    #include<stdio.h> #include<limits.h>  #define TYPE_BITS sizeof(TYPE)*CHAR_BIT  typedef unsigned long TYPE;  TYPE reverser(TYPE n) {     TYPE nrev = 0, i, bit1, bit2;     int count;      for(i = 0; i < TYPE_BITS; i += 2)     {         /*In each iteration, we  swap one bit on the 'right half'          of the number with another on the left half*/          count = TYPE_BITS - i - 1;  /*this is used to find how many positions                                      to the left (and right) we gotta move                                      the bits in this iteration*/          bit1 = n & (1<<(i/2)); /*Extract 'right half' bit*/         bit1 <<= count;         /*Shift it to where it belongs*/          bit2 = n & 1<<((i/2) + count);  /*Find the 'left half' bit*/         bit2 >>= count;         /*Place that bit in bit1's original position*/          nrev |= bit1;   /*Now add the bits to the reversal result*/         nrev |= bit2;     }     return nrev; }  int main() {     TYPE n = 6;      printf('%lu', reverser(n));     return 0; } 

    This time I’ve used the ‘number of bits’ idea from TK, but made it somewhat more portable by not assuming a byte contains 8 bits and instead using the CHAR_BIT macro. The code is more efficient now (with the inner for loop removed). I hope the code is also slightly less cryptic this time. 🙂

    The need for using count is that the number of positions by which we have to shift a bit varies in each iteration – we have to move the rightmost bit by 31 positions (assuming 32 bit number), the second rightmost bit by 29 positions and so on. Hence count must decrease with each iteration as i increases.

    Hope that bit of info proves helpful in understanding the code…

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

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try checking your .csproj fie (open in notepad) for the… May 11, 2026 at 8:23 pm
  • Editorial Team
    Editorial Team added an answer The output for _CrtDbgReportW depends on how you set it… May 11, 2026 at 8:23 pm
  • Editorial Team
    Editorial Team added an answer I am not sure what you're trying to do with… May 11, 2026 at 8:23 pm

Related Questions

I thought I really understood this, and re-reading the standard (ISO 9899:1990) just confirms
I'm currently trying to verify whether or not, given an unsorted array A of
I need to find the smallest power of two that's greater or equal to
I want to write a function that returns the nearest next power of 2

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.