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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:45:06+00:00 2026-05-25T16:45:06+00:00

I came across this question in a forum. The answer is something like this:

  • 0

I came across this question in a forum. The answer is something like this:

#define ISUNSIGNED(a) (a >= 0 && ~a >= 0) 

//Alternatively, assuming the argument is to be a type, one answer would use type casts: 

#define ISUNSIGNED(type) ((type)0 - 1 > 0)

I have a few questions regarding this.Why do we need to check ~a >= 0? What is the second solution all about? I did not understand the statement: “argument is to be a type”. More importantly the author states that first #define will not work in ANSI C (but will work in K&R C). Why not?

  • 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-25T16:45:07+00:00Added an answer on May 25, 2026 at 4:45 pm
    #define ISUNSIGNED(a) (a >= 0 && ~a >= 0) 
    

    For a signed value which is positive, a >= 0 will be true (obviously) and ~a >= 0 will be false since we’ve flipped the bits so the sign bit is now set, resulting in a negative value. The entire expression is therefore false.

    For a signed value which is negative, a >= 0 will be false (obviously) and the rest of the expression will not be evaluated; the overall result for the expression is false.

    For an unsigned value, a >= 0 will always be true (obviously, since unsigned values can’t be negative). If we flip the bits then ~a >= 0 is also true, since even with the most significant bit (the sign bit) set to 1, it’s still treated as a positive value.

    So, the expression returns true if the original value and its bitwise inverse are both positive, i.e. it’s an unsigned value.

    #define ISUNSIGNED(type) ((type)0 - 1 > 0)
    

    This is to be called with a type rather than a value: ISUNSIGNED(int) or ISUNSIGNED(unsigned int), for example.

    For an int, the code expands to

    ((int)0 - 1 > 0)  
    

    which is false, since -1 is not greater than 0.

    For an unsigned int, the code expands to

    ((unsigned int)0 - 1 > 0) 
    

    The signed 1 and 0 literals in the expression are promoted to unsigned to match the first 0, so the entire expression is evaluated as an unsigned comparison. 0 - 1 in unsigned arithmetic will wrap around resulting in the largest possible unsigned value (all bits set to 1), which is greater than 0, so the result is true.

    As to why it would work with K&R C, but not ANSI C, maybe this article can shed some light:

    When an unsigned char or unsigned short is widened, the result type is
    int if an int is large enough to represent all the values of the
    smaller type. Otherwise, the result type is unsigned int. The value
    preserving rule produces the least surprise arithmetic result for most
    expressions.

    I guess that means that when comparing an unsigned short to 0, for example, the unsigned value is converted to a signed int which breaks the behaviour of the macro.

    You can probably work around this by having (a-a) which evaluates to either signed or unsigned zero as appropriate, instead of the literal 0 which is always signed.

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

Sidebar

Related Questions

I recently came across this interview question (posted in a forum somehwere... looks like
In trying to answer this question for myself I came across this nugget, after
In attempting to Answer this Question I came across this in the output of
I came across this question on an interview questions thread. Here is the question:
I came across this question after searching for a ODBC or JDBC. To my
I came across this question: say given two weights 1 and 3, u can
I came across this question on transcender: What should you apply to a field
I just came across this question about initializing local variables. Many of the answers
Today I came across this question: you have a code static int counter =
I am preparing for my interview and came across this question: Consider that i

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.