-
Re: please... read this ... it is strange...!!!
#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;
>
-
Re: please... read this ... it is strange...!!!
Hi,
It's really not that strange given the fact that you are most likely working
on a 32bit comp. Further, there should be no question as to what's happening
given the response that 'rt11guru' posted. As for a definite reason - the
sign bit is being extended through bits 16-31. You could also check this
by trying values like 0x00F0, 0x10F0, ... , 0x70F0 where the most significant
bit(15) is 0.
Chris
"kings" <donggl278@hanmail.net> wrote:
>
>#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;
>>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks