Velocity Reviews > bitshift operator problem

# bitshift operator problem

CBFalconer
Guest
Posts: n/a

 09-06-2008
>
>> If E1 has a signed type and nonnegative value,
>> and E1×2E2 is representable in the result type, then that is
>> the resulting value; OTHERWISE, the behavior is undefined.

>
> What is OTHERWISE?

If E1 has an unsigned type or negative value, or E1*2E2 is not
representablie in the result type.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

CBFalconer
Guest
Posts: n/a

 09-06-2008
Ali Karaali wrote:
>

.... total lack of useful quotes ...
>
>
> signed char i = 1;

works !!!
>
> i << 7;

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

Ben Bacarisse
Guest
Posts: n/a

 09-06-2008
CBFalconer <(E-Mail Removed)> writes:

>>
>>> If E1 has a signed type and nonnegative value,
>>> and E1Ã—2E2 is representable in the result type, then that is
>>> the resulting value; OTHERWISE, the behavior is undefined.

>>
>> What is OTHERWISE?

>
> If E1 has an unsigned type or negative value, or E1*2E2 is not
> representablie in the result type.

Not sure if the smiley is there just to say "yes, I know I got this
wrong, I am joking".

For the OP: (1) the odd E1x2E2 means E1 times 2 raised to the power E2
(the text version of the standard that CBFalconer uses looses the
formatting of powers). (2) The otherwise refers to those cases when
the result is not previously covered. You need to read the whole
thing, including the previous part:

3 The integer promotions are performed on each of the operands. The
type of the result is that of the promoted left operand. If the
value of the right operand is negative or is greater than or equal
to the width of the promoted left operand, the behavior is
undefined.

4 The result of E1 << E2 is E1 left-shifted E2 bit positions;
vacated bits are filled with zeros. If E1 has an unsigned type,
the value of the result is E1 x 2^E2 , reduced modulo one more
than the maximum value representable in the result type. If E1 has
a signed type and nonnegative value, and E1 x 2^E2 is
representable in the result type, then that is the resulting
value; otherwise, the behavior is undefined."

So E2 being too big or too small (negative) is covered by 3. In all
other cases E1 << E2 is well-defined if E1 is an unsigned type. The
otherwise refers to those cases when E1 is signed and the result can't
be represented in the promoted type. Thus, on most systems you will
come across,

1 << (sizeof(int) * CHAR_BIT - 1)

is undefined.

--
Ben.

Ben Bacarisse
Guest
Posts: n/a

 09-06-2008
Ali Karaali <(E-Mail Removed)> writes:

>> Eric Sosman
>> (E-Mail Removed)

>
>
> signed char i = 1;
>
> i << 7;

This is fine (but daft sine the result is thrown away). The shift is
performed after promoting the operands and 1 << 7 is representable as
in int (the promoted type). See my other message to the full wording.

This, however,

i = i << 7;

is likely to be a problem because the integer result of the shift
(12 probably can't be converted back to a signed char. C99 says you
get implementation-defined behaviour or an implementation-defined
signal is raised.

--
Ben.