Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Basic doubt in datatypes of signed and unsigned

Reply
Thread Tools

Basic doubt in datatypes of signed and unsigned

 
 
hari
Guest
Posts: n/a
 
      12-21-2008
Hi,

#include <stdio.h>
#include<conio.h>
int main()
{
int x= -1;
unsigned int y = x ;
unsigned int j = y;

printf("%x %x",x,y)
return 0;
}

When I execute this,I' m getting the output as 0xfffffff
0xfffffff.My expectation is , y is unsigned int,so on assigning a
signed variable with value -1.It should go to 0.

Please correct me if I' m wrong .

Regards
Hari
 
Reply With Quote
 
 
 
 
Tomás Ó hÉilidhe
Guest
Posts: n/a
 
      12-21-2008
On Dec 21, 3:09*pm, hari <(E-Mail Removed)> wrote:

> #include<conio.h>



Just so you know, conio.h is not a part of the C Standard. For this
particular program, you don't even need conio.h.


> int main()
> {
> * * * * * * * * int x= -1;
> * * * * * * * * unsigned int y = x ;



When you convert a signed integer to an unsigned integer, the value
you're left with is as follows:

(WHATEVER_UNSIGNED_INT_TYPE_MAX + 1) + (-the_negative_value)

So in your case, it's:

(UINT_MAX + 1) - (1)

And so that becomes:

UINT_MAX
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      12-21-2008
hari wrote:
> Hi,
>
> #include <stdio.h>
> #include<conio.h>
> int main()
> {
> int x= -1;
> unsigned int y = x ;
> unsigned int j = y;
>
> printf("%x %x",x,y)
> return 0;
> }
>
> When I execute this,I' m getting the output as 0xfffffff


Are you sure that wasn't 0xffffffff? A value of 0xfffffff is technically
possible, but extremely unlikely unless you're using a very unusual machine.

> 0xfffffff.My expectation is , y is unsigned int,so on assigning a
> signed variable with value -1.It should go to 0.
>
> Please correct me if I' m wrong .


If an integer value is outside the valid range for an unsigned integer
type, when you convert that value to that type, the result is determined
using modulus arithmetic. This means that you first determine the number
that is 1 larger than the largest value that can be stored in in the
unsigned type; in your case, that would be UINT_MAX+1, which is probably
0x100000000 (assuming that I was right about you mis-counting the number
of 'f's). Then you repeatedly either add or subtract this number from
the value, until you get a result that is in range for the unsigned
type. In this case, a single addition of UINT_MAX+1 to -1 gives you a
value of UINT_MAX, which is within range, so that's the result you
should be getting.
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      12-23-2008
James Kuyper <(E-Mail Removed)> writes:
> hari wrote:
>> Hi,
>>
>> #include <stdio.h>
>> #include<conio.h>


*ahem*

>> int main()
>> {
>> int x= -1;
>> unsigned int y = x ;
>> unsigned int j = y;
>>
>> printf("%x %x",x,y)
>> return 0;
>> }
>>
>> When I execute this,I' m getting the output as 0xfffffff

>
> Are you sure that wasn't 0xffffffff?


Odd that you should expect one character more, I would have expected
one character fewer.

Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-23-2008
Phil Carmody wrote:
> James Kuyper <(E-Mail Removed)> writes:
>> hari wrote:
>>> Hi,
>>>
>>> #include <stdio.h>
>>> #include<conio.h>

>
> *ahem*
>
>>> int main()
>>> {
>>> int x= -1;
>>> unsigned int y = x ;
>>> unsigned int j = y;
>>>
>>> printf("%x %x",x,y)
>>> return 0;
>>> }
>>>
>>> When I execute this,I' m getting the output as 0xfffffff

>> Are you sure that wasn't 0xffffffff?

>
> Odd that you should expect one character more, I would have expected
> one character fewer.


Yes, that leading 0 shouldn't appear in his output, and I shouldn't have
included it in my proposed alternative.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
Convert a signed binary number into a signed one ? Rob1bureau VHDL 1 02-27-2010 12:13 AM
signed and unsigned Sign bit doubt hari C Programming 4 12-23-2008 12:18 AM
signed(12 downto 0) to signed (8 downto 0) kyrpa83 VHDL 1 10-17-2007 06:58 PM
hex and unsigned and signed decimal me2 C Programming 14 01-02-2007 10:29 AM



Advertisments