454,445 Members | 1,329 Online
Need help? Post your question and get tips & solutions from a community of 454,445 IT Pros & Developers. It's quick & easy.

# How to determine a short variable signed or not

 P: n/a 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 Jan 3 '07 #1
7 Replies

 P: n/a Z-Z wrote: 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. -- Chris "hopefully not Pyecroft" Dollin A rock is not a fact. A rock is a rock. Jan 3 '07 #2

 P: n/a 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 "Chris Dollin" I used the following macro to determine variable signed or not, but onlyfind that it is useless for a short or char variable. I think that itmightcaused 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 signedornot? 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. -- Chris "hopefully not Pyecroft" Dollin A rock is not a fact. A rock is a rock. Jan 3 '07 #3

 P: n/a Sorry, the macro used in the previous message should be ISUNSIGNED, no ISUNSIGNED2. Bests, "Z-Z" Z-Z wrote: >>I used the following macro to determine variable signed or not, but onlyfind that it is useless for a short or char variable. I think that itmightcaused 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 signedornot? I read the code.What on Earth are you trying to do that depends on whether a short issigned or not? It feels ... fragile. Maybe if you explained theactual problem we could offer a more robust solution.--Chris "hopefully not Pyecroft" DollinA rock is not a fact. A rock is a rock. Jan 3 '07 #4

 P: n/a Z-Z wrote: (PLEASE DON'T TOP-POST. Fixed here.) "Chris Dollin" Z-Z wrote: >>I used the following macro to determine variable signed or not, but onlyfind that it is useless for a short or char variable. I think that itmightcaused 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 signedornot? I read the code.What on Earth are you trying to do that depends on whether a short issigned or not? It feels ... fragile. Maybe if you explained theactual problem we could offer a more robust solution. 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 =0 && ~a>= 0) doesn't work. Was there an actual problem you were trying to solve? -- Chris "hopefully not Pyecroft" Dollin "Never ask that question!" Ambassador Kosh, /Babylon 5/ Jan 3 '07 #5

 P: n/a please don't top post. I have rearranged your post Z-Z wrote:"Chris Dollin" =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. -- Chris "hopefully not Pyecroft" Dollin A rock is not a fact. A rock is a rock. 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? -- Nick Keighley Jan 3 '07 #6

 P: n/a 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 Jan 3 '07 #7

 P: n/a 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 Jan 3 '07 #8

### This discussion thread is closed

Replies have been disabled for this discussion.