y >> (8 * (sizeof(int) -1))

A

aarklon

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.
 
J

Joachim Schmitz

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
 
H

Harald van Dijk

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?
 
P

Peter Nilsson

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.
 
A

aarklon

No, it isn't.



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 ?
 
F

Flash Gordon

(e-mail address removed) wrote, On 16/04/08 18:00:

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.
 
S

Spiros Bousbouras

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.
 
S

Spiros Bousbouras

(e-mail address removed) wrote, On 16/04/08 18:00:



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 */
 
T

Tomás Ó hÉilidhe

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.
 
S

Spiros Bousbouras

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.
 
S

Spiros Bousbouras

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.
 
H

Harald van Dijk

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top