Hi,
What will happen in following scenario..
long a = -2;
longlong b = a
long c;
c = *(ulonglong *)&b;
b = c;
also when
long d;
d = *&b;
b = d;
What will be the value of b ? When will it lose the upper 32bits ?
regards
amit
Nov 14 '05
25 2687
On Wed, 07 Apr 2004 01:54:43 GMT, Leor Zolman <le**@bdsoft.co m> wrote
in comp.lang.c: On Tue, 06 Apr 2004 20:45:25 -0500, Jack Klein <ja*******@spam cop.net> wrote:
You will need to try it on your compiler to find out, or consult the compiler's documentation. You are assigning a value to a signed integer type from a wider type when the value of the wider type is outside the range of the destination signed type. The C standard does not specify the result, only that it is implementation-defined. That means the compiler must document what it does.
And there is no guarantee that long long has 32 more bits than long, of course.
Of course (not that it even occurred to me, because even with my new locker and badge I still don't always think in terms of "the general case" when faced with questions like this) this is all accurate in terms of the Standard.
I began with the assumption the OP's long long was really longer than his long (watch it, Richard!) and that he was interested in issues such as the ones I tried to highlight in my test program. I'm kind of hoping he finds /both/ analyses to be useful... -leor
Your test program was actually an excellent idea. One way to
determine implementation-defined results with a particular compiler is
to test it. And since the OP's question involved only
implementation-defined, and not undefined, behavior, a test is quite
practical.
The only thing the OP needs to remember is that he should compile and
run your test program on his implementation, where the results he gets
might or might not be the same as those you got.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
amit wrote: What will happen in following scenario.. long a = -2; longlong b = a
The compiler complains about a syntax error. long c; c = *(ulonglong *)&b;
Another syntax error.
b = c; also when long d; d = *&b; b = d; What will be the value of b ? When will it lose the upper 32bits ?
Since things don't compile, the remainder of the question is
meaningless. You should consider the use and meaning of the blank
character.
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
amit wrote: What will happen in following scenario.. long a = -2; longlong b = a
The compiler complains about a syntax error. long c; c = *(ulonglong *)&b;
Another syntax error.
b = c; also when long d; d = *&b; b = d; What will be the value of b ? When will it lose the upper 32bits ?
Since things don't compile, the remainder of the question is
meaningless. You should consider the use and meaning of the blank
character.
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
You get the same results if you use lcc-win32.
lcc-win32 supports long long since quite a while
now.
You get the same results if you use lcc-win32.
lcc-win32 supports long long since quite a while
now.
On Wed, 7 Apr 2004 08:55:10 +0200, "jacob navia" <ja***@jacob.re mcomp.fr>
wrote: You get the same results if you use lcc-win32. lcc-win32 supports long long since quite a while now.
Thanks, Jacob, I didn't even know about your compiler. I've been
"collecting " C++ compilers (for STLFilt development work), but for C I've
just been relying on the ones (such as Comeau) that come "free" with C++
and have done the best job of diagnosing things. I'll get lcc-win32 and
play with it.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
On Wed, 7 Apr 2004 08:55:10 +0200, "jacob navia" <ja***@jacob.re mcomp.fr>
wrote: You get the same results if you use lcc-win32. lcc-win32 supports long long since quite a while now.
Thanks, Jacob, I didn't even know about your compiler. I've been
"collecting " C++ compilers (for STLFilt development work), but for C I've
just been relying on the ones (such as Comeau) that come "free" with C++
and have done the best job of diagnosing things. I'll get lcc-win32 and
play with it.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
On Wed, 07 Apr 2004 01:29:19 GMT, Leor Zolman <le**@bdsoft.co m> wrote: On Wed, 07 Apr 2004 01:13:59 GMT, Leor Zolman <le**@bdsoft.co m> wrote:
Well, that was my very first time attempting to display values of long longs, and in my confusion I ended up with a spurious "d" in four format conversions (I put %uld when %ul was sufficient for a plain unsigned long).
Fortunately, that didn't really break the output, except that there's a trailing, meaningless 'd' at the end of 4 of the lines. Sorry about that. -leor
P.S. It took me a while to find a platform that displayed long long values at all! Good thing I had that Dinkum lib lying around... ;-)
You're all way too polite (heh), I can't believe no one has posted yet to
give me hell. To set the record straight, I do in fact realize that those
conversions I originally wrote as "%uld", then corrected to "%ul", should
actually be "%lu". The consistency between long and long long conversion
syntax is way too sensible for me to have thought of trying it first, and
then bad eyesight must have made me think the ells before the d's were
ones...
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
On Wed, 07 Apr 2004 01:29:19 GMT, Leor Zolman <le**@bdsoft.co m> wrote: On Wed, 07 Apr 2004 01:13:59 GMT, Leor Zolman <le**@bdsoft.co m> wrote:
Well, that was my very first time attempting to display values of long longs, and in my confusion I ended up with a spurious "d" in four format conversions (I put %uld when %ul was sufficient for a plain unsigned long).
Fortunately, that didn't really break the output, except that there's a trailing, meaningless 'd' at the end of 4 of the lines. Sorry about that. -leor
P.S. It took me a while to find a platform that displayed long long values at all! Good thing I had that Dinkum lib lying around... ;-)
You're all way too polite (heh), I can't believe no one has posted yet to
give me hell. To set the record straight, I do in fact realize that those
conversions I originally wrote as "%uld", then corrected to "%ul", should
actually be "%lu". The consistency between long and long long conversion
syntax is way too sensible for me to have thought of trying it first, and
then bad eyesight must have made me think the ells before the d's were
ones...
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
In <r2************ *************** *****@4ax.com> Leor Zolman <le**@bdsoft.co m> writes: On Wed, 07 Apr 2004 01:13:59 GMT, Leor Zolman <le**@bdsoft.co m> wrote:
Well, that was my very first time attempting to display values of long longs, and in my confusion I ended up with a spurious "d" in four format conversions (I put %uld when %ul was sufficient for a plain unsigned long).
Bzzzt, still wrong. The *correct* conversion descriptor for unsigned long
is, and has always been, %lu. %ul expects an unsigned int and appends an
'l' to its value. If it receives an unsigned long, undefined behaviour.
Fortunately, that didn't really break the output, except that there's a trailing, meaningless 'd' at the end of 4 of the lines. Sorry about that.
It broke everything, due to invoking undefined behaviour, so any output
you got was by accident, rather than by design.
P.S. It took me a while to find a platform that displayed long long values at all! Good thing I had that Dinkum lib lying around... ;-)
It's been a common Unix extension for ages...
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Siemel Naran |
last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int,
char to unsigned char, signed char to unsigned char, and so on for all the
fundamental integer types? Something like
template <>
struct to_unsigned<signed int> : public std::unary_function<signed int,
unsigned int>
{
unsigned int operator()(signed int x) const { return x; }
};
|
by: amit |
last post by:
Hi,
What will happen in following scenario..
long a = -2;
longlong b = a
long c;
c = *(ulonglong *)&b;
b = c;
also when
|
by: tinesan |
last post by:
Hello fellow C programmers,
I'm just learning to program with C, and I'm wondering what the
difference between signed and unsigned char is. To me there seems to
be no difference, and the standard doesn't even care what a normal char
is (because signed and unsigned have equal behavior).
For example if someone does this:
unsigned char a = -2; /* or = 254 */
|
by: bruce.james.lee |
last post by:
hi
i have a problem with integer subtraction in C.
printf("%d", c < (a - b));
a is got from a #define and is 0x80000000 and b is got from input and
is also 0x80000000.
c is ffffffff (-1).
Now, this should print 1 (true) but it prints 0!
|
by: felixnielsen |
last post by:
What i really wanna do, is defining my own types, it doesnt really
matter why.
Anyway, i have run into some problems
1)
typedef unsigned short U16;
U16 test = 0xffffffff; // There should be a limit on this, max value
you should be able to asign should be 0xffff //
std::cout << test; // result = 0xffff //
| |
by: Smurff |
last post by:
Hi,
This code works fine on win and linux but not on hpux. All is compiled
with gcc. Can anyone help please?
/*****************************************************************************/
/*
*/
/* encrypt
*/
|
by: Charles Sullivan |
last post by:
I have a program written in C under Linux (gcc) which a user has
ported to run under AT&T SysV R4.
He sent me a copy of his makelog which displays a large number
of compiler warnings similar to this:
warning: semantics of ">>" change in ANSI C; use explicit cast
The statement to which this applies is:
xuc = ((uc & 0xF0 ) >4);
|
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
|
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
|
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...
|
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...
| |
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |