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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:41:08+00:00 2026-06-01T15:41:08+00:00

I am working on a function that will essentially see which of two ints

  • 0

I am working on a function that will essentially see which of two ints is larger. The parameters that are passed are 2 32-bit ints. The trick is the only operators allowed are ! ~ | & << >> ^ (no casting, other data types besides signed int, *, /, -, etc..).

My idea so far is to ^ the two binaries together to see all the positions of the 1 values that they don’t share. What I want to do is then take that value and isolate the 1 farthest to the left. Then see of which of them has that value in it. That value then will be the larger.
(Say we use 8-bit ints instead of 32-bit).
If the two values passed were 01011011 and 01101001
I used ^ on them to get 00100010.
I then want to make it 00100000 in other words 01xxxxxx -> 01000000
Then & it with the first number
!! the result and return it.
If it is 1, then the first # is larger.

Any thoughts on how to 01xxxxxx -> 01000000 or anything else to help?

Forgot to note: no ifs, whiles, fors etc…

  • 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-01T15:41:10+00:00Added an answer on June 1, 2026 at 3:41 pm

    Here’s a loop-free version which compares unsigned integers in O(lg b) operations where b is the word size of the machine. Note the OP states no other data types than signed int, so it seems likely the top part of this answer does not meet the OP’s specifications. (Spoiler version as at the bottom.)

    Note that the behavior we want to capture is when the most significant bit mismatch is 1 for a and 0 for b. Another way of thinking about this is any bit in a being larger than the corresponding bit in b means a is greater than b, so long as there wasn’t an earlier bit in a that was less than the corresponding bit in b.

    To that end, we compute all the bits in a greater than the corresponding bits in b, and likewise compute all the bits in a less than the corresponding bits in b. We now want to mask out all the ‘greater than’ bits that are below any ‘less than’ bits, so we take all the ‘less than’ bits and smear them all to the right making a mask: the most significant bit set all the way down to the least significant bit are now 1.

    Now all we have to do is remove the ‘greater than’ bits set by using simple bit masking logic.

    The resulting value is 0 if a <= b and nonzero if a > b. If we want it to be 1 in the latter case we can do a similar smearing trick and just take a look at the least significant bit.

    #include <stdio.h>
    
    // Works for unsigned ints.
    // Scroll down to the "actual algorithm" to see the interesting code.
    
    // Utility function for displaying binary representation of an unsigned integer
    void printBin(unsigned int x) {
        for (int i = 31; i >= 0; i--) printf("%i", (x >> i) & 1);
        printf("\n");
    }
    // Utility function to print out a separator
    void printSep() {
        for (int i = 31; i>= 0; i--) printf("-");
        printf("\n");
    }
    
    int main()
    {
        while (1)
        {
            unsigned int a, b;
    
            printf("Enter two unsigned integers separated by spaces: ");
            scanf("%u %u", &a, &b);
            getchar();
    
            printBin(a);
            printBin(b);
            printSep();
    
                /************ The actual algorithm starts here ************/
    
            // These are all the bits in a that are less than their corresponding bits in b.
            unsigned int ltb = ~a & b;
    
            // These are all the bits in a that are greater than their corresponding bits in b.
            unsigned int gtb = a & ~b;
    
            ltb |= ltb >> 1;
            ltb |= ltb >> 2;
            ltb |= ltb >> 4;
            ltb |= ltb >> 8;
            ltb |= ltb >> 16;
    
            // Nonzero if a > b
            // Zero if a <= b
            unsigned int isGt = gtb & ~ltb;
    
            // If you want to make this exactly '1' when nonzero do this part:
            isGt |= isGt >> 1;
            isGt |= isGt >> 2;
            isGt |= isGt >> 4;
            isGt |= isGt >> 8;
            isGt |= isGt >> 16;
            isGt &= 1;
    
                /************ The actual algorithm ends here ************/
    
            // Print out the results.
            printBin(ltb); // Debug info
            printBin(gtb); // Debug info
            printSep();
            printBin(isGt); // The actual result
        }
    }
    

    Note: This should work for signed integers as well if you flip the top bit on both of the inputs, e.g. a ^= 0x80000000.

    Spoiler

    If you want an answer that meets all of the requirements (including 25 operators or less):

    int isGt(int a, int b)
    {
        int diff = a ^ b;
        diff |= diff >> 1;
        diff |= diff >> 2;
        diff |= diff >> 4;
        diff |= diff >> 8;
        diff |= diff >> 16;
    
        diff &= ~(diff >> 1) | 0x80000000;
        diff &= (a ^ 0x80000000) & (b ^ 0x7fffffff);
    
        return !!diff;
    }
    

    I’ll leave explaining why it works up to you.

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

Sidebar

Related Questions

I was just working on a function that I needed to return two values,
I am working on a function that will give me a Kermit CRC value
I am working on a function that will take a low number and a
I'm working on a function that will return a context menu for use within
I am working on a function that will be used by no less than
I need a working code for a function that will return a random string
I'm working on a friends list type function that will display a user's friends
I've been working on trying to write a function that will grab the POST
I'm working on a function that handles events from a number of buttons and
I'm working with a PHP function that takes a string and converts all of

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.