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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:46:22+00:00 2026-05-17T17:46:22+00:00

Check whether a number x is nonzero using the legal operators except ! .

  • 0

Check whether a number x is nonzero using the legal operators except !.

Examples: isNonZero(3) = 1, isNonZero(0) = 0

Legal ops: ~ & ^ | + << >>

  • Note : Only bitwise operators should be used. if, else, for, etc. cannot be used.
  • Edit1 : No. of operators should not exceed 10.
  • Edit2 : Consider size of int to be 4 bytes.
int isNonZero(int x) {
return ???;
}

Using ! this would be trivial , but how do we do it without using ! ?

  • 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-17T17:46:22+00:00Added an answer on May 17, 2026 at 5:46 pm

    The logarithmic version of the adamk function:

    int isNotZero(unsigned int n){
      n |= n >> 16;
      n |= n >> 8;
      n |= n >> 4;
      n |= n >> 2;
      n |= n >> 1;
      return n & 1;
    };
    

    And the fastest one, but in assembly:

    xor eax, eax
    sub eax, n  // carry would be set if the number was not 0
    xor eax, eax
    adc eax, 0  // eax was 0, and if we had carry, it will became 1
    

    Something similar to assembly version can be written in C, you just have to play with the sign bit and with some differences.

    EDIT: here is the fastest version I can think of in C:

    1) for negative numbers: if the sign bit is set, the number is not 0.

    2) for positive: 0 - n will be negaive, and can be checked as in case 1. I don’t see the - in the list of the legal operations, so we’ll use ~n + 1 instead.

    What we get:

    int isNotZero(unsigned int n){ // unsigned is safer for bit operations
       return ((n | (~n + 1)) >> 31) & 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how to write a regular expression to check whether a number is consisting only
Is there any way to check whether a file is locked without using a
I wrote a function to check whether a number is prime or not: prime
Is there any predefined function in c++ to check whether the number is square
can anyone tell me the regular expression to check whether the entered number is
I'm trying to check whether the number is a prime(by dividing it by all
Is there any method or quick way to check whether a number is an
I want to I check whether a string is in ASCII or not. I
How can I check whether a variable is defined in Ruby? Is there an
I need to check whether a page is being redirected or not without actually

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.