473,881 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(unsigned)-INT_MIN

Hi all

int i= INT_MIN;
unsigned int u= -i;

Is u guaranteed to have the absolute value of INT_MIN?

Why it might not: -i has type (int), and -INT_MIN might be more than
INT_MAX.

A cast might not work either, because in (unsigned)-i; the cast is too
late, and in -(unsigned)i then i cannot be represented as unsigned.

If long is longer than int, I can do -(long)i, but what if we are talking
about the longest integer type available?

TIA
viza
Jul 21 '08 #1
30 4939
On Jul 21, 11:53 am, viza <tom.v...@gm-il.com.obviousc hange.invalid>
wrote:
Hi all

int i= INT_MIN;
unsigned int u= -i;

Is u guaranteed to have the absolute value of INT_MIN?

Why it might not: -i has type (int), and -INT_MIN might be more than
INT_MAX.
So what? corresponding integer types have the same range of values; If
int has N values, unsigned int has N values.
It is guaranteed.
<snip>
Jul 21 '08 #2
On Jul 21, 1:53*pm, viza <tom.v...@gm-il.com.obviousc hange.invalid>
wrote:
Hi all

int i= INT_MIN;
unsigned int u= -i;

Is u guaranteed to have the absolute value of INT_MIN?
On my system, u becomes 0 as INT_MIN is -2147483648 and INT_MAX is
2147483647. So, -INT_MIN wraps around to 0.
Why it might not: -i has type (int), and -INT_MIN might be more than
INT_MAX.
-INT_MIN is greater than INT_MAX on most of the systems.

If long is longer than int, I can do -(long)i, but what if we are talking
about the longest integer type available?
What are we looking at? Is it about getting the absolute value of a
signed int in an unsigned int?
Jul 21 '08 #3
On Jul 21, 12:21 pm, vipps...@gmail. com wrote:
On Jul 21, 11:53 am, viza <tom.v...@gm-il.com.obviousc hange.invalid>
wrote:Hi all
int i= INT_MIN;
unsigned int u= -i;
Is u guaranteed to have the absolute value of INT_MIN?
Why it might not: -i has type (int), and -INT_MIN might be more than
INT_MAX.

So what? corresponding integer types have the same range of values; If
int has N values, unsigned int has N values.
It is guaranteed.
<snip>

I'm sorry, I misunderstood you. You're interested in the case that
INT_MIN < -INT_MAX.
Well, in that case, computing -INT_MIN invokes undefined behavior;
(overflow)
Try this

if(i == INT_MIN)
u += INT_MAX + (unsigned)-(i + INT_MAX);
u = -i;
Jul 21 '08 #4
On Jul 21, 8:53 pm, viza <tom.v...@gm-il.com.obviousc hange.invalid>
wrote:
int i= INT_MIN;
unsigned int u= -i;

Is u guaranteed to have the absolute value of INT_MIN?
No. On a normal (2's complement) system, the
code causes undefined behaviour because the
operation "-i" causes an integer overflow.

Not sure what 'vipps' was on about..
A cast might not work either, because in (unsigned)-i; the cast is too
late, and in -(unsigned)i then i cannot be represented as unsigned.
Signed values can be converted to unsigned ;
(unsigned)i is guaranteed to be UINT_MAX - INT_MIN,
which is INT_MAX on the normal machine.
Jul 21 '08 #5
On Jul 21, 12:38 pm, Old Wolf <oldw...@inspir e.net.nzwrote:
On Jul 21, 8:53 pm, viza <tom.v...@gm-il.com.obviousc hange.invalid>
wrote:
int i= INT_MIN;
unsigned int u= -i;
Is u guaranteed to have the absolute value of INT_MIN?

No. On a normal (2's complement) system, the
code causes undefined behaviour because the
operation "-i" causes an integer overflow.

Not sure what 'vipps' was on about..
Which one of my two (without counting this one) messages are you
refering to?
I posted a follow-up quoting my first message saying I misunderstood,
and then I gave an answer which I believe to be correct. (and
relevant)
A cast might not work either, because in (unsigned)-i; the cast is too
late, and in -(unsigned)i then i cannot be represented as unsigned.

Signed values can be converted to unsigned ;
(unsigned)i is guaranteed to be UINT_MAX - INT_MIN,
which is INT_MAX on the normal machine.
Wrong.
Assuming i is -N with N >= 0, (unsigned)i is UINT_MAX - N + 1.
So (unsigned)INT_M IN would be UINT_MAX + INT_MIN + 1.
Jul 21 '08 #6
On Jul 21, 12:36 pm, vipps...@gmail. com wrote:
<snip>
Try this

if(i == INT_MIN)
u += INT_MAX + (unsigned)-(i + INT_MAX);
u = -i;
^
insert 'else' there... :-( I should be looking twice at my messages
before posting them.
Jul 21 '08 #7
Hi

On Mon, 21 Jul 2008 08:53:51 +0000, viza wrote:
int i= INT_MIN;
unsigned int u= -i;

Is u guaranteed to have the absolute value of INT_MIN?
If long is longer than int, I can do -(long)i, but what if we are
talking about the longest integer type available?
Thanks for the answers so far, but none of them help. Perhaps I can
explain it a bit better:

What I would like is a constant expression of unsigned type for the
absolute value of an constant negative expression of the corresponding
signed type.

Ie:

1) I know i is negative
2) I want (unsigned)abs(i )

The answer simply: ( (unsigned)-i ) for any i except INT_MIN. What is
the answer for INT_MIN? or for any i?

Thanks,

viza
Jul 21 '08 #8
Hi

On Mon, 21 Jul 2008 10:02:32 +0000, viza wrote:
What I would like is a constant expression of unsigned type for the
absolute value of an constant negative expression of the corresponding
signed type.
Thanks for the answers so far, but none of them help
I could use a conditional operation based on vippstars second post, but
is there a neater way?

viza
Jul 21 '08 #9
On Jul 21, 9:51 pm, vipps...@gmail. com wrote:
On Jul 21, 12:38 pm, Old Wolf <oldw...@inspir e.net.nzwrote:
Not sure what 'vipps' was on about..

Which one of my two (without counting this one) messages are you
refering to?
I was referring to your first post on this thread
(and I posted before you posted your correction).
Signed values can be converted to unsigned ;
(unsigned)i is guaranteed to be UINT_MAX - INT_MIN,
which is INT_MAX on the normal machine.

Wrong.
Assuming i is -N with N >= 0, (unsigned)i is UINT_MAX - N + 1.
So (unsigned)INT_M IN would be UINT_MAX + INT_MIN + 1.
Knew I should have checked that more before
posting .. the baby was crying though :)
Jul 21 '08 #10

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

Similar topics

96
4011
by: John Harrison | last post by:
I knew that unsigned integral data types were the cause of scads of mostly spurious warning messages, but I didn't realise that they were a security risk too (see here http://www.securitytracker.com/alerts/2004/Feb/1009067.html). All for one measly extra bit. So has the time come for C++ to deprecate unsigned integral types? john
7
3612
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For example, consider this four-bit number: 1100 from this number I want to extract two numbers: 1000 and 100 had the four-bit number been 0101 I would want to extract 100 and 1. How should I do this? I wish I had some code to post but I don't...
34
16711
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;
20
5378
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include <stdlib.h>
4
15364
by: techie | last post by:
I have defined a number of unsigned integer types as follows: typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; typedfe long long uint64; Is it necessary to explicitly cast from one type of unsigned integer type to another even though they do so implicitly?
10
3321
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add signed/unsigned modifier to class declarations to next revision of C++ programming language
20
3240
by: Junmin H. | last post by:
Hello, I am trying to print the range of unsigned char and unsigned int. The one for char is working good, gives me a correct output, however the other one for int doesnt, why?? Thanks #include <stdio.h> main(){ unsigned char c, temp; c = temp = 0; printf("the range of unsigned char is from %d to ", c);
6
6472
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but vaguely . Can
8
2343
by: Steven | last post by:
Hello, everyone! I find a version of strcpy(), I don't know why it return the unsigned char value. Can I change it into return *s1-*s2? int strcmp(const char *s1, const char *s2) { while (*s1 == *s2) {
29
10007
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
0
9776
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
11100
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
10718
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
10402
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
9555
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...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
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
3
3225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.