Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Shift Operators: Syntax and Behavior

Learn how C# shift operators work, including sign extension, masking rules, and practical uses for bit flags and arithmetic.

~5 min readC#
#bit manipulation#operators#C# syntax#integer arithmetic
Diagram showing left and right shift operations on binary bits in C#.

Shift operators move the bits of an integer value left or right. In C#, << shifts left and >> shifts right. Their syntax is simple, but their behavior depends on the integer type, the sign of the value, and the shift count, so it is worth reviewing these details before using them in bit manipulation or arithmetic code.

How the Left Shift Operator Works

The left shift operator << moves each bit of an integer to a higher position. The low-order bits are filled with zeros. For an int value, the operation is equivalent to multiplying by a power of two, as long as no bits are shifted out of the type's range.

csharp
int value = 5; // binary: 00000000 00000000 00000000 00000101 int shifted = value << 2; // binary: 00000000 00000000 00000000 00010100 = 20

Here, 5 << 2 produces 20, which is 5 * 2^2. The bits that fall off the most significant end are discarded. For signed types, this can change the sign bit and produce unexpected negative numbers when the result exceeds the maximum positive value.

How the Right Shift Operator Works

The right shift operator >> moves bits toward the least significant position. For signed integers, the behavior depends on the sign of the value. For non-negative numbers, the high-order bits are filled with zeros. For negative numbers, the high-order bits are filled with ones, preserving the sign. This is called an arithmetic shift. For unsigned types, right shift always fills with zeros.

csharp
int positive = 20; // binary: 00000000 00000000 00000000 00010100 int posShifted = positive >> 2; // 5 int negative = -20; // binary: 11111111 11111111 11111111 11101100 int negShifted = negative >> 2; // -5

In the negative case, the sign bit is copied into the vacated high-order positions. This is different from a logical right shift, which always fills with zeros. C# does not have a dedicated logical right shift operator; you can achieve it by casting to an unsigned type first.

csharp
int value = -20; uint unsignedValue = (uint)value; uint logicalShifted = unsignedValue >> 2; // fills with zeros

Shift Counts and the Masking Rule

The shift count in C# is not used directly. The runtime masks the count based on the type's bit width. For int and uint (32-bit), the count is masked to the lower 5 bits. For long and ulong (64-bit), it is masked to the lower 6 bits. This means that shifting by 32 for an int is equivalent to shifting by 0, not by 32.

csharp
int a = 1; int b = a << 32; // same as a << 0, result is 1 int c = a << 33; // same as a << 1, result is 2

This behavior is defined by the C# specification and matches the underlying CLR instructions. It can be surprising if you expect a shift count larger than the bit width to produce zero. Always keep the effective shift count within the range of the type to avoid confusion.

Using Shift Operators for Bit Flags

One of the most common practical uses of shift operators is to define and manipulate bit flags. Instead of writing out powers of two manually, you can express them as shifts.

csharp
[Flags] public enum Permissions { None = 0, Read = 1 << 0, // 1 Write = 1 << 1, // 2 Execute = 1 << 2, // 4 Delete = 1 << 3 // 8 }

Shifting makes the sequence explicit and reduces the chance of typos when you add new flags. To combine flags, use the bitwise OR operator. To test a flag, use the AND operator.

csharp
Permissions user = Permissions.Read | Permissions.Write; bool canWrite = (user & Permissions.Write) != 0;

Shift operators also help when you need to pack multiple small values into a single integer. For example, storing two 16-bit values in a 32-bit integer:

csharp
int packed = (high << 16) | low; int highPart = packed >> 16; int lowPart = packed & 0xFFFF;

This pattern appears in protocol parsing, image processing, and other low-level code where memory or bandwidth is constrained. When using it, make sure both values fit in 16 bits so they do not overwrite each other's bits.

Multiplication and Division by Powers of Two

Because left shift multiplies by a power of two and right shift divides (for non-negative values), some developers use them to replace arithmetic operations. In modern C#, the JIT compiler can already optimize multiplication and division by constants into shift instructions when appropriate. Handwritten shifts usually do not improve performance and can hurt readability.

csharp
int multiplyBy8 = value << 3; // value * 8 int divideBy8 = value >> 3; // value / 8 (only for non-negative)

The division case is subtle. For negative numbers, >> performs an arithmetic shift, which is not the same as integer division. Integer division truncates toward zero, while an arithmetic shift floors toward negative infinity. For example, -7 / 2 in C# is -3, but -7 >> 1 is -4. If you need division semantics, use the / operator, not a shift.

Common Mistakes and Edge Cases

One frequent mistake is assuming that right shift on a negative number behaves like division. As noted, it does not. Another is forgetting the masking rule and writing a loop that shifts by a dynamic count without checking the range.

Overflow is another concern. Left shifting a signed integer can set the sign bit, producing a negative result. This is not an exception; it is defined behavior. If you need to shift without sign interference, use an unsigned type.

csharp
int value = 0x20000000; // 2^29 int result = value << 2; // 0x80000000, which is -2147483648 as an int

No overflow check applies to shift operations, even in a checked context; bits shifted beyond the type's width are discarded. If you need to detect that meaningful bits were lost, compare against the original value or validate the range before shifting.

Performance and Readability Tradeoffs

Shift operators are extremely fast at the CPU level, but in C# the JIT already emits efficient code for multiplication and division by constants. Using shifts for arithmetic rarely provides a measurable performance benefit in application code. The real value of shifts lies in bit manipulation, where they are the clearest way to express the intent.

When you use shifts, keep the code self-documenting. Name the constants or use an enum with [Flags] rather than scattering magic numbers. If you are shifting to pack or unpack data, add comments that show the bit layout. This helps future maintainers understand the code without having to reconstruct the binary math.

For performance-critical paths, shifts are appropriate, but always measure the impact. In most business applications, the bottleneck is I/O or database access, not integer arithmetic. Prefer clarity over micro-optimizations unless profiling shows a real need.

One final note on compatibility: shift operators work on int, uint, long, and ulong. They do not work directly on byte, short, or other smaller integer types. The compiler implicitly converts them to int before shifting, so the result is always at least int. Be aware of this when assigning back to a smaller type.

csharp
byte b = 1; int shifted = b << 8; // result is int, not byte

Understanding these behaviors allows you to use shift operators with confidence, whether you are implementing a bitmask, parsing a binary protocol, or optimizing a hot loop.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.