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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:31:36+00:00 2026-06-11T18:31:36+00:00

I want reverse the binary unsigned short gf_t = 44 // = 00101100 in

  • 0

I want reverse the binary

unsigned short gf_t  = 44 // = 00101100

in 00110100 in C language. How i will able to for that using bitwise operators?

pdta: My computer have 32 bits pattern.

  • 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-11T18:31:37+00:00Added an answer on June 11, 2026 at 6:31 pm

    When in doubt, see the Bit Twiddling Hacks page. In fact, there you can find a very simple algorithm that does what you want…

    Reverse bits the obvious way

    unsigned int v;     // input bits to be reversed
    unsigned int r = v; // r will be reversed bits of v; first get LSB of v
    int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end
    
    for (v >>= 1; v; v >>= 1)
    {   
      r <<= 1;
      r |= v & 1;
      s--;
    }
    r <<= s; // shift when v's highest bits are zero 
    

    On October 15, 2004, Michael Hoisie pointed out a bug in the original version. Randal E. Bryant suggested removing an extra operation on May 3, 2005. Behdad Esfabod suggested a slight change that eliminated one iteration of the loop on May 18, 2005. Then, on February 6, 2007, Liyong Zhou suggested a better version that loops while v is not 0, so rather than iterating over all bits it stops early.

    There is also, however, several nifty approaches documented there. You can look into those and try to understand them for learning 🙂 For example, here is one particular interesting form…

    Reverse an N-bit quantity in parallel in 5 * lg(N) operations:

    unsigned int v; // 32-bit word to reverse bit order
    
    // swap odd and even bits
    v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1);
    // swap consecutive pairs
    v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2);
    // swap nibbles ... 
    v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);
    // swap bytes
    v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8);
    // swap 2-byte long pairs
    v = ( v >> 16             ) | ( v               << 16);
    

    Note that if sizeof(unsigned short) * CHAR_BIT is 16, the appropriate usage would only require the first 4 transpositions — see as follows:

    unsigned short v;
    
    // swap odd and even bits
    v = ((v >> 1) & 0x5555) | ((v & 0x5555) << 1);
    // swap consecutive pairs
    v = ((v >> 2) & 0x3333) | ((v & 0x3333) << 2);
    // swap nibbles ... 
    v = ((v >> 4) & 0x0F0F) | ((v & 0x0F0F) << 4);
    // swap bytes
    v = ((v >> 8) & 0x00FF) | ((v & 0x00FF) << 8);
    

    That being said, why not just use uint16_t (if it’s available)?

    Here is working example (see ideone):

    #include <stdio.h>
    #include <assert.h>
    #include <stdint.h>
    
    inline uint16_t reverse(uint16_t v) {
      v = ((v >> 1) & 0x5555) | ((v & 0x5555) << 1); /* swap odd/even bits */
      v = ((v >> 2) & 0x3333) | ((v & 0x3333) << 2); /* swap bit pairs */
      v = ((v >> 4) & 0x0F0F) | ((v & 0x0F0F) << 4); /* swap nibbles */
      v = ((v >> 8) & 0x00FF) | ((v & 0x00FF) << 8); /* swap bytes */
      return v;
    }
    
    main() {
      uint16_t gf_t = 44;
      printf("%hu\n", reverse(gf_t));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to reverse a 2d array of type char using std::reverse() function in
I have an AnimatedSprite that after it finishes the animation I want to reverse
First I want to clearify that I mean by reverse engineering something like decompiling
I'm using Express JS and I want a functionality similar to Django's reverse function.
I have a developer eyes-only activity that I don't want end users to reverse
I have one list that I want to take a slice of, reverse that
For example, you want to reverse a string, will there two ways: first: String
I want to reverse my tinycarousel slider so that i can see again my
I have a String and want to reverse it so that some characters get
I want to reverse a sequence in Clojure without using the reverse function, and

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.