473,769 Members | 6,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unary negation operator question

I searched through the newsgroup for this and found the answer but
wanted to make sure because of something that came up.

I want the absolute value of a 'short int' so to avoid the dangers of
overflow I am doing the follow:

short int x = -4; /* want abs(x) */
unsigned short int abs_val;

....

/* already performed check to make sure x is negative */
abs_val = -(unsigned short int)x;

I take it that the above is how I should safely get the absolute value
of a negative number. I want to confirm this because my Lint package
says that there is a:
"Loss of sign(assignment )(int to unsigned short)."

Thanks!

Nov 14 '05 #1
17 1974
joshc wrote:
/* already performed check to make sure x is negative */
abs_val = -(unsigned short int)x;

I take it that the above is how I should safely get the absolute value of a negative number. I want to confirm this because my Lint package
says that there is a:
"Loss of sign(assignment )(int to unsigned short)."


I just wanted to add that if I cast that whole thing to an unsigned
short int then I don't get that message but I'm still curious why Lint
warns me without the cast.

Nov 14 '05 #2
joshc wrote:
...
I want the absolute value of a 'short int' so to avoid the dangers of
overflow I am doing the follow:

short int x = -4; /* want abs(x) */
unsigned short int abs_val;

...

/* already performed check to make sure x is negative */
abs_val = -(unsigned short int)x;

I take it that the above is how I should safely get the absolute value
of a negative number.


Let's see what happens in this expression.

Initially, the operand ('x') is implicitly promoted to type 'int' (the
value is preserved). Then the conversion to 'unsigned short' causes the
original value to wrap around zero/SHORT_MAX+1 limit in accordance with
the rules of modulo arithmetics, i.e. the original negative value turns
into the positive value 'SHORT_MAX+1+x' . This value is once again
implicitly promoted to type 'int' (the value is preserved). And now the
unary '-' is applied to the operand, resulting in a negative value
'-SHORT_MAX-1-x' of 'int' type. Now, this final value is squeezed into a
variable of type 'unsigned short'. This will cause another wraparound.
The final value is positive '-SHORT_MAX-1-x+SHORT_MAX+1'. It is indeed
equal to '-x'.

It appears to be working (I hope I didn't miss anything), but if I were
you I wouldn't jump through all those hoops and employ all that
wraparound trickery to get to the negation of the original value. I
would do it this way instead

/* already performed check to make sure x is negative */
abs_val = -x;

This just makes more sense than the original version.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #3
joshc wrote:
joshc wrote:
/* already performed check to make sure x is negative */
abs_val = -(unsigned short int)x;

I take it that the above is how I should safely get the absolute

value
of a negative number. I want to confirm this because my Lint package
says that there is a:
"Loss of sign(assignment )(int to unsigned short)."


I just wanted to add that if I cast that whole thing to an unsigned
short int then I don't get that message but I'm still curious why Lint
warns me without the cast.


I don't exactly understand why the above cast helps you to get rid of
the warning. But if you really want to use the cast, it would probably
make more sense to do it like this

abs_val = (unsigned short int) -x;

Read my previous message for more details.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #4
Andrey Tarasevich wrote:

Let's see what happens in this expression.

Initially, the operand ('x') is implicitly promoted to type 'int' (the value is preserved). Then the conversion to 'unsigned short' causes the original value to wrap around zero/SHORT_MAX+1 limit in accordance with the rules of modulo arithmetics, i.e. the original negative value turns into the positive value 'SHORT_MAX+1+x' . This value is once again
implicitly promoted to type 'int' (the value is preserved). And now the

I don't see why after the negation the value would be "implicity
promoted to type 'int'." I cast x to an (unsigned short int) so why
would it be cast back to a signed int?

It appears to be working (I hope I didn't miss anything), but if I were you I wouldn't jump through all those hoops and employ all that
wraparound trickery to get to the negation of the original value. I
would do it this way instead

/* already performed check to make sure x is negative */
abs_val = -x;

This just makes more sense than the original version.


The problem with that way is that you can have overflow if x =
SHRT_MIN because abs(SHRT_MIN) can be greater than SHRT_MAX.

Nov 14 '05 #5

Andrey Tarasevich wrote:
joshc wrote:
joshc wrote:
/* already performed check to make sure x is negative */
abs_val = -(unsigned short int)x;

I take it that the above is how I should safely get the absolute

value
of a negative number. I want to confirm this because my Lint package says that there is a:
"Loss of sign(assignment )(int to unsigned short)."


I just wanted to add that if I cast that whole thing to an unsigned
short int then I don't get that message but I'm still curious why Lint warns me without the cast.


I don't exactly understand why the above cast helps you to get rid of
the warning. But if you really want to use the cast, it would

probably

What I meant was if I do the following:

abs_val = (unsigned short int)(-(unsigned short int)x);

That seems to get rid of the warning.

thx.

Nov 14 '05 #6
joshc wrote:
...
Let's see what happens in this expression.

Initially, the operand ('x') is implicitly promoted to type 'int'

(the
value is preserved). Then the conversion to 'unsigned short' causes

the
original value to wrap around zero/SHORT_MAX+1 limit in accordance

with
the rules of modulo arithmetics, i.e. the original negative value

turns
into the positive value 'SHORT_MAX+1+x' . This value is once again
implicitly promoted to type 'int' (the value is preserved). And now

the

I don't see why after the negation the value would be "implicity
promoted to type 'int'." I cast x to an (unsigned short int) so why
would it be cast back to a signed int?


Speaking informally, C never performs any arithmetical computations on
types smaller than 'int'. Whenever you get a value of smaller type, that
value is immediately implicitly promoted to 'int' (or 'unsigned int', if
'int' is too narrow). This process is called 'integral promotion'. In
many cases it is purely conceptual, i.e. it doesn't really take place in
the final code, but it is still important to take it into account in
general case, because it can affect the final result.

It appears to be working (I hope I didn't miss anything), but if I

were
you I wouldn't jump through all those hoops and employ all that
wraparound trickery to get to the negation of the original value. I
would do it this way instead

/* already performed check to make sure x is negative */
abs_val = -x;

This just makes more sense than the original version.


The problem with that way is that you can have overflow if x =
SHRT_MIN because abs(SHRT_MIN) can be greater than SHRT_MAX.


Not really that simple. The original value of 'x', as I said above, is
promoted to 'int' before '-' is applied. And '-' is computed within the
bounds of type 'int', not within the bounds of type 'short'. However,
the overflow you describe is still possible if, for example, INT_MIN ==
SHORT_MIN and INT_MAX == SHOT_MAX. But that's more of a question of what
do you want to have as the final result in this case, not the problem
with this particular approach.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #7
Andrey Tarasevich wrote:
...
I want the absolute value of a 'short int' so to avoid the dangers of
overflow I am doing the follow:

short int x = -4; /* want abs(x) */
unsigned short int abs_val;

...

/* already performed check to make sure x is negative */
abs_val = -(unsigned short int)x;

I take it that the above is how I should safely get the absolute value
of a negative number.
Let's see what happens in this expression.


A couple of important corrections follow.
Initially, the operand ('x') is implicitly promoted to type 'int' (the
value is preserved). Then the conversion to 'unsigned short' causes the
original value to wrap around zero/SHORT_MAX+1 limit in accordance with
Should be "... wrap around zero/USHORT_MAX+1 limit ..."
the rules of modulo arithmetics, i.e. the original negative value turns
into the positive value 'SHORT_MAX+1+x' . This value is once again
USHORT_MAX+1+x
implicitly promoted to type 'int' (the value is preserved).
It is possible that at this stage the value is promoted to type
'unsigned short'. The choice depends on whether 'int' is large enough to
represent all 'unsigned short' values. If it is (USHORT_MAX <= INT_MAX),
'int' is chosen. Otherwise (USHORT_MAX > INT_MAX), 'unsigned int' is chosen.
And now the
unary '-' is applied to the operand, resulting in a negative value
'-SHORT_MAX-1-x' of 'int' type.
'-USHORT_MAX-1-x' of type 'int' if the previous promotion was to 'int'.

Otherwise, if the previous promotion was to 'unsigned int', the result
of unary '-' application is a positive value 'UINT_MAX-USHORT_MAX-x' of
type 'unsigned int'.
Now, this final value is squeezed into a
variable of type 'unsigned short'. This will cause another wraparound.
The final value is positive '-SHORT_MAX-1-x+SHORT_MAX+1'. It is indeed
equal to '-x'.


'-USHORT_MAX-1-x+USHORT_MAX+1' in case of 'int' promotion and the final
result is '-x'.

In case of 'unsigned int' promotion, the value of
'UINT_MAX-USHORT_MAX-x' will be wrapped around 'USHORT_MAX + 1' boundary
and it appears that it does not give the desired result in general case,
unless I'm missing something.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #8

Andrey Tarasevich wrote:
Andrey Tarasevich wrote:
...
I want the absolute value of a 'short int' so to avoid the dangers of overflow I am doing the follow:

short int x = -4; /* want abs(x) */
unsigned short int abs_val;

...

/* already performed check to make sure x is negative */
abs_val = -(unsigned short int)x;

I take it that the above is how I should safely get the absolute value of a negative number.
Let's see what happens in this expression.


A couple of important corrections follow.
Initially, the operand ('x') is implicitly promoted to type 'int' (the value is preserved). Then the conversion to 'unsigned short' causes the original value to wrap around zero/SHORT_MAX+1 limit in accordance with
Should be "... wrap around zero/USHORT_MAX+1 limit ..."
the rules of modulo arithmetics, i.e. the original negative value
turns into the positive value 'SHORT_MAX+1+x' . This value is once again


USHORT_MAX+1+x
implicitly promoted to type 'int' (the value is preserved).


It is possible that at this stage the value is promoted to type
'unsigned short'. The choice depends on whether 'int' is large enough

to represent all 'unsigned short' values. If it is (USHORT_MAX <= INT_MAX), 'int' is chosen. Otherwise (USHORT_MAX > INT_MAX), 'unsigned int' is chosen.
And now the
unary '-' is applied to the operand, resulting in a negative value
'-SHORT_MAX-1-x' of 'int' type.
'-USHORT_MAX-1-x' of type 'int' if the previous promotion was to

'int'.
Otherwise, if the previous promotion was to 'unsigned int', the result of unary '-' application is a positive value 'UINT_MAX-USHORT_MAX-x' of type 'unsigned int'.
Now, this final value is squeezed into a
variable of type 'unsigned short'. This will cause another wraparound. The final value is positive '-SHORT_MAX-1-x+SHORT_MAX+1'. It is indeed equal to '-x'.
'-USHORT_MAX-1-x+USHORT_MAX+1' in case of 'int' promotion and the

final result is '-x'.

In case of 'unsigned int' promotion, the value of
'UINT_MAX-USHORT_MAX-x' will be wrapped around 'USHORT_MAX + 1' boundary and it appears that it does not give the desired result in general case, unless I'm missing something.

--
Best regards,
Andrey Tarasevich


Thanks for reminding me about integer promotions. That kind of
complicates things for the short int case. My reasoning was based on
what I had read about calculating the absolute value of an "int" but as
you correctly pointed out that doesn't apply to short int.

So basically I have a bunch of typedef'd types like uint16, uint32,
int16, int32, etc. How can I make safe absolute value functions for
these types since their base types can be changed(so I don't know if
integer promotions will be applicable or not)?

I have seen plenty of threads on absolute value functions for "int" but
in light of the issues you brought up I am not clear as to how to
proceed.

Thanks.

Nov 14 '05 #9
joshc wrote:

Andrey Tarasevich wrote:
joshc wrote:
joshc wrote:

> /* already performed check to make sure x is negative */
> abs_val = -(unsigned short int)x;
>
> I take it that the above is how I should
> safely get the absolute value
> of a negative number. I want to
> confirm this because my Lint package
> says that there is a:
> "Loss of sign(assignment )(int to unsigned short)."

I just wanted to add that if I cast that
whole thing to an unsigned
short int then I don't get that message
but I'm still curious why Lint
warns me without the cast.
I don't exactly understand why the above
cast helps you to get rid of
the warning. But if you really want to use the cast,
it would probably


What I meant was if I do the following:

abs_val = (unsigned short int)(-(unsigned short int)x);

That seems to get rid of the warning.


I recomend that you don't use small arithmetic types:
float, unsigned short, short, signed char, unsigned char, and char
in any way other than as array element types or string characters,
unless you have a special reason.

Also for
I want the absolute value of a 'short int' so to avoid the dangers of
overflow I am doing the follow:

short int x = -4; /* want abs(x) */
unsigned short int abs_val;


It is not guaranteed that there is an integer type
which is capable of representing the magnitude SHORT_MIN.

--
pete
Nov 14 '05 #10

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

Similar topics

3
3909
by: Carlos Ribeiro | last post by:
I was checking the Prolog recipe in the Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303057 It's a clever implementation that explores some aspects of Python that I wasn't aware of. One of them is the unary plus operator, that calls the __pos__ method. It's something that can be highly useful in my own experiments with the use of classes as a tool for generic declarative descriptions of objects -- UI forms,...
2
1884
by: Dave Theese | last post by:
Am I correct in saying that it is not possible to overload unary + and unary -?
7
3121
by: John J | last post by:
To write a unary operator- operation that returns the negative of a value is it just as simple as subtracting the value from itself? ie. { return (value - value); } Thanks
5
2640
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object1, const MyTemplate <T> & object2); template <typename T> class MyTemplate
2
2372
by: Javier Estrada | last post by:
1. For types smaller than int, when I compile: class MyClass { static void Main(string args) { x = 10; y = -x; }
1
1137
by: Heloise | last post by:
Hi all, I'm trying to write an overloaded operator for unary negation e.g., MyClass a(5); MyClass b(0); b = -a; Here is my class implementation:
13
5097
by: Marc | last post by:
Hi, I've been lurking on clc for a few months now, and want to start by thanking the regulars here for opening my eyes to a whole new dimension of "knowing c". Considering I had never even touched the standards a year ago, though I graduated in embedded SW development... Anyway, to the problem at hand: I've stumbled upon the following construct at work recently, and cannot really make up my mind about the standard's take on the matter.
28
5084
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be the ++, the compiler says: "Error: invalid l-value in increment". int i = 10; ~!*&i++;
16
4719
by: JoseMariaSola | last post by:
How may operators and operands does (typename) expression has? I'd say one operator, the cast operator, and two operands: typename and expression. But everywhere I read, cast is categorized as an unary operator. Why is that? Is it just a syntax cotegory? Thanks.
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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
10045
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...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
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
7408
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
6673
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
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.