C# Reference ยท Operators and Expressions
C# bitwise and shift operators
Reference for C# bitwise and shift operators: &, |, ^, ~, <<, >>, >>>, flag enums, masks, arithmetic versus logical shift and shift count rules.
Bitwise operators work on the individual bits of integer values. & keeps the bits set in both operands, | keeps bits set in either, ^ keeps bits that differ, and ~ flips every bit. The shift operators <<, >> and >>> move bits left or right.
They are used for flags, masks, packed data, hashing, checksums and protocols. The same symbols &, | and ^ on bool operands are logical operators (see logical operators); on enums they combine flags.
int flags = 0b0101; bool hasBit2 = (flags & 0b0100) != 0; // test a bit flags |= 0b0010; // set a bit flags &= ~0b0001; // clear a bit
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
r = x & y; r = x | y; r = x ^ y; r = ~x; r = x << n; r = x >> n; r = x >>> n; x &= y; x |= y; x ^= y; x <<= n; x >>= n; x >>>= n;
| Operator | Description |
|---|---|
x & y |
Bits set in both. |
x | y |
Bits set in either. |
x ^ y |
Bits set in exactly one. |
~x |
Complement: every bit flipped. |
x << n |
Shift left, filling with zeros. |
x >> n |
Shift right; signed types copy the sign bit (arithmetic), unsigned types fill with zeros. |
x >>> n |
Unsigned (logical) right shift: always fills with zeros. Added in C# 11. |
Returns: a value of the promoted type. byte, sbyte, short, ushort and char operands become int. The shift count must be int, and only its low 5 bits (for int and uint) or low 6 bits (for long and ulong) count.
Errors: the operators exist for integral types; &, |, ^ and ~ also work on enums, and &, |, ^ on bool. Shifting a float, double or decimal is a compile error. Mixing a signed and an unsigned operand of the same size, such as int & uint, converts both to long.
Usage notes
- Use parentheses.
&,^and|have lower precedence than==:x & mask == 0does not compile (mask == 0is evaluated first). Write(x & mask) == 0. ~on small types gives a negativeint:~5is-6. Cast or mask (~a & 0xFF) to get the bits you expect.- Arithmetic versus logical shift.
-16 >> 2is-4;-16 >>> 2is1073741820. Foruintandulongboth forms are the same. - Shifting by the width or more does not clear the value; the count wraps. Check the count if it comes from input.
- Overflow:
<<discards bits shifted out; in acheckedcontext only arithmetic operations throw, shifts never do. - Flags enums need values that are powers of two and the
[Flags]attribute for readableToString(). Test with(value & flag) == flagorHasFlag. - Digit separators and binary literals (
0b1010_0101, C# 7) make masks readable.BitOperations,BitConverterandBinaryPrimitivescover counting, rotating and byte-order work. - Performance: each operation is one instruction. Shifts replace multiplication or division by powers of two; the JIT does this for constants already.