473,406 Members | 2,956 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,406 software developers and data experts.

cast unsigned long to long


unsigned long a = ...;
long b = (long)a;

while a overflows, what is the result of b?
Thank you.

Dec 27 '05 #1
10 17514
On 2005-12-27, jeff <xi*****@gmail.com> wrote:

unsigned long a = ...;
long b = (long)a;

while a overflows, what is the result of b?
Thank you.


undefined.
Dec 27 '05 #2
jeff said:

unsigned long a = ...;
long b = (long)a;

while a overflows, what is the result of b?


a can't overflow. If the value of a exceeds LONG_MAX, the value of b is
undefined.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 27 '05 #3
"jeff" <xi*****@gmail.com> writes:
unsigned long a = ...;
long b = (long)a;

while a overflows, what is the result of b?


The cast is unnecessary; the declaration
long b = a;
is equivalent, since there's an implicit conversion. (Most casts are
unnecessary.)

If a conversion to a signed integer type overflows, "either the result
is implementation-defined or an implementation-defined signal is
raised" (C99 6.3.1.3p3). I think the permission to raise a signal is
new in C99; in C90, you just get an implementation-defined result.
(No, it's not undefined.)

"Implementation-defined" means that your implementation is required to
document it, but you shouldn't depend on this since it's likely to
vary from one implementation to another, making your code
non-portable.

Note that this is different from what happens on artithmetic overflow,
which invokes undefined behavior for signed types.

--
Keith Thompson (The_Other_Keith) 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.
Dec 27 '05 #4
On 2005-12-27, Keith Thompson <ks***@mib.org> wrote:
"jeff" <xi*****@gmail.com> writes:
unsigned long a = ...;
long b = (long)a;

while a overflows, what is the result of b?
The cast is unnecessary; the declaration
long b = a;
is equivalent, since there's an implicit conversion. (Most casts are
unnecessary.)

If a conversion to a signed integer type overflows, "either the result
is implementation-defined or an implementation-defined signal is
raised" (C99 6.3.1.3p3). I think the permission to raise a signal is
new in C99; in C90, you just get an implementation-defined result.
(No, it's not undefined.)


That's not considered "integer overflow"?

An example of undefined behavior is the behavior on integer overflow.

my mistake, it's not

"Implementation-defined" means that your implementation is required to
document it, but you shouldn't depend on this since it's likely to
vary from one implementation to another, making your code
non-portable.

Note that this is different from what happens on artithmetic overflow,
which invokes undefined behavior for signed types.

Dec 27 '05 #5
On 27 Dec 2005 06:13:19 -0800, "jeff" <xi*****@gmail.com> wrote in
comp.lang.c:

unsigned long a = ...;
long b = (long)a;
The cast is completely unnecessary and gains you nothing at all. An
assignment between two different types where assignment is permitted
causes an implicit conversion of the source type to the destination
type. So when you assign 'a' to 'b', there is an implicit and
automatic conversion of the value of 'a' to signed long. Adding a
cast to specify an explicit conversion that is being performed
automatically only obfuscates your code.
while a overflows, what is the result of b?
'a' is an unsigned long, and unsigned types can't overflow. I suppose
the question you are really asking is, what happens if 'a' contains a
value larger than LONG_MAX.

In that case, either 'b' receives an implementation-defined value or
an implementation-defined signal is raised.
Thank you.


You're welcome.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 27 '05 #6
On Tue, 27 Dec 2005 14:21:12 +0000 (UTC), Jordan Abel
<jm****@purdue.edu> wrote in comp.lang.c:
On 2005-12-27, jeff <xi*****@gmail.com> wrote:

unsigned long a = ...;
long b = (long)a;

while a overflows, what is the result of b?
Thank you.


undefined.


That's not correct:

====
6.3 Conversions
6.3.1 Arithmetic operands
6.3.1.3 Signed and unsigned integers

1 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.

2 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.

3 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.
====

The "implementation-defined signal" option is new with C99. Under
C89/90, the result was always an implementation-defined value, and
never undefined behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 27 '05 #7
On Tue, 27 Dec 2005 14:38:56 +0000 (UTC), Richard Heathfield
<in*****@invalid.invalid> wrote in comp.lang.c:
jeff said:

unsigned long a = ...;
long b = (long)a;

while a overflows, what is the result of b?


a can't overflow. If the value of a exceeds LONG_MAX, the value of b is
undefined.


That's not correct:

====
6.3 Conversions
6.3.1 Arithmetic operands
6.3.1.3 Signed and unsigned integers

1 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.

2 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.

3 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.
====

The "implementation-defined signal" option is new with C99. Under
C89/90, the result was always an implementation-defined value, and
never undefined behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 27 '05 #8
Jack Klein said:
Under
C89/90, the result was always an implementation-defined value, and
never undefined behavior.


Jack, I sit corrected. Thank you.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 27 '05 #9
In article <ct********************************@4ax.com>,
Jack Klein <ja*******@spamcop.net> wrote:
'a' is an unsigned long, and unsigned types can't overflow. I suppose
the question you are really asking is, what happens if 'a' contains a
value larger than LONG_MAX.

In that case, either 'b' receives an implementation-defined value or
an implementation-defined signal is raised.


Do you know if it is implementation-defined which one will happen? So an
implementation has to specify that either there will _always_ be an
implementation defined value or _always_ an implementation-defined
signal?
Dec 27 '05 #10
On Tue, 27 Dec 2005 22:39:54 +0000, Christian Bau
<ch***********@cbau.freeserve.co.uk> wrote in comp.lang.c:
In article <ct********************************@4ax.com>,
Jack Klein <ja*******@spamcop.net> wrote:
'a' is an unsigned long, and unsigned types can't overflow. I suppose
the question you are really asking is, what happens if 'a' contains a
value larger than LONG_MAX.

In that case, either 'b' receives an implementation-defined value or
an implementation-defined signal is raised.


Do you know if it is implementation-defined which one will happen? So an
implementation has to specify that either there will _always_ be an
implementation defined value or _always_ an implementation-defined
signal?


No, I don't. The "implementation-defined signal" option did not exist
prior to C99. The one time I can remember the question being asked on
comp.std.c, no one who responded claimed to know of any such system.
One committee member wrote something about not constraining future
implementations.

As to whether the implementation is required to do the same thing for
all integer types, I am not so sure.

Consider an architecture with 32 bit registers that implemented
int64_t and uint64_t using its floating point hardware, which is not
particularly far-fetched since one could do this on anything in the
x86 family from the 486DX on up.

Such an implementation could specify the "expected"
implementation-defined behavior of bit truncation for the types up to
and including int32_t, but specify that it would throw a signal on
converting an out-of-range double to an int64_t. If the floating
point hardware generated an exception on out-of-range conversion, the
compiler run time would need to trap it and throw a C signal.

I have no idea whether such an implementation ever has or ever will
exist.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 27 '05 #11

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

Similar topics

31
by: Jamie Burns | last post by:
Hello, I am writing a client / server application. There is 1 server, and many clients. The server processes requests from each client, and typically creates and manipulates C++ objects on their...
36
by: MSG | last post by:
The answer is neither. Use macros. #define ALLOC(size, type) ((type) *) malloc((size) * sizeof(type)) #define NEW(type, name, size) (type) * (name) = ALLOC((size), (type)) They are both ...
12
by: Martin | last post by:
Two questions relating to FAQ answer 12.42. (1) In the statement s.i16 |= (unsigned)(getc(fp) << 8); i16 is declared int. The reason for casting to (unsigned) is explained as guarding...
12
by: Ralf | last post by:
If you want to explicitly state the type of an integer constant, you basically have two options. Either you use an explicit cast, as in: (unsigned int) 1234 or you use the UL suffixes, as in:...
9
by: Hamish M | last post by:
Hi I am interested in opinions on this topic. I have heard that a suffix is not a good solution and type casts are much better for example. ...
10
by: Allen | last post by:
I want to package an address to byte buffer. So I need to cast it to integer value. How to cast it? const char * ptr = 0x0013e328; int value = <cast(ptr); // use reinterpret_cast?
18
by: Felix Kater | last post by:
I haven't been thinking about it for years but recently I've stumbled on the fact that 'casting' is actually doing (at least) two different things: On the one hand 'casting' means: 'Change...
5
by: aaragon | last post by:
Hi everyone, I wrote a very simple function to try to understand the casting of variables in C++. The function is function foo() { std::vector<inttest(100); randomize(test); unsigned long...
8
by: ManicQin | last post by:
Hi, I've browsed the STL code a bit and stumble upon the next line (in the operator << overload of both long and short outputs - the line is for checking does the input is a manipulator) long...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...
0
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,...

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.