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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:43:22+00:00 2026-05-15T21:43:22+00:00

I need the most efficient way (in cpu cycles) to determine if two numbers

  • 0

I need the most efficient way (in cpu cycles) to determine if two numbers have the same/different sign. But the catch is if either number is zero I need to be able to distinguish it from numbers with same/different signs (ie. zero is treated as a “third” sign). The following code is similar to what I need, but the return values can be anything as long as there are only three distinct return values.

int foo(int x, int y) {
    if (x * y > 0) return 1;
    if (x * y < 0) return -1;
    return 0;
}

For my specific problem, the values are in the range [-6, 6] and X is guaranteed to not be 0. I found a solution to find if two numbers have the same sign, and altered it to get the following solution.

return y? (((x^y) >= 0)? 1 : -1) : 0;

There should be some bitops/comparisons that give faster results than using multiplication, branching, comparisons.

  • 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-15T21:43:23+00:00Added an answer on May 15, 2026 at 9:43 pm

    Here is another version (with ugly, non-portable bit manipulation tricks):

    int foo(int x, int y) {
        return ((x^y) >> 4) - ((x^(-y)) >> 4);
    }
    

    Some explanations:

    • ((x^y) >> 4) is -1 if exactly one of x and y is negative, otherwise it is 0.
    • ((x^(-y)) >> 4) is -1 if exactly one of x and -y is negative, otherwise it is 0.
    • If x > 0 and y > 0, the result will be 0 – (-1) = 1.
    • If x < 0 and y < 0, the result will be 0 – (-1) = 1.
    • If x > 0 and y = 0, the result will be 0 – 0 = 0.
    • If x < 0 and y = 0, the result will be (-1) – (-1) = 0.
    • If x > 0 and y < 0, the result will be (-1) – 0 = -1.
    • If x < 0 and y > 0, the result will be (-1) – 0 = -1.

    Assumes two’s complement arithmetic and assumes that >> shifts with sign-extension.

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

Sidebar

Related Questions

I need to find the most efficient way of matching multiple regular expressions on
The most efficient way to code powers of two is by bit shifting of
I need to randomly 'sort' a list of integers (0-1999) in the most efficient
I am looking for the most efficient/direct way to do this simple C/C++ operation:
Possible Duplicate: What is the most efficient way to clone a JavaScript object? I
what is the best/most efficient way of creating dynamic CSS with Rails. I am
I need to implement a browser based component (most likely java applet since it
I need WCF service with 3 endpoints High Normal Low Most of messages will
I need to read smallish (few MB at the most, UTF-8 encoded) XML files,
I've gone through most of the example code and I still need some help.

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.