<!--
{
  "documentType" : "article",
  "framework" : "Xcode",
  "identifier" : "/documentation/Xcode/invalid-shift",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Invalid shift"
}
-->

# Invalid shift

Detects invalid and overflowing shifts.

## Overview

Use this check to detect bitwise shifts with invalid shift amounts and shifts that might overflow. These shifts have undefined behavior and the optimizer may omit them. Available in Xcode 9 and later.

### Invalid shift amount in C

The following code shows a shift with an invalid shift amount because the destination type can’t represent the result:

```occ
int32_t x = 1;
x <<= 32; // Error: (1 << 32) can't be represented in an int32_t
```

If the optimizer can prove that a shift amount may be invalid, it may replace the result of the shift with an arbitrary value.

#### Solution

Use a larger destination type, such as an `int64_t`.

### Shift overflow in C

In the following code, the second shift overflows `x` because `int32_t` can’t represent `((1U << 31) - 1) << 2`:

```occ
int32_t x = (1U << 31) - 1;
x <<= 2; // Error: the shift result can't fit in x
```

#### Solution

Use a larger destination type, such as an `int64_t`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)