Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Question |
| Date | 2011-06-08 11:55 -0700 |
| Organization | None to speak of |
| Message-ID | <lnzklscfvf.fsf@nuthaus.mib.org> (permalink) |
| References | <cbbad7e4-3532-4062-b39e-e4ad92a34f9d@p13g2000yqh.googlegroups.com> <8739jkmdin.fsf@blp.benpfaff.org> <e2ce4af8-9fb7-459c-88f6-58764f24f6b8@g12g2000yqd.googlegroups.com> <87y61cky4a.fsf@blp.benpfaff.org> <1bvcwgf9a0.fsf@snowball.wb.pfeifferfamily.net> |
Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
> Ben Pfaff <blp@cs.stanford.edu> writes:
>> Mateusz_madi <madi.czadi@gmail.com> writes:
>>> if char is 8 bit long so -1 is : 1111 1111
>>> and i assign it to int which let's say is 32 bit long, shoudn't int
>>> look like: 0000 0000 0000 1111 ?
>>
>> No. Type conversions in C preserve value, not bit pattern. If
>> you assign -1, of any integer type, to an object with any other
>> signed integer type, the destination will have value -1.
>
> To amplify (so I'm not disagreeing with Ben here), in general if a
> signed value is copied into a larger word, the value of the integer is
> preserved if the sign bit is extended across all the new high-order
> bits. So, since your sign bit was a 1, all the new bits are 1's as
> well. This is called "sign-extending".
>
>> I don't understand how you'd come up with the bit pattern that
>> you suggested anyway. You wrote a total of 16 bits (not 32), and
>> you only made 4 of them 1-bits (not 8).
>
> I'll guess he meant
>
> 0000 0000 0000 0000 0000 0000 1111 1111
>
> Sign-extended, he would actually get
>
> 1111 1111 1111 1111 1111 1111 1111 1111
>
> Something else that ought to be noted is that char can be either signed
> or unsigned (it's implementation-dependent). If he wants it to be
> signed, he should use a signed char.
Sign-extension preserves the value if negative numbers are
represented using 2's-complement. If the implementation uses either
1s'-complement or sign-and-magnitude, then it needs to use some other
method to preserve the value (I'm too lazy to work out the details).
The point is that the standard requires the value to be preserved,
without regard to how either the source or the target is represented,
or how much extra work the implementation has to go through to get
the required result.
A 2's-complement representation makes some operations easier,
which is why it's used so often.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Question Mateusz_madi <madi.czadi@gmail.com> - 2011-06-08 10:31 -0700
Re: Question Ben Pfaff <blp@cs.stanford.edu> - 2011-06-08 10:36 -0700
Re: Question Mateusz_madi <madi.czadi@gmail.com> - 2011-06-08 10:49 -0700
Re: Question Ben Pfaff <blp@cs.stanford.edu> - 2011-06-08 10:54 -0700
Re: Question Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-08 12:49 -0600
Re: Question Keith Thompson <kst-u@mib.org> - 2011-06-08 11:55 -0700
Re: Question Ben Pfaff <blp@cs.stanford.edu> - 2011-06-08 12:21 -0700
Re: Question "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-06-08 16:02 -0700
Re: Question Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-08 17:31 -0600
Re: Question Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-08 17:30 -0600
Re: Question Peter Nilsson <airia@acay.com.au> - 2011-06-08 16:34 -0700
Re: Question "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-06-08 16:52 -0400
Re: Question Ben Pfaff <blp@cs.stanford.edu> - 2011-06-08 13:54 -0700
Re: Question "J. J. Farrell" <jjf@bcs.org.uk> - 2011-06-09 03:22 +0100
Re: Question Keith Thompson <kst-u@mib.org> - 2011-06-08 10:47 -0700
csiph-web