473,796 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unsigned to signed integer convesion

How the unsigned to signed integet conversion is done ?
For eg:

unsigned int ui = 100;
int si = ui;

What will be the value if "si" is printed ? How this conversion
is done ?

Thanx for any help in advance ....

Nov 15 '05 #1
19 2076
ju**********@ya hoo.co.in wrote:
How the unsigned to signed integet conversion is done ?
For eg:

unsigned int ui = 100;
int si = ui;

What will be the value if "si" is printed ? How this conversion
is done ?


si will be 100, the value of ui is converted to the type of si (signed
int) and the result is stored in si. If the value of ui is larger than
INT_MAX (which it isn't in this case), overflow will occur during this
conversion process resulting in undefined behavior.

Robert Gamble

Nov 15 '05 #2
ju**********@ya hoo.co.in writes:
How the unsigned to signed integet conversion is done ?
C99 6.3.1.3:

When a value with integer type is converted to another integer
type other than _Bool, if the value can be represented by the new
type, it is unchanged.

Otherwise, if the new type is unsigned, the value is converted by
repeatedly adding or subtracting one more than the maximum value
that can be represented in the new type until the value is in the
range of the new type. (Footnote: The rules describe arithmetic
on the mathematical value, not the value of a given type of
expression.)

Otherwise, the new type is signed and the value cannot be
represented in it; either the result is implementation-defined or
an implementation-defined signal is raised.
For eg:

unsigned int ui = 100;
int si = ui;

What will be the value if "si" is printed ? How this conversion
is done ?


Since both unsigned int and int are guaranteed to be able to represent
the value 100, the value of si will be 100.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #3
"Robert Gamble" <rg*******@gmai l.com> writes:
ju**********@ya hoo.co.in wrote:
How the unsigned to signed integet conversion is done ?
For eg:

unsigned int ui = 100;
int si = ui;

What will be the value if "si" is printed ? How this conversion
is done ?


si will be 100, the value of ui is converted to the type of si (signed
int) and the result is stored in si. If the value of ui is larger than
INT_MAX (which it isn't in this case), overflow will occur during this
conversion process resulting in undefined behavior.


No, the rules for conversions are different from the rules for
arithmetic operators.

When converting a signed or unsigned type to an unsigned type,
the result is well-defined (it wraps around).

When converting a signed or unsigned type to an unsigned type, if the
result can't be represented, either the result is
implementation-defined or an implementation-defined signal is raised.
No nasal demons are allowed.

See my other response in this thread for the standard's definition.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4


Keith Thompson wrote:
"Robert Gamble" <rg*******@gmai l.com> writes:
ju**********@ yahoo.co.in wrote:
How the unsigned to signed integet conversion is done ?
For eg:

unsigned int ui = 100;
int si = ui;

What will be the value if "si" is printed ? How this conversion
is done ?


si will be 100, the value of ui is converted to the type of si (signed
int) and the result is stored in si. If the value of ui is larger than
INT_MAX (which it isn't in this case), overflow will occur during this
conversion process resulting in undefined behavior.

No, the rules for conversions are different from the rules for
arithmetic operators.

When converting a signed or unsigned type to an unsigned type,
the result is well-defined (it wraps around).

When converting a signed or unsigned type to an unsigned type, if the
result can't be represented, either the result is
implementation-defined or an implementation-defined signal is raised.
No nasal demons are allowed.


The conversion doesn't yield undefined behavior, but
it yields something very nearly as good (if that's the right
word). The raising of the implementation-defined signal is
the moment when the behavior becomes if not undefined at least
unreliable. It's up to the implementation whether the signal
is treated as if with SIG_IGN or SIG_DFL; it's also up to the
implementation just what's involved in SIG_DFL for each signal.
If you install a handler, things *do* become undefined:

7.14.1.1/3
[...] If and when the function returns, if the value
of sig is [any] value corresponding to a computational
exception, the behavior is undefined; [...]

So the only way to handle the signal without invoking U.B. is
to terminate the program by calling abort() or _Exit() in the
handler. The signal is fatal unless it is SIG_IGN'ed (if the
implementation permits it) or SIG_DFL'ed with an implementation-
defined handling that itself isn't fatal.

So, yes: You're right, and the behavior is not undefined
unless a signal handler tries to return. But that's a bit
like the observation that falling from atop a forty-story
building won't hurt you -- the fall is harmless, but watch
out for that sudden stop at the bottom ...

--

Nov 15 '05 #5
<ju**********@y ahoo.co.in> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
How the unsigned to signed integet conversion is done ?
For eg:

unsigned int ui = 100;
int si = ui;

What will be the value if "si" is printed ? How this conversion
is done ?

Thanx for any help in advance ....


There's one more problem related to what's being discussed in "find out the
problem" by me and others...
It's all about the conversion.
Shamefully, but I had to fix a few bugs in my recent code... The code was
something like this:

typedef uint unsigned int;

void drawlineh (int x, int y, uint len, uint color)
{
if (x+len < 0)
return;
// the rest of code that's properly working...
}

The problem is that x gets converted to unsigned int in the expression
"x+len" and hence for negative x I wouldn't have the desired return from the
function.
This is really an annoying feature of C... The only way to make it work w/o
changing the API (i.e. making len of type int too) is to do a cast:

if (x+(int)len < 0)
return;

That makes me love asm more and more. Asm is the simplest programming
language ever -- I always know what the code does and I know that for all
bugs and problems it's me to blame. :) But C is such a wonderful creature,
no surprise so many people get it all wrong. They have always been getting
it wrong and they continue to do so. At times it's so counter intuitive for
an HLL. And it seems like most (or all) the books on C that I had in the
past never really described all this conversion well. I don't know if recent
do (now's the time to get it right finally!), but these days I always check
either in the standard or in the code if there's a doubt about something.
Thankfully, I'm not writing bad code for I know many things outside the C
scope... I don't wonder now that I should declare a variable before using it
(Hello Basic, matlab, etc users!:), I don't wonder now about the range and
precision of fixed and floating point things, etc etc.

If I were to change C, I'd change the conversions/promotions to be more
math-like, not C-like. But that's not going to happen anytime soon. Sigh.

Alex
Nov 15 '05 #6
Alexei A. Frounze wrote:
if (x+len < 0)
return;

The problem is that x gets converted to unsigned int in the expression
"x+len" and hence for negative x I wouldn't have the desired return from the
function.
This is really an annoying feature of C... The only way to make it work w/o
changing the API (i.e. making len of type int too) is to do a cast:

if (x+(int)len < 0)
return;
What if len > INT_MAX ?
That makes me love asm more and more. Asm is the simplest programming
language ever -- I always know what the code does and I know that for all
bugs and problems it's me to blame. :)


So how would you compare -2000000000 and +3000000000 in ASM,
without something at least as complicated as you would do in C ?

Nov 15 '05 #7
"Old Wolf" <ol*****@inspir e.net.nz> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Alexei A. Frounze wrote:
if (x+len < 0)
return;

The problem is that x gets converted to unsigned int in the expression
"x+len" and hence for negative x I wouldn't have the desired return from the function.
This is really an annoying feature of C... The only way to make it work w/o changing the API (i.e. making len of type int too) is to do a cast:

if (x+(int)len < 0)
return;


What if len > INT_MAX ?


Right. And using long would help neither on msvc++ nor on gcc on x86.
int=long there. Only long long would do, but I guess it would be _int64 or
something in terms of m$.
That makes me love asm more and more. Asm is the simplest programming
language ever -- I always know what the code does and I know that for all bugs and problems it's me to blame. :)


So how would you compare -2000000000 and +3000000000 in ASM,
without something at least as complicated as you would do in C ?


At least I would know about it for sure, w/o special books and standards :)

Am I the only who feels uncomfortable with certain aspects of C? Or is there
anyone who understands a bit of my concerns?

Alex
Nov 15 '05 #8
On Tue, 9 Aug 2005 00:44:30 +0400, Alexei A. Frounze
<al*****@chat.r u> wrote:
Shamefully, but I had to fix a few bugs in my recent code... The code was
something like this:

typedef uint unsigned int;

void drawlineh (int x, int y, uint len, uint color)
{
if (x+len < 0)
return;
// the rest of code that's properly working...
}

The problem is that x gets converted to unsigned int in the expression
"x+len" and hence for negative x I wouldn't have the desired return from the
function.
Several popular compilers will warn you about that if you enable
warnings, because the condition can never be true so the statements
after it are unreachable.
This is really an annoying feature of C... The only way to make it work w/o
changing the API (i.e. making len of type int too) is to do a cast:

if (x+(int)len < 0)
return;
If that's what you mean then do it -- but if len is larger than MAXINT
it will have undefined effect so you need to check for that first.
That makes me love asm more and more. Asm is the simplest programming
language ever -- I always know what the code does and I know that for all
bugs and problems it's me to blame. :)
And is totally non-portable. Even between assemblers for the same
platform.
But C is such a wonderful creature,
no surprise so many people get it all wrong. They have always been getting
it wrong and they continue to do so. At times it's so counter intuitive for
an HLL.
But different people find different parts of it counter-intuitive,
that's the problem, and same is true for all other programming
languages. Including asssembler.

NOT R1
BIC R1, R0

instead of

AND R1, R0

And many other oddities which only make sense if you know a particular
processor in detail.
And it seems like most (or all) the books on C that I had in the
past never really described all this conversion well. I don't know if recent
do (now's the time to get it right finally!), but these days I always check
either in the standard or in the code if there's a doubt about something.
Yup. At least the C standard is clearer than the C++ standard...
Thankfully, I'm not writing bad code for I know many things outside the C
scope... I don't wonder now that I should declare a variable before using it
(Hello Basic, matlab, etc users!:), I don't wonder now about the range and
precision of fixed and floating point things, etc etc.

If I were to change C, I'd change the conversions/promotions to be more
math-like, not C-like. But that's not going to happen anytime soon. Sigh.


And would confuse many more. C was written for systems programmers,
largely, and so was designed tro do what you told it and nothing more.
So if you want to do some conversions other than the default, you do
them. C is not designed for mathematics (try FORTRAN -- "FORmula
TRANslation"), so you get 1/2 == 0 because that is what is expected in
integer arithmetic even though it isn't in mathematics, and if you then
assign the result to a floating point variable it sets it to zero.

It's certainly not perfect, at least one of the original authors of the
language has admitted that the precedence of some of the operators was
wrong, but there is by now no chance of changing it because there is too
much code which depends on it. Just as with natural language, we can't
just change English spelling to something more lojikal at a wim, evn tho
sum pepl hav trid to do that. Except it's worse with computer languages
because you can probably understand what I wrote where a compiler
can't...

(And yes, I know that the Cyrillic alphabet did have several letters
removed as an act of the government, but that can only be done when the
government can say that all books in the previous script have to be
replaced and be obeyed...)

Chris C
Nov 15 '05 #9
"Alexei A. Frounze" <al*****@chat.r u> writes:
"Old Wolf" <ol*****@inspir e.net.nz> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. . [...]
So how would you compare -2000000000 and +3000000000 in ASM,
without something at least as complicated as you would do in C ?


At least I would know about it for sure, w/o special books and standards :)


There are no special books for assembly language?
Am I the only who feels uncomfortable with certain aspects of C? Or is there
anyone who understands a bit of my concerns?


I'd be suspicious of anyone who *wasn't* uncomfortable with some
aspects of C. K&R themselves (or at least K and/or R) have expressed
misgivings about some of the design choices.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10

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

Similar topics

19
6482
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real answer here? If a is converted to unsigned, then b>a. But, if b is converted to signed,then a>b. What's the correct coversion (what is the compiler supposed to do?)
96
4004
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
8
2247
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions. The reference should be 3.9.1 (Fundamental types), and 4.7 (Integral conversions). It seems to me that the Standard doesn't specify: 1) The "value representation" of any of these types, except that (3.9.1/3) "... The range of nonnegative...
10
11161
by: Peter Piper | last post by:
A Quick question if someone could help, im sure this has been asked before, but googling is getting me now where slowly. Is it possible to get an unsigned 32 bit integer field in Access. Im storing IP address in their integer form (ie. 32 bits) and Long Integer because it is signed can not represent the full range needed (0..4294967295), neither can Double. Any suggestions please?
16
5144
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. And K&R2 mentions "signed extension" everywhere. Reading some old clc posts, I've beginning to realize that these books are over-generalizing the topic. I am just wondering what the difference between the following pairs of terms are: 1)...
9
3953
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
4
15357
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?
7
5049
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are involved. The problem is that comparisons between signed and unsigned values are machine- dependent, because they depend on the sizes of the various integer types. For example, suppose that int is 16 bits
6
6459
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
0
9680
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
9528
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,...
1
10173
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
9052
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
5441
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
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
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
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
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.