Skip to content

Inaccuracies in the reference about bitshifts and overflows #23421

Closed
@simias

Description

@simias

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions