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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:54:38+00:00 2026-06-03T10:54:38+00:00

I was studying the radix sort algorithm but I could not understand some of

  • 0

I was studying the radix sort algorithm but I could not understand some of the original source code.

static void rad_sort_u(unsigned *from, unsigned *to, unsigned bit)
{
    if (!bit || to < from + 1) return;

    unsigned *ll = from, *rr = to - 1,tmp;
    while (1) {
        /* find left most with bit, and right most without bit, swap */
        while (ll < rr && !(*ll & bit)) ll++;
        while (ll < rr &&  (*rr & bit)) rr--;
        if (ll >= rr) break;
        swap(*ll, *rr);
    }

    if (!(bit & *ll) && ll < to) ll++;
    bit >>= 1;

    rad_sort_u(from, ll, bit);
    rad_sort_u(ll, to, bit);
}

/* sort signed ints: flip highest bit, sort as unsigned, flip back */
static void radix_sort(int *a, const size_t len)
{
    size_t i;
    unsigned *x = (unsigned*) a;

    for (i = 0; i < len; i++) 
            x[i] ^= INT_MIN;

    rad_sort_u(x, x + len, INT_MIN);

    for (i = 0; i < len; i++) 
            x[i] ^= INT_MIN;
}

I don’t know why its using this line
for (i = 0; i < len; i++)
x[i] ^= INT_MIN;

I know its xor but I don’t understand the use of this operator in this context.

  • 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-03T10:54:43+00:00Added an answer on June 3, 2026 at 10:54 am

    It is toggling the MSB (most significant bit).

    INT_MIN is different depending on the compiler and system in use, to my understanding, but it will generally be something like 0x80000000 in hex, which would evaluate to something like 10000…0 in binary.

    If you XOR any bit with one, you toggle it:

    eg: if y = A xor B
    
    y | A B
    ==+====
    0   0 0
    1   0 1
    1   1 0
    0   1 1
    
    y | A 1
    ==+====
    1   0 1
    0   1 1
    
    Therefore
    A xor 1 = !A
    

    So, when that line is executed, the highest bit of x[i] is being toggled. If it was zero, it is now one. If it was one, it is now zero.

    In short: XOR any value, X, with 0, you get the original value, X. XOR any value X, with 1, you get the complement of X, !X.

     Y | X A
    ===+====
     X   X 0
    !X   X 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was studying an open source code where I came across the following line
i studying TDateTime functions and procedure, but not found something that allow me to
Studying some code from a codeigniter tut, the following preg_match pattern has me baffled:
studying some sample code from an iOS programming course (cs193p fall2010) i came across
I am new to studying jquery.ajax. Through some tutorials, I tried some code by
i am self-studying asp.net and was trying some small projects to help understand the
While studying Java tutorials, Reflection and Late Binding have confused me. In some tutorials,
While studying the Collection API, we find that some methods ( add , remove
When studying a snippet of unknown Python code, I occasionally bump into the varName.methodName()
I am studying an existing Perl program, which includes the following line of code:

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.