[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bitshift of int8, int16, etc?

From: John W. Eaton
Subject: Re: bitshift of int8, int16, etc?
Date: Mon, 25 Jun 2007 11:58:51 -0400

On 25-Jun-2007, David Bateman wrote:

| I believe the attached patch is what is needed to make the bitshift of
| signed integer types the same as bitshifts of floating point numbers

I'm not sure what is best here.

My copy of K&R says

  The result is undefined if the right operand is negative, or greater
  than or equal to the number of bits in the left expressions type.


  The value of E1 << E2 is E1 (interpreted as a bit pattern)
  left-shifted E2 bits; in the absence of overflow, this is equivalent
  to multiplication by 2^E2.

(I'm not sure what is supposed to happen for overflow; I guess
undefined behavior.)

and finally

  The right shift is equivalent to division by 2^E2 if E1 is unsigned
  or if it has a non-negative value; otherwise the result is
  implementation defined.

So, do we follow C here and have results that could be system
dependent, or do we try harder to ensure that the results are the same
for all systems, or do we just throw errors for the cases that are
undefined in C?

I think I would prefer to ensure consistent behavior on all systems,
even if that means that some operations aren't available or generate


reply via email to

[Prev in Thread] Current Thread [Next in Thread]