Re: Is it a bug when use “<<”if the operator is out of the size "0~63"

2012-02-24 Thread Jonathan Wakely
This question is not appropriate for this mailing list, questions
about using GCC should be sent to the gcc-h...@gcc.gnu.org list,
please take any follow up there, thanks.

On 24 February 2012 08:34, Yang Yueming wrote:
>
> The result of xyz should be "0",but it is "2468acf123579bc" ,same as  xyz = 
> abc << 1,Why?

The C standard says it's undefined behaviour:

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


Re: Is it a bug when use “<<”if the operator is out of the size "0~63"

2012-02-24 Thread Miles Bader
Yang Yueming  writes:
> long long abc = 0x01234567891abcde;
> long long xyz;
...
>   xyz = abc << 65; 
...
> The result of xyz should be "0",but it is "2468acf123579bc" ,same as
> xyz = abc << 1,Why?

Because the shift operators in C have an undefined result when the
shift-count is larger than the size of the type.

As the compiler generally just generates the underlying CPU's shift
instruction, then what happens in practice depends on the CPU.
Different CPUs do different things in such a case, and one popular
alternative is to simply interpret the shift-count modulo the word-size.

> So as for "int a<<35"and etc..

Same thing.

-miles

-- 
Electricity, n. The cause of all natural phenomena not known to be caused by
something else.