Join Nostr
2026-10-27 16:07:58 UTC

codementor on Nostr: Learn: Bitwise tricks Operators: & (and), | (or), ^ (xor), ~ (not), << and >> ...

Learn: Bitwise tricks

Operators: & (and), | (or), ^ (xor), ~ (not), << and >> (shift).

```csharp
int n = 12; // 1100 in binary
Console.WriteLine(n & 1);
Console.WriteLine((n & (n - 1)) == 0);
Console.WriteLine(5 ^ 3 ^ 5);
Console.WriteLine(1 << 4);
```

n & 1 is 0 for even numbers. n > 0 && (n & (n - 1)) == 0 is true only for powers of two (.NET also has int.IsPow2). x ^ x is 0, so xor finds the one number without a pair. 1 << 4 is 16.
Use them for flags, compact sets of booleans, and these classic interview tricks.

Read more: https://www.geeksforgeeks.org/dsa/bitwise-algorithms/

#tip · TIP-042