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
Published at
2026-10-28 14:37:53 UTCEvent JSON
{
"id": "dee1e62e36cf75d2c1f8e88f8691f12df6875529747e4fa4d5eac8576d6240de",
"pubkey": "e68a0856e90d9997b1f721e905249896412dd570399a987b2e8fd5e7fcfe2279",
"created_at": 1793198273,
"kind": 1,
"tags": [
[
"t",
"tip"
]
],
"content": "Learn: Bitwise tricks\n\nOperators: \u0026 (and), | (or), ^ (xor), ~ (not), \u003c\u003c and \u003e\u003e (shift).\n\n```csharp\nint n = 12; // 1100 in binary\nConsole.WriteLine(n \u0026 1);\nConsole.WriteLine((n \u0026 (n - 1)) == 0);\nConsole.WriteLine(5 ^ 3 ^ 5);\nConsole.WriteLine(1 \u003c\u003c 4);\n```\n\nn \u0026 1 is 0 for even numbers. n \u003e 0 \u0026\u0026 (n \u0026 (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 \u003c\u003c 4 is 16.\nUse them for flags, compact sets of booleans, and these classic interview tricks.\n\nRead more: https://www.geeksforgeeks.org/dsa/bitwise-algorithms/\n\n#tip · TIP-042",
"sig": "5f866d2a779e2a49ff60ddd0fd83fb2e6528733d1687dc1bee01a130fa50e964b751d65625b193dda55952c495ec32460a463fc204fb3976b21c319d60543847"
}