PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Quick check on signed promotion of bytes
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Quick check on signed promotion of bytes

Réponse
 
LinkBack Outils de la discussion
Vieux 31/01/2008, 10h45   #1
toe@lavabit.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Quick check on signed promotion of bytes


I have a byte called "x". I want byte "y" to be the complement of
"x" (i.e. all the bits flipped).

Initially I wrote:

char unsigned x, y;

...

x = 72;

y = ~x;

But then I thought that the following might happen on your average
system (CHAR_BIT == 8, sizeof(int) == 4):

1) x is promoted to signed int.
2) The complement is take of this signed int.
3) This signed int is then converted to an unsigned char

Am I right in thinking that this is what will happen? If so, would I
be wise to do the following instead:

y = ~(unsigned)x;

--
Tomás Ó hÉilidhe
  Réponse avec citation
Vieux 31/01/2008, 10h48   #2
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Quick check on signed promotion of bytes

On Jan 31, 12:45 pm, t...@lavabit.com wrote:
> I have a byte called "x". I want byte "y" to be the complement of
> "x" (i.e. all the bits flipped).
>
> Initially I wrote:
>
> char unsigned x, y;
>
> ...
>
> x = 72;
>
> y = ~x;
>
> But then I thought that the following might happen on your average
> system (CHAR_BIT == 8, sizeof(int) == 4):
>
> 1) x is promoted to signed int.

That won't happend, therefore the rest of your thoughts is false.
Your code is perfectly valid.
  Réponse avec citation
Vieux 31/01/2008, 10h54   #3
toe@lavabit.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Quick check on signed promotion of bytes

On Jan 31, 10:48am, vipps...@gmail.com wrote:

> > 1) x is promoted to signed int.

>
> That won't happend, therefore the rest of your thoughts is false.
> Your code is perfectly valid.


My understanding was as follows:

"If you're dealing with an integer type smaller than int, then it
must undergo promotion before you can perform any operations on it."

--
Tomás Ó hÉilidhe


  Réponse avec citation
Vieux 31/01/2008, 16h22   #4
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Quick check on signed promotion of bytes

toe@lavabit.com wrote:
> I have a byte called "x". I want byte "y" to be the complement of
> "x" (i.e. all the bits flipped).
>
> Initially I wrote:
>
> char unsigned x, y;
>
> ...
>
> x = 72;
>
> y = ~x;
>
> But then I thought that the following might happen on your average
> system (CHAR_BIT == 8, sizeof(int) == 4):
>
> 1) x is promoted to signed int.
> 2) The complement is take of this signed int.
> 3) This signed int is then converted to an unsigned char


That's right, under your assumptions. On "exotic" machines
where UCHAR_MAX > INT_MAX (for example, on hardware where all
of char, short, and int are 16 bits wide), then x promotes to
an unsigned int instead of to an int in step 1.

Also see vippstar's response: It's wrong.

> Am I right in thinking that this is what will happen? If so, would I
> be wise to do the following instead:
>
> y = ~(unsigned)x;


Yes, this will work on both "exotic" and "humdrum" machines.

--
Eric.Sosman@sun.com

  Réponse avec citation
Vieux 31/01/2008, 17h17   #5
Tomás Ó hÉilidhe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Quick check on signed promotion of bytes

Eric Sosman:

>> 1) x is promoted to signed int.
>> 2) The complement is take of this signed int.
>> 3) This signed int is then converted to an unsigned char

>
> That's right, under your assumptions. On "exotic" machines
> where UCHAR_MAX > INT_MAX (for example, on hardware where all
> of char, short, and int are 16 bits wide), then x promotes to
> an unsigned int instead of to an int in step 1.



So just to confirm if I'm thinking right. We start off with:

char unsigned x, y;

x = 0xf0;

y = ~x;

What we _want_ this code to do is to give us the value 0x0f for y (i.e.
the complement of x). However what _could_ happen is:

1) x is promoted to signed int.

=> Now we have a signed int with the value 0xf0

2) The complement is taken of this signed int.

=> Now we have some other number that is negative. The exact number
depends on the number system in use (e.g. sign-magnitude), and
also the amount of bits in an int.

3) The signed int is converted to an unsigned char.

=> This process is well-defined, but we don't know what signed
value we have.

Therefore, it is my assumption that will not give the desired behaviour
on every implementation of the Standard -- we could get something
totally different from 0x0f as an answer (even on an 8-bit-byte system).

That sound right?

--
Tomás Ó hÉilidhe
  Réponse avec citation
Vieux 31/01/2008, 17h48   #6
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Quick check on signed promotion of bytes

Tomás Ó hÉilidhe wrote:
> Eric Sosman:
>
>>> 1) x is promoted to signed int.
>>> 2) The complement is take of this signed int.
>>> 3) This signed int is then converted to an unsigned char

>> That's right, under your assumptions. On "exotic" machines
>> where UCHAR_MAX > INT_MAX (for example, on hardware where all
>> of char, short, and int are 16 bits wide), then x promotes to
>> an unsigned int instead of to an int in step 1.

>
>
> So just to confirm if I'm thinking right. We start off with:
>
> char unsigned x, y;
>
> x = 0xf0;
>
> y = ~x;
>
> What we _want_ this code to do is to give us the value 0x0f for y (i.e.
> the complement of x). However what _could_ happen is:
>
> 1) x is promoted to signed int.
>
> => Now we have a signed int with the value 0xf0
>
> 2) The complement is taken of this signed int.
>
> => Now we have some other number that is negative. The exact number
> depends on the number system in use (e.g. sign-magnitude), and
> also the amount of bits in an int.
>
> 3) The signed int is converted to an unsigned char.
>
> => This process is well-defined, but we don't know what signed
> value we have.
>
> Therefore, it is my assumption that will not give the desired behaviour
> on every implementation of the Standard -- we could get something
> totally different from 0x0f as an answer (even on an 8-bit-byte system).
>
> That sound right?


Sounds right to me. The list of potential negative values
is long but "sparse:" two's complement and ones' complement give
-241 and -240, respectively, while signed magnitude gives one of
-0x7f0f, -0xff0f, -0x1ff0f, ... depending on the number of value
bits in an int.

--
Eric.Sosman@sun.com
  Réponse avec citation
Vieux 31/01/2008, 18h18   #7
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Quick check on signed promotion of bytes

On Jan 31, 6:22 pm, Eric Sosman <Eric.Sos...@sun.com> wrote:
> t...@lavabit.com wrote:
> > I have a byte called "x". I want byte "y" to be the complement of
> > "x" (i.e. all the bits flipped).

>
> > Initially I wrote:

>
> > char unsigned x, y;

>
> > ...

>
> > x = 72;

>
> > y = ~x;

>
> > But then I thought that the following might happen on your average
> > system (CHAR_BIT == 8, sizeof(int) == 4):

>
> > 1) x is promoted to signed int.
> > 2) The complement is take of this signed int.
> > 3) This signed int is then converted to an unsigned char

>
> That's right, under your assumptions. On "exotic" machines
> where UCHAR_MAX > INT_MAX (for example, on hardware where all
> of char, short, and int are 16 bits wide), then x promotes to
> an unsigned int instead of to an int in step 1.
>
> Also see vippstar's response: It's wrong.

Ah, I apologise, just something i am not sure about, char unsigned x,
y; means unsigned char x, 'plain' char y?
  Réponse avec citation
Vieux 31/01/2008, 18h23   #8
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Quick check on signed promotion of bytes

vippstar@gmail.com wrote:

> On Jan 31, 6:22 pm, Eric Sosman <Eric.Sos...@sun.com> wrote:
>> t...@lavabit.com wrote:
>> > I have a byte called "x". I want byte "y" to be the complement of
>> > "x" (i.e. all the bits flipped).

>>
>> > Initially I wrote:

>>
>> > char unsigned x, y;

>>
>> > ...

>>
>> > x = 72;

>>
>> > y = ~x;

>>
>> > But then I thought that the following might happen on your average
>> > system (CHAR_BIT == 8, sizeof(int) == 4):

>>
>> > 1) x is promoted to signed int.
>> > 2) The complement is take of this signed int.
>> > 3) This signed int is then converted to an unsigned char

>>
>> That's right, under your assumptions. On "exotic" machines
>> where UCHAR_MAX > INT_MAX (for example, on hardware where all
>> of char, short, and int are 16 bits wide), then x promotes to
>> an unsigned int instead of to an int in step 1.
>>
>> Also see vippstar's response: It's wrong.

> Ah, I apologise, just something i am not sure about, char unsigned x,
> y; means unsigned char x, 'plain' char y?


No, it's equivalent to:

unsigned char x;
unsigned char y;

  Réponse avec citation
Vieux 31/01/2008, 18h36   #9
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Quick check on signed promotion of bytes

vippstar@gmail.com wrote:
>
> Ah, I apologise, just something i am not sure about, char unsigned x,
> y; means unsigned char x, 'plain' char y?


`char unsigned' means the same thing as `unsigned char';
the order of the keywords in a multi-keyword type specifier
doesn't matter. So `char unsigned x, y;' means the same thing
as `unsigned char x, y;' and gives both x and y the same type.

Win pocket money by writing `long int signed long x;'
or `double long y;' and betting the boys at the bar that
it's legal C.

--
Eric.Sosman@sun.com

  Réponse avec citation
Vieux 01/02/2008, 04h25   #10
Jack Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Quick check on signed promotion of bytes

On Thu, 31 Jan 2008 02:48:28 -0800 (PST), vippstar@gmail.com wrote in
comp.lang.c:

> On Jan 31, 12:45 pm, t...@lavabit.com wrote:
> > I have a byte called "x". I want byte "y" to be the complement of
> > "x" (i.e. all the bits flipped).
> >
> > Initially I wrote:
> >
> > char unsigned x, y;
> >
> > ...
> >
> > x = 72;
> >
> > y = ~x;
> >
> > But then I thought that the following might happen on your average
> > system (CHAR_BIT == 8, sizeof(int) == 4):
> >
> > 1) x is promoted to signed int.

> That won't happend, therefore the rest of your thoughts is false.
> Your code is perfectly valid.


No, your correction is false. 'x' must be promoted to either signed
or unsigned int before the bitwise inversion operator is applies. In
a system with CHAR_BIT is 8, a signed int must be able to hold all
possible values of unsigned char, so 'x' is indeed promoted to signed
int.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h00.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,23128 seconds with 18 queries