---
title: Invalid shift
framework: xcode
role: article
role_heading: Article
path: xcode/invalid-shift
---

# Invalid shift

Detects invalid and overflowing shifts.

## Overview

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

## See Also

### Undefined Behavior Sanitizer

- [Misaligned pointer](xcode/misaligned-pointer.md)
- [Invalid Boolean value](xcode/invalid-boolean.md)
- [Out-of-bounds array access](xcode/out-of-bounds-array-access.md)
- [Invalid enumeration value](xcode/invalid-enumeration-value.md)
- [Reaching of unreachable point](xcode/reaching-of-unreachable-point.md)
- [Dynamic type violation](xcode/dynamic-type-violation.md)
- [Invalid float cast](xcode/invalid-float-cast.md)
- [Division by zero](xcode/division-by-zero.md)
- [Nonnull argument violation](xcode/nonnull-argument-violation.md)
- [Nonnull return value violation](xcode/nonnull-return-value-violation.md)
- [Nonnull variable assignment violation](xcode/nonnull-variable-assignment-violation.md)
- [Null reference creation and null pointer dereference](xcode/null-reference-creation-and-null-pointer-dereference.md)
- [Invalid object size](xcode/invalid-object-size.md)
- [Integer overflow](xcode/integer-overflow.md)
- [Invalid variable-length array](xcode/invalid-variable-length-array.md)
