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 25 2685
On 6 Apr 2004 17:44:38 -0700, an******@yahoo. com (amit) wrote: 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
Let me know if I've misinterpreted anything you wrote up there:
#include <stdio.h>
int main()
{
long a = -2;
printf("a = %ld\n", a);
printf("a (unsigned) = %uld\n\n", a);
long long b = a;
printf("b = %lld\n", b);
printf("b (unsigned) = %llu\n\n", b);
long c;
c = *(unsigned long long *)&b;
printf("c (via pointers) = %ld\n", c);
printf("c (via pointers, unsigned) = %uld\n\n", c);
/* I've added this (why bother with the pointers? */
c = (unsigned long long) b;
printf("c (no pointers) = %ld\n", c);
printf("c (no pointers, unsigned) = %uld\n\n", c);
b = c;
long d;
d = *&b;
printf("d = %ld\n", d);
printf("d (unsigned) = %uld\n\n", d);
b = d;
printf("b (from d) = %lld\n", b);
printf("b (from d) (unsigned) = %llu\n\n", b);
b = (unsigned) d;
printf("b (from unsigned d) = %lld\n", b);
printf("b (from unsigned d) (unsigned) = %llu\n\n", b);
return 0;
}
Output:
a = -2
a (unsigned) = 4294967294ld
b = -2
b (unsigned) = 184467440737095 51614
c (via pointers) = -2
c (via pointers, unsigned) = 4294967294ld
c (no pointers) = -2
c (no pointers, unsigned) = 4294967294ld
d = -2
d (unsigned) = 4294967294ld
b (from d) = -2
b (from d) (unsigned) = 184467440737095 51614
b (from unsigned d) = 4294967294
b (from unsigned d) (unsigned) = 4294967294
So note that you do lose finally lose the upper 32 bits when assigning from
d to c, /if/ you first cast d's value to unsigned. So long as you're doing
the assignment as a signed value, you'll get sign extension.
(Compiled with Comeau 4.3.3, Dinkum Unabridged Library)
-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 6 Apr 2004 17:44:38 -0700, an******@yahoo. com (amit) wrote: 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
Let me know if I've misinterpreted anything you wrote up there:
#include <stdio.h>
int main()
{
long a = -2;
printf("a = %ld\n", a);
printf("a (unsigned) = %uld\n\n", a);
long long b = a;
printf("b = %lld\n", b);
printf("b (unsigned) = %llu\n\n", b);
long c;
c = *(unsigned long long *)&b;
printf("c (via pointers) = %ld\n", c);
printf("c (via pointers, unsigned) = %uld\n\n", c);
/* I've added this (why bother with the pointers? */
c = (unsigned long long) b;
printf("c (no pointers) = %ld\n", c);
printf("c (no pointers, unsigned) = %uld\n\n", c);
b = c;
long d;
d = *&b;
printf("d = %ld\n", d);
printf("d (unsigned) = %uld\n\n", d);
b = d;
printf("b (from d) = %lld\n", b);
printf("b (from d) (unsigned) = %llu\n\n", b);
b = (unsigned) d;
printf("b (from unsigned d) = %lld\n", b);
printf("b (from unsigned d) (unsigned) = %llu\n\n", b);
return 0;
}
Output:
a = -2
a (unsigned) = 4294967294ld
b = -2
b (unsigned) = 184467440737095 51614
c (via pointers) = -2
c (via pointers, unsigned) = 4294967294ld
c (no pointers) = -2
c (no pointers, unsigned) = 4294967294ld
d = -2
d (unsigned) = 4294967294ld
b (from d) = -2
b (from d) (unsigned) = 184467440737095 51614
b (from unsigned d) = 4294967294
b (from unsigned d) (unsigned) = 4294967294
So note that you do lose finally lose the upper 32 bits when assigning from
d to c, /if/ you first cast d's value to unsigned. So long as you're doing
the assignment as a signed value, you'll get sign extension.
(Compiled with Comeau 4.3.3, Dinkum Unabridged Library)
-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: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... ;-)
--
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: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... ;-)
--
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 6 Apr 2004 17:44:38 -0700, an******@yahoo. com (amit) wrote in
comp.lang.c: Hi,
What will happen in following scenario.. long a = -2; longlong b = a
long c; c = *(ulonglong *)&b;
The result of the assignment is implementation-defined, or, under C99,
for some strange reason, there is the alternative possibility that an
implementation-defined signal will be raised.
b = c; also when long d; d = *&b;
Again, the result is implementation-defined.
b = d; What will be the value of b ? When will it lose the upper 32bits ?
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.
regards amit
--
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
On 6 Apr 2004 17:44:38 -0700, an******@yahoo. com (amit) wrote in
comp.lang.c: Hi,
What will happen in following scenario.. long a = -2; longlong b = a
long c; c = *(ulonglong *)&b;
The result of the assignment is implementation-defined, or, under C99,
for some strange reason, there is the alternative possibility that an
implementation-defined signal will be raised.
b = c; also when long d; d = *&b;
Again, the result is implementation-defined.
b = d; What will be the value of b ? When will it lose the upper 32bits ?
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.
regards amit
--
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
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 regards amit
--
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 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 regards amit
--
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: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 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: 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,...
|
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...
| |
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,...
|
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: 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...
| |