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 > y >> (8 * (sizeof(int) -1))
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
y >> (8 * (sizeof(int) -1))

Réponse
 
LinkBack Outils de la discussion
Vieux 15/04/2008, 17h47   #1
aarklon@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut y >> (8 * (sizeof(int) -1))


Is y >> (8 * (sizeof(int) -1)) portable expression to find The MSB of
an unsigned integer y ??

will this work in all the cases, all three of the representations C
allows:

1) two's complement
2) ones'complement
3) signed magnitude.
  Réponse avec citation
Vieux 15/04/2008, 17h55   #2
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: y >> (8 * (sizeof(int) -1))

aarklon@gmail.com wrote:
> Is y >> (8 * (sizeof(int) -1)) portable expression to find The MSB of
> an unsigned integer y ??

Probably better and more portable:
#include <limits.h>
....
y >> (CHAR_BIT * (sizeof y -1))

> will this work in all the cases, all three of the representations C
> allows:
>
> 1) two's complement
> 2) ones'complement
> 3) signed magnitude.


Bye, Jojo


  Réponse avec citation
Vieux 15/04/2008, 17h56   #3
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: y >> (8 * (sizeof(int) -1))

On Tue, 15 Apr 2008 09:47:15 -0700, aarklon wrote:
> Is y >> (8 * (sizeof(int) -1)) portable expression to find The MSB of
> an unsigned integer y ??


No, it isn't.

> will this work in all the cases, all three of the representations C
> allows:
>
> 1) two's complement
> 2) ones'complement
> 3) signed magnitude.


These differ in how negative values are represented. Why do you think
negative values matter, given that you've got an unsigned integer?
  Réponse avec citation
Vieux 15/04/2008, 23h48   #4
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: y >> (8 * (sizeof(int) -1))

aark...@gmail.com wrote:
> Is y >> (8 * (sizeof(int) -1)) portable expression to find The MSB of
> an unsigned integer y ??


Define MSB in the following case...

CHAR_BIT = 8
UINT_MAX = 262143

If you want the top CHAR_BIT bits from an unsigned int y, use...

y / ((UINT_MAX >> (CHAR_BIT - 1) >> 1) + 1)

> will this work in all the cases, all three of the representations C
> allows:
>
> 1) two's complement
> 2) ones'complement
> 3) signed magnitude.


Unsigned integers use pure binary representation.

--
Peter
  Réponse avec citation
Vieux 16/04/2008, 03h08   #5
Jack Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: y >> (8 * (sizeof(int) -1))

On Tue, 15 Apr 2008 09:47:15 -0700 (PDT), aarklon@gmail.com wrote in
comp.lang.c:

>
> Is y >> (8 * (sizeof(int) -1)) portable expression to find The MSB of
> an unsigned integer y ??


It is absolutely not portable, even if you change 8 to CHAR_BIT.

I do a lot of work on a DSP where sizeof(char), sizeof(short), and
sizeof(int) are all 1. CHAR_BIT happens to be 16, of course, and
sizeof(long) is 2, sizeof(long long) is 4.

So on this implementation, either of the expressions:

(8 * (1 - 1))

....or:

(16 * (1 - 1))

....will not tell you anything about the MSB.

> will this work in all the cases, all three of the representations C
> allows:
>
> 1) two's complement
> 2) ones'complement
> 3) signed magnitude.


As others have stated, the representation of the signed int types in a
C implementation has nothing to do with the unsigned types.

Try this:

#include <limits.h>

#define UINT_MSB_MASK (UINT_MAX - (UINT_MAX >> 1))

if (some_unsigned_int & UINT_MSB_MASK)
/* do stuff because it is set */
else
/* do stuff because it is not set */

--
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
Vieux 16/04/2008, 18h00   #6
aarklon@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: y >> (8 * (sizeof(int) -1))

On Apr 15, 9:56 pm, Harald van D©¦k <true...@gmail.com> wrote:
> On Tue, 15 Apr 2008 09:47:15 -0700,aarklonwrote:
> > Is y >> (8 * (sizeof(int) -1)) portable expression to find The MSB of
> > an unsigned integer y ??

>
> No, it isn't.
>
> > will this work in all the cases, all three of the representations C
> > allows:

>
> > 1) two's complement
> > 2) ones'complement
> > 3) signed magnitude.

>
> These differ in how negative values are represented. Why do you think
> negative values matter, given that you've got an unsigned integer?



what about finding the MSB of a signed integer ?
  Réponse avec citation
Vieux 16/04/2008, 20h18   #7
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: y >> (8 * (sizeof(int) -1))

aarklon@gmail.com wrote, On 16/04/08 18:00:

<snip>

> what about finding the MSB of a signed integer ?


You cannot find that by shifting since right shifting a negative value
gives an implementation defined result and left shifting a 1 in to the
MSB invokes undefined behaviour. If you want to know the sign and don't
care about negative zero then just use either <0 or >=0 as appropriate.
--
Flash Gordon
  Réponse avec citation
Vieux 19/04/2008, 15h31   #8
Spiros Bousbouras
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: y >> (8 * (sizeof(int) -1))

On 16 Apr, 03:08, Jack Klein <jackkl...@spamcop.net> wrote:
> On Tue, 15 Apr 2008 09:47:15 -0700 (PDT), aark...@gmail.com wrote in
> comp.lang.c:
> > Is y >> (8 * (sizeof(int) -1)) portable expression to find TheMSBof
> > an unsigned integer y ??

>
> It is absolutely notportable, even if you change 8 to CHAR_BIT.
> <S N I P>
> Try this:
>
> #include <limits.h>
>
> #define UINT_MSB_MASK (UINT_MAX - (UINT_MAX >> 1))
>
> if (some_unsigned_int & UINT_MSB_MASK)
> /* do stuff because it is set */
> else
> /* do stuff because it is not set */


I think he got the parentheses wrong.
y >> (CHAR_BIT * sizeof(int) - 1)
where the expression will be 1 if and only
if the MSB of y is set , seems fine to me.

But your solution looks neater and is likely
faster.
  Réponse avec citation
Vieux 19/04/2008, 15h49   #9
Spiros Bousbouras
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: y >> (8 * (sizeof(int) -1))

On 16 Apr, 20:18, Flash Gordon <s...@flash-gordon.me.uk> wrote:
> aark...@gmail.com wrote, On 16/04/08 18:00:
>
> <snip>
>
> > what about finding the MSB of a signed integer ?

>
> You cannot find that by shifting since right shifting a negative value
> gives an implementation defined result and left shifting a 1 in to the
> MSB invokes undefined behaviour. If you want to know the sign and don't
> care about negative zero then just use either <0 or >=0 as appropriate.


I think that by "MSB of a signed integer" he means
the bit which contributes the most to the magnitude
of the value rather than the sign bit.

I believe that a variation of Jack Klein's solution
for unsigned integers will also work in this case:

#include <limits.h>

#define INT_MSB_MASK (INT_MAX - (INT_MAX >> 1))

if (#include <limits.h>


#define UINT_MSB_MASK (UINT_MAX - (UINT_MAX >> 1))

if (some_unsigned_int == 0)
/* do stuff because it is not set */
else if (some_signed_int & INT_MSB_MASK)
/* do stuff because it is set */
else
/* do stuff because it is not set */
  Réponse avec citation
Vieux 19/04/2008, 17h14   #10
Tomás Ó hÉilidhe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: y >> (8 * (sizeof(int) -1))


On a minority of systems, there's padding bits within integer types,
and so sizeof(integer type) won't give you the amount of value
representational bits.
  Réponse avec citation
Vieux 22/04/2008, 18h44   #11
Spiros Bousbouras
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: y >> (8 * (sizeof(int) -1))

On 19 Apr, 15:49, Spiros Bousbouras <spi...@gmail.com> wrote:
> On 16 Apr, 20:18, Flash Gordon <s...@flash-gordon.me.uk> wrote:
>
> > aark...@gmail.com wrote, On 16/04/08 18:00:

>
> > <snip>

>
> > > what about finding the MSB of a signed integer ?

>
> > You cannot find that by shifting since right shifting a negative value
> > gives an implementation defined result and left shifting a 1 in to the
> > MSB invokes undefined behaviour. If you want to know the sign and don't
> > care about negative zero then just use either <0 or >=0 as appropriate..

>
> I think that by "MSB of a signed integer" he means
> the bit which contributes the most to the magnitude
> of the value rather than the sign bit.
>
> I believe that a variation of Jack Klein's solution
> for unsigned integers will also work in this case:
>
> #include <limits.h>
>
> #define INT_MSB_MASK (INT_MAX - (INT_MAX >> 1))
>
> if (#include <limits.h>
>
> #define UINT_MSB_MASK (UINT_MAX - (UINT_MAX >> 1))
>
> if (some_unsigned_int == 0)
> /* do stuff because it is not set */
> else if (some_signed_int & INT_MSB_MASK)
> /* do stuff because it is set */
> else
> /* do stuff because it is not set */


I bungled up my post above. The correct version is

#include <limits.h>

#define INT_MSB_MASK (INT_MAX - (INT_MAX >> 1))

if (some_signed_int == 0)
/* do stuff because it is not set */
else if (some_signed_int & INT_MSB_MASK)
/* do stuff because it is set */
else
/* do stuff because it is not set */

The above treats specially the value 0 in that even if it is
negative zero in an one's complement representation it still
considers the bit not to be set.
  Réponse avec citation
Vieux 22/04/2008, 18h45   #12
Spiros Bousbouras
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: y >> (8 * (sizeof(int) -1))

On 19 Apr, 17:14, Tomás Ó hÉilidhe <t...@lavabit.com> wrote:
> On a minority of systems, there's padding bits within integer types,
> and so sizeof(integer type) won't give you the amount of value
> representational bits.


Therefore the expression y >> (CHAR_BIT * sizeof(int) - 1) is not
a portable way to check if the MSB is set after all. If there is
even one padding bit the expression will always yield 0 regardless
of whether the MSB is set or not.
  Réponse avec citation
Vieux 22/04/2008, 18h53   #13
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: y >> (8 * (sizeof(int) -1))

On Tue, 22 Apr 2008 10:45:41 -0700, Spiros Bousbouras wrote:
> On 19 Apr, 17:14, Tomás Ó hÉilidhe <t...@lavabit.com> wrote:
>> On a minority of systems, there's padding bits within integer types,
>> and so sizeof(integer type) won't give you the amount of value
>> representational bits.

>
> Therefore the expression y >> (CHAR_BIT * sizeof(int) - 1) is not a
> portable way to check if the MSB is set after all. If there is even one
> padding bit the expression will always yield 0 regardless of whether the
> MSB is set or not.


If there is any padding, (CHAR_BIT * sizeof(int) - 1) will be greater
than or equal to the width of unsigned int, meaning the behaviour is
undefined if you right-shift by that amount. The expression won't
necessarily evaluate to 0.
  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 12h22.


É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,26308 seconds with 21 queries