Closed
Description
The references states that >>
is a logical right shift, however it seems that it behaves like an arithmetic right shift for signed integers. For instance the following snippet prints -2
:
let i = -4i32;
println!("{}", i >> 1);
A logical right shift should produce 2147483646
(assuming 2's complement representation which I think rust guarantees?).
In the same spirit the reference does not say if shifting outside the range of an integer is defined. From my quick test it doesn't appear to be:
let i = 10u32;
let j = 200u32;
println!("{}", i << j);
This prints garbage on my computer and on the playpen, so I assume it's UB or at least UV (it doesn't trigger the overflow check in debug builds though, maybe it should?)
On a similar topic the reference also states that the following behaviours are safe:
- Unsigned integer overflow (well-defined as wrapping)
- Signed integer overflow (well-defined as two's complement representation wrapping)
I believe that's not true anymore.
Metadata
Metadata
Assignees
Labels
No labels