473,394 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to determine a short variable signed or not

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
Jan 3 '07 #1
7 1621
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
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

"Chris Dollin" <ch**********@hp.comwrote in message
news:en**********@murdoch.hpl.hp.com...
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 #3
Z-Z
Sorry, the macro used in the previous message should be
ISUNSIGNED, no ISUNSIGNED2.

Bests,

"Z-Z" <z-*@hotmail.comwrote in message
news:3o********************@news1.nokia.com...
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" <ch**********@hp.comwrote in message
news:en**********@murdoch.hpl.hp.com...
>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 #4
Z-Z wrote:

(PLEASE DON'T TOP-POST. Fixed here.)
"Chris Dollin" <ch**********@hp.comwrote in message
news:en**********@murdoch.hpl.hp.com...
>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.
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 <quantifierpre-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?

--
Chris "hopefully not Pyecroft" Dollin
"Never ask that question!" Ambassador Kosh, /Babylon 5/

Jan 3 '07 #5
please don't top post. I have rearranged your post

Z-Z wrote:"Chris Dollin" <ch**********@hp.comwrote in message
news:en**********@murdoch.hpl.hp.com...
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.
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
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


Jan 3 '07 #7
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
Jan 3 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
99
by: Glen Herrmannsfeldt | last post by:
I was compiling a program written by someone else about six years ago, and widely distributed at the time. It also includes makefiles for many different systems, so I know it has been compiled...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
10
by: Vijay Kumar R Zanvar | last post by:
Hi clc, Please elaborate the warning: F:\Vijay\C> type unsigned2.c #include <stdio.h> #include <stdlib.h> int
5
by: _Andy_ | last post by:
I'm looking for the alogithm to take a piece of 8-bit audio data, and scale it to a 16-bit short. I got as far as: private static short ByteToShort(byte b) { int word = 0; word |= ( ( b &...
4
by: rakesh.Mysore | last post by:
HI I have a problem in casting short to ushort in my project. i have a ushort value of 38,143 now it is converted into short value of 27,393 and stored in variable.(when i casted ushort to...
4
by: slougheed | last post by:
I encountered a problem after we had converted our declarations of 'unsigned short int' to uint16_t. In one instance, whoever did the conversion failed to delete the 'short' keyword so we had a...
7
by: stefan.istrate | last post by:
I have a short int variable and I'm trying to read it with scanf("%d",&x); but I'm getting a warning message which sounds like this: warning: format '%d' expects type 'int*', but argument 2 has...
36
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.