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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:01:05+00:00 2026-05-21T17:01:05+00:00

Is there a good and fast way in C/C++ to test if multiple variables

  • 0

Is there a good and fast way in C/C++ to test if multiple variables contains either all positive or all negative values?

Say there a 5 variables to test:

Variant 1

int test(int a[5]) {
    if (a[0] < 0 && a[1] < 0 && a[2] < 0 && a[3] < 0 && a[4] < 0) {
        return -1;
    } else if (a[0] > 0 && a[1] > 0 && a[2] > 0 && a[3] > 0 && a[4] > 0) {
        return 1;
    } else {
        return 0;
    }
}

Variant 2

int test(int a[5]) {
    unsigned int mask = 0;
    mask |= (a[0] >> numeric_limits<int>::digits) << 1;
    mask |= (a[1] >> numeric_limits<int>::digits) << 2;
    mask |= (a[2] >> numeric_limits<int>::digits) << 3;
    mask |= (a[3] >> numeric_limits<int>::digits) << 4;
    mask |= (a[4] >> numeric_limits<int>::digits) << 5;
    if (mask == 0) {
        return 1;
    } else if (mask == (1 << 5) - 1) {
        return -1;
    } else {
        return 0;
    }
}

Variant 2a

int test(int a[5]) {
    unsigned int mask = 0;
    for (int i = 0; i < 5; i++) {
        mask <<= 1;
        mask |= a[i] >> numeric_limits<int>::digits;
    }
    if (mask == 0) {
        return 1;
    } else if (mask == (1 << 5) - 1) {
        return -1;
    } else {
        return 0;
    }
}

What Version should I prefer? Is there any adavantage using variant 2/2a over 1? Or is there a better/faster/cleaner way?

  • 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-21T17:01:06+00:00Added an answer on May 21, 2026 at 5:01 pm

    I think your question and what you’re looking for don’t agree. You asked how to detect if they’re signed or unsigned, but it looks like you mean how to test if they’re positive or negative.

    A quick test for all negative:

    if ((a[0]&a[1]&a[2]&a[3]&a[4])<0)
    

    and all non-negative (>=0):

    if ((a[0]|a[1]|a[2]|a[3]|a[4])>=0)
    

    I can’t think of a good way to test that they’re all strictly positive (not zero) right off, but there should be one.

    Note that these tests are correct and portable for twos complement systems (anything in the real world you would care about), but they’re slightly wrong for ones complement or sign-magnitude. They might can be fixed if you really care.

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

Sidebar

Related Questions

Is there any way on C# to find a pixel color every 1 second
If I have an insert statement with a bunch of values where the first
I am working on a flood control for a chat system, one of the
I have a C function that produces about 6 million unique arrays. These arrays
I am currently researching GWT as a means to develop web apps for a
Several pieces of software I'm maintaining make direct connections to remote databases to get
Im really new to Grails and I try to understand how it works. I
I work on an audio processing project that needs to do a lot of
Well, my problem is what the title says. I have build a small application
I'm currently looking into the spring-security framework - great stuff so far, pretty impressed.

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.