Read the syntax
Combine bit masks and distinguish arithmetic from logical right shifts.
The OR operation preserves both set bits, producing integer 3.
What it is and why to use it
Integral values can represent individual on/off bits as well as ordinary numbers. Bitwise &, | and ^ compare or combine those bits; shifts move their positions. These operations are useful for packed formats and low-level masks. For domain permissions, named Flags enums are often clearer than unexplained numeric constants.
How it works
Left shift << moves bits toward more significant positions and discards bits that leave the fixed-width value. Right shift >> on a signed integer propagates the sign bit, while >>>, introduced in C# 11, shifts in zeroes. For int operands, only the low five bits of the shift count are used. Shifts are not checked arithmetic overflow operations.
Common mistakes and limitations
Do not assume shifting is always interchangeable with multiplication or division, especially for negative numbers and overflow. Do not confuse bitwise & with short-circuit &&. Name meaningful masks and keep signedness and operand width explicit.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
int combined = firstMask | secondMask;
int shifted = value >>> count;Examples
Combine and test independent bits
Model two flags using binary literals so their positions are visible.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
const int Read = 0b0001;
const int Write = 0b0010;
int permissions = Read | Write;
Console.WriteLine(permissions);
Console.WriteLine((permissions & Write) != 0);
- The binary constants occupy different bit positions.
- The OR operation preserves both set bits, producing integer 3.
- AND isolates the Write bit and the comparison converts the nonzero result into a boolean. This is a representation example, not an authorisation system.
- Expected output: 3 True
- Use named constants or enums to explain mask meanings.
Compare signed and zero-filling right shifts
Use a negative int to make the distinction visible.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
int value = -8;
Console.WriteLine(value >> 1);
Console.WriteLine(value >>> 1);
- The arithmetic right shift preserves the negative sign and yields -4.
- The unsigned right shift fills the leading position with zero and returns a nonnegative int here.
- The large second result reflects the 32-bit representation. >>> requires C# 11 or later; the documented C# 12 configuration supports it.
- Expected output: -4 2147483644
- Use named constants or enums to explain mask meanings.
Best practices
- Use named constants or enums to explain mask meanings.
- State operand width and signedness in bit-oriented code.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference