473,657 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1632
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(n umber1))
{
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(n umber1))
{
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**********@h p.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.comwr ote in message
news:3o******** ************@ne ws1.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(n umber1))
{
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(n umber1))
{
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**********@h p.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**********@h p.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(n umber1))
{
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(n umber1))
{
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 <quantifierpr e-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**********@h p.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(n umber1))
{
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(n umber1))
{
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
6135
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. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
99
9038
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 with many different compilers. I got compile errors when it used va_arg to fetch an argument of type short. That seemed a little strange to me, so I changed it to int and it compiled just fine. So now I wonder, just what is the rule for va_arg...
34
16664
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 - 3;
10
4341
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
2651
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 & 0x01 ) << 1 ); word |= ( ( b & 0x02 ) << 2 ); word |= ( ( b & 0x04 ) << 3 ); word |= ( ( b & 0x08 ) << 4 );
4
10983
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 short) 1. now how can i get back value 38,143 from short variable value 27,393 ? 2. how can i check whether the conversion of value happned when i
4
5184
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 'uint16_t short'. The compiler (GNU 3.3.5) allows this but ignores the original unsigned keyword and initialzes the variable as a signed short int. I created a very simple test program just to see what was happening: #include <stdio.h> ...
7
46352
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 type 'short int*' How can I avoid this warning and still read with "scanf"? I mention that I don't want to use "cout". Maybe this problem is for C (not for C ++), but the program is in C++ because i use after this the Standard Template Library.
36
3795
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 to read text or handle arrays of arbitrary length. I don't have the expertise in C of many folks here so I feel like I'm offering a small furry animal for sacrifice to a big armour plated one... but will offer it anyway. Please do suggest...
0
8323
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.