How to determine a short variable signed or not

Z

Z-Z

Hi,

I used the following macro to determine variable signed or not, but only
find that it is useless for a short or char variable. I think that it might
caused by value conversion rules. I used VC.

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

Who knows the reason and other methods to determine short varible signed or
not?

Thanks.
Zoe
 
C

Chris Dollin

Z-Z said:
I used the following macro to determine variable signed or not, but only
find that it is useless for a short or char variable. I think that it might
caused by value conversion rules. I used VC.

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

Who knows the reason and other methods to determine short varible signed or
not?

I read the code.

What on Earth are you trying to do that depends on whether a short is
signed or not? It feels ... fragile. Maybe if you explained the
actual problem we could offer a more robust solution.
 
Z

Z-Z

Hi,

Ok. I only want to know why the following two cases have different results :

Case 1:
unsigned short number1 = 1;
signed short number2 = 1;

if(ISUNSIGNED(number1))
{
printf("number 1 is unsigned \n");
}
if(ISUNSIGNED2(number2))
{
printf("number 2 is unsigned\n");
}
No printout and seemed that it can't figure out variable unsigned .

Case 2:
unsigned int number1 = 1;
signed int number2 = 1;

if(ISUNSIGNED(number1))
{
printf("number 1 is unsigned \n");
}
if(ISUNSIGNED2(number2))
{
printf("number 2 is unsigned\n");
}
Printout: number1 is unsigned. It works!

Bests,
Zoe
 
Z

Z-Z

Sorry, the macro used in the previous message should be
ISUNSIGNED, no ISUNSIGNED2.

Bests,

Z-Z said:
Hi,

Ok. I only want to know why the following two cases have different results
:

Case 1:
unsigned short number1 = 1;
signed short number2 = 1;

if(ISUNSIGNED(number1))
{
printf("number 1 is unsigned \n");
}
if(ISUNSIGNED2(number2))
{
printf("number 2 is unsigned\n");
}
No printout and seemed that it can't figure out variable unsigned .

Case 2:
unsigned int number1 = 1;
signed int number2 = 1;

if(ISUNSIGNED(number1))
{
printf("number 1 is unsigned \n");
}
if(ISUNSIGNED2(number2))
{
printf("number 2 is unsigned\n");
}
Printout: number1 is unsigned. It works!

Bests,
Zoe
 
C

Chris Dollin

Z-Z wrote:

(PLEASE DON'T TOP-POST. Fixed here.)
Ok. I only want to know why the following two cases have different results :

Case 1:
unsigned short number1 = 1;
signed short number2 = 1;

if(ISUNSIGNED(number1))
{
printf("number 1 is unsigned \n");
}
if(ISUNSIGNED2(number2))
{
printf("number 2 is unsigned\n");
}
No printout and seemed that it can't figure out variable unsigned .

Case 2:
unsigned int number1 = 1;
signed int number2 = 1;

if(ISUNSIGNED(number1))
{
printf("number 1 is unsigned \n");
}
if(ISUNSIGNED2(number2))
{
printf("number 2 is unsigned\n");
}
Printout: number1 is unsigned. It works!

In your implementation (and many others), unsigned short is
promoted to (signed) int. This is the "value-preserving"
promotion rule (as opposed to the "[un]signedness-preserving"
rule that I seem to recall was present in <quantifier> pre-ANSI
C implementations).

Your macro

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

doesn't work. Was there an actual problem you were trying to
solve?
 
N

Nick Keighley

please don't top post. I have rearranged your post


please remove sigs when replying

Ok. I only want to know why the following two cases have different results :

Case 1:
unsigned short number1 = 1;
signed short number2 = 1;

if(ISUNSIGNED(number1))
{
printf("number 1 is unsigned \n");
}
if(ISUNSIGNED2(number2))
{
printf("number 2 is unsigned\n");
}
No printout and seemed that it can't figure out variable unsigned .

Case 2:
unsigned int number1 = 1;
signed int number2 = 1;

if(ISUNSIGNED(number1))
{
printf("number 1 is unsigned \n");
}
if(ISUNSIGNED2(number2))
{
printf("number 2 is unsigned\n");
}
Printout: number1 is unsigned. It works!

but WHY do you want to do this? What is your real-world problem?
 
Z

Z-Z

Thanks for your answer and correction.

There is no actual problem. In fact, this came from a execrise: write code
to figure out whether a variable is signed or not.

Yes, you are right. The rule can be found in K&R C:

Sounds a silly question: could I say that the exercise question is
meaningless for a shorter and char?
Zoe
 
Z

Z-Z

Thanks for your correction and answer.
but WHY do you want to do this? What is your real-world problem?

There is no actual problem. In fact, this came from a execrise: write code
to figure out whether a variable is signed or not. I tried the above macro
and found that it couldn't work for short type.Now the reason is known.
Chris said that this caused by "value-preserving".

Zoe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top