freelanceprogrammers.org Forum Index » C

Doubt in Union & signed and unsigned Integer ...


View user's profile Post To page top
contactrrajesh@... Posted: Tue Jan 04, 2005 10:46 am


Joined: 04 Jan 2005

Posts: 2
Doubt in Union & signed and unsigned Integer ...
Hi  ,
 
         I am using Turbo C Compiler and code is ,
 
              union Abc
                {
                        signed int a;
                        unsigned int b;
                };
 
        Union wont allocate memory for all its Member variables. Union allocate memory only to variable whose size is larger than other. 
 
What is the bit level difference between signed int and unsigned int?
 
In the above code, Union allocates memory to  signed int a or unsigned int b , explain ?
 
 
With Kind Regards,
R.Rajesh.
Reply with quote
Send private message
View user's profile Post To page top
maniksinghal Posted: Tue Jan 04, 2005 1:35 pm


Joined: 02 Jan 2005

Posts: 9
Doubt in Union & signed and unsigned Integer ...
--- "R.Rajesh" <contactrrajesh@...> wrote:

> Hi ,
>
> I am using Turbo C Compiler and code is ,
>
> union Abc
> {
> signed int a;
> unsigned int b;
> };
>
> Union wont allocate memory for all its
> Member variables. Union allocate memory only to
> variable whose size is larger than other.
>
> What is the bit level difference between signed int
> and unsigned int?
>
> In the above code, Union allocates memory to signed
> int a or unsigned int b , explain ?
>
>
> With Kind Regards,
> R.Rajesh.
>


Hi rajesh,
The difference between signed and unsigned is only of
the MSB to be used as a sign bit or contribute to the
range of the number.
so in case of union the same size is there for both
signed and unsigned. only the interpretation of value
is different.

With regards
Manik



__________________________________
Do you Yahoo!?
The all-new My Yahoo! - Get yours free!
http://my.yahoo.com
Reply with quote
Send private message
View user's profile Post To page top
shabble Posted: Tue Jan 04, 2005 3:30 pm


Joined: 29 Dec 2004

Posts: 31
Doubt in Union & signed and unsigned Integer ...
From: R.Rajesh [mailto:contactrrajesh@...]

>Hi ,
>
> I am using Turbo C Compiler and code is ,
>
> union Abc
> {
> signed int a;
> unsigned int b;
> };
>
> Union wont allocate memory for all its Member
>variables. Union allocate memory only to variable whose size
>is larger than other.

In your particular example, both the members are the same size.

>What is the bit level difference between signed int and unsigned int?

None - it is how the bit pattern is interpreted that is different.

>In the above code, Union allocates memory to signed int a or
>unsigned int b , explain ?

The largest of the members, but as pointed out both members are the same
size.

--
PJH

"...one of the main causes of the fall of the Roman Empire was that,
lacking zero, they had no way to indicate successful termination of
their C programs." - Robert Firth



Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272

Website : www.alderley.com Sales : sales@... Service :
service@...

This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify it@...

This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
Reply with quote
Send private message
View user's profile Post To page top
taxikaps Posted: Tue Jan 04, 2005 5:32 pm


Joined: 03 Jan 2005

Posts: 10
Doubt in Union & signed and unsigned Integer ...
> union Abc
> {
> signed int a;
> unsigned int b;
> };
>
> Union wont allocate memory for all its Member variables. Union
> allocate memory only to variable whose size is larger than other.

Thats correct

> What is the bit level difference between signed int and unsigned int?

There is no difference as far as storage is concerned.

> In the above code, Union allocates memory to signed int a or unsigned
> int b , explain ?

As explained above.


There are only three fundamental data types in C AFAIK - char, int and
float. Using sign directive is therefore a tool for programmer not the
compiler.

Kapil
Reply with quote
Send private message
View user's profile Post To page top
vaddireddy1226 Posted: Tue Jan 04, 2005 8:46 pm


Joined: 04 Jan 2005

Posts: 1
Doubt in Union & signed and unsigned Integer ...
HI Rajesh
                   Union allocates memory that much any individual member takes at most . In this condition both signed and unsigned int takes 2 bytes , signed and unsigned integers differ in range . signed integer can store -2^15 to  (2^15 - 1). Unsigned Integer can store 0 to ( 2 ^ 16 - 1) . Though we assign negative numbers to unsigned integers the compiler consider them as positive .    In this case union Abc is allocated with 2 bytes .
   
                                                                                   Loknath Reddy
"R.Rajesh" <contactrrajesh@...> wrote:

Hi  ,
 
         I am using Turbo C Compiler and code is ,
 
              union Abc
                {
                        signed int a;
                        unsigned int b;
                };
 
        Union wont allocate memory for all its Member variables. Union allocate memory only to variable whose size is larger than other. 
 
What is the bit level difference between signed int and unsigned int?
 
In the above code, Union allocates memory to  signed int a or unsigned int b , explain ?
 
 
With Kind Regards,
R.Rajesh.To unsubscribe : programmers-town-unsubscribe@yahoogroups.com
Do you Yahoo!? Yahoo! Mail - Easier than ever with enhanced search. Learn more.
Reply with quote
Send private message
View user's profile Post To page top
sflam108 Posted: Tue Jan 04, 2005 10:44 pm


Joined: 25 Jul 2003

Posts: 48
Doubt in Union & signed and unsigned Integer ...
Reply embedded...

> From: R.Rajesh [mailto:contactrrajesh@...]
> Sent: Monday, January 03, 2005 10:46 PM
> To: Programmer Group
> Subject: (PT) Doubt in Union & signed and unsigned Integer ...
>
> Hi  ,

>         I am using Turbo C Compiler and code is ,

>              union Abc
>                {
>                        signed int a;
>                        unsigned int b;
>                };

>        Union wont allocate memory for all its Member variables.
> Union allocate memory only to variable whose size is larger than other. 

"Allocate" implies dynamic allocation.

ITYM: The size of a union type is the largest size of its member.


> What is the bit level difference between signed int and unsigned int?

http://dev.unicals.com/c99-draft.html#6.2.5

6.2.5p6, p9.

http://dev.unicals.com/c99-draft.html#6.2.6

6.2.6.1p6, p7.
6.2.6.2

Signed bit is not necessary become one of the value bit in the unsigned
type. So the interpretation is system specified.


> In the above code, Union allocates memory to  signed int a or unsigned
> int b , explain ?

Again, union does not and will not "allocate" anything.

6.2.5p6 and 6.2.6.2 should answered this.

Shyan
Reply with quote
Send private message
View user's profile Post To page top
shabble Posted: Wed Jan 05, 2005 5:28 pm


Joined: 29 Dec 2004

Posts: 31
Doubt in Union & signed and unsigned Integer ...
From: lokanath reddy [mailto:vaddireddy1226@...]
>"R.Rajesh" <contactrrajesh@...> wrote:
>>
>> Hi ,
>>
>> I am using Turbo C Compiler and code is ,
>>
>> union Abc
>> {
>> signed int a;
>> unsigned int b;
>> };
>>
>> Union wont allocate memory for all its Member
>>variables. Union allocate memory only to variable whose size
>>is larger than other.
>>
>> What is the bit level difference between signed int and
>>unsigned int?
>>
>> In the above code, Union allocates memory to signed
>>int a or unsigned int b , explain ?
>
> Union allocates memory that much any
>individual member takes at most . In this condition both
>signed and unsigned int takes 2 bytes , signed and unsigned
>integers differ in range . signed integer can store -2^15

ITYM -(2^15-1)

>to (2^15 - 1). Unsigned Integer can store 0 to ( 2 ^ 16 - 1) .

Note that implementations are permitted to have larger magnitudes, but
not smaller, than those you give. The actual numbers for a given
implementation are given (respectively) as INT_MIN, INT_MAX and
UINT_MAX, and are defined in <limits.h>

>Though we assign negative numbers to unsigned integers the
>compiler consider them as positive . In this case union Abc
>is allocated with 2 bytes .

... assuming sizeof(int)==2...

--
PJH
The simple man`s approach to exception handling is to put a try-catch
around the whole application and just restart the application on error.
That`s what Microsoft does anyway.
- Thomas Hruska sums up MS in C-Prog



Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12
8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272

Website : www.alderley.com Sales : sales@... Service :
service@...

This email and its contents are confidential and are solely for the use of the
intended recipient. If you are not the original recipient you have received it
in error and any use, dissemination, forwarding, printing or copying of this
email is strictly prohibited. Should you receive this email in error please
immediately notify it@...

This email has been scanned for viruses, however you should always scan emails
with your own systems prior to opening.
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help