Click to See Complete Forum and Search --> : help me... // i don't understand...//


kings
07-01-2001, 09:00 AM
i'm using turbo c++3.0(dos version)

#include<iostream.h>
#include<conio.h>
void main()
{
int a=0xf0f0;
cout<<hex<<a;
}


i'm expecting the result "f0f0" but the result is "fffff0f0"
i don't understanding...// help me please...

Gerd
07-01-2001, 10:45 AM
"kings" <donggl278@hanmail.net> wrote:
>
>i'm using turbo c++3.0(dos version)
>
>#include<iostream.h>
>#include<conio.h>
>void main()
>{
> int a=0xf0f0;
> cout<<hex<<a;
>}
>
>
>i'm expecting the result "f0f0" but the result is "fffff0f0"
>i don't understanding...// help me please...


I am compiling it on VC++6.0 and it works just fine !
If you intend to become a programmer, I would suggest you get
a professional compiler and start using that, saving yourself
a lot of time and effort. There is a lot to learn. I have
seen two guys now make it from amateurs to being programmers.
People do it all the time. The right tools make all the difference.

You may have a hardware problem here. It could be just about
anything. (You could be just b'sssing us.)

Gerd

rt11guru
07-02-2001, 08:56 AM
"kings" <donggl278@hanmail.net> wrote:
>
>i'm using turbo c++3.0(dos version)
>
>#include<iostream.h>
>#include<conio.h>
>void main()
>{
> int a=0xf0f0;
> cout<<hex<<a;
>}
>
>
>i'm expecting the result "f0f0" but the result is "fffff0f0"
>i don't understanding...// help me please...

It looks to me like you compiler declares an int to be 16 bits, while your
cout is expecting output in hex format to be 32 bits. You getting a conversion
to 32 bits with the sign bit extended.
Like "Gerd" I'm using VC++ 6.0 and can't duplicate your result directly.
However, the code

short a = 0xf0f0;
long b = a;
cout<<hex<<b;

yields your result.

The reason it doesn't work directly on VC++, is that VC++ defines an int
to be 32 bits, so
int a = 0xf0f0;
is the same as
int a = 0x0000f0f0;

kings
07-02-2001, 11:26 AM
#include<iostream.h>
#include<conio.h>
void main()
{
int a=0xf0f0;
cout<<sizeof(a)<<endl; // if your compiler is vc++, the result is 4byte.
cout<<hex<<a; //so the result a is f0f0.
}

the result
2
fffff0f0
i had compiled this program with tc3.0(dos version)or tcwin3.1
the result is same...
although sizeof(a) is 2byte,the result is 4byte...
i know your meaning about sign expending... i want to know definite reason
i guess it is related with hardware(bus). i'll expecting your answer~!!^^*



"rt11guru" <rt11guru@yahoo.com> wrote:
>
>"kings" <donggl278@hanmail.net> wrote:
>>
>>i'm using turbo c++3.0(dos version)
>>
>>#include<iostream.h>
>>#include<conio.h>
>>void main()
>>{
>> int a=0xf0f0;
>> cout<<hex<<a;
>>}
>>
>>
>>i'm expecting the result "f0f0" but the result is "fffff0f0"
>>i don't understanding...// help me please...
>
>It looks to me like you compiler declares an int to be 16 bits, while your
>cout is expecting output in hex format to be 32 bits. You getting a conversion
>to 32 bits with the sign bit extended.
>Like "Gerd" I'm using VC++ 6.0 and can't duplicate your result directly.
>However, the code
>
> short a = 0xf0f0;
> long b = a;
> cout<<hex<<b;
>
>yields your result.
>
>The reason it doesn't work directly on VC++, is that VC++ defines an int
>to be 32 bits, so
>int a = 0xf0f0;
>is the same as
>int a = 0x0000f0f0;
>

jonnin
07-02-2001, 05:01 PM
>However, the code
>
> short a = 0xf0f0;
> long b = a;
> cout<<hex<<b;
>
>yields your result.

borland3 is a 16 bit compiler. Not sure how "hex" works, but if it works
on int, its going to want 16 bits on this platform. but maybe the long thing
will fix it; not sure if b3 was after 386 (first 32 bit ints) or not, or
even if before that they used emulation, I forget.

And borland 3 was professional, still is for dos users (dos is alive in embedded
world). (other post).

still, if you can't get it to work, revert to other ways.
char x[20];
sprintf(x, "%x",a); //I think. my memory is shot...
cout << x ;

Harris Shaikh
07-05-2001, 01:17 PM
This code has been run on VC++, and that is the most powerfull compiler. your
compiler probably does not compat with the c++ version you are using. You
can go to www.bloodshed.net to get a free up-to-date compiler

Best Regards,
Harris Shaikh


"kings" <donggl278@hanmail.net> wrote:
>
>i'm using turbo c++3.0(dos version)
>
>#include<iostream.h>
>#include<conio.h>
>void main()
>{
> int a=0xf0f0;
> cout<<hex<<a;
>}
>
>
>i'm expecting the result "f0f0" but the result is "fffff0f0"
>i don't understanding...// help me please...