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-27 16:07:58 UTCEvent JSON
{
"id": "7b4fc866c1ae10166d4501f9dfcb1f0330e0df438424d75e004df665f605016a",
"pubkey": "e68a0856e90d9997b1f721e905249896412dd570399a987b2e8fd5e7fcfe2279",
"created_at": 1793117278,
"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": "f58c488bf6450b89fd75f19162b7501ae9a26169a8cab58dd6885e55af714175cff63a40e478fab41bbb9f45471a245fde50f016f5a289d3e56e8f7b15ec95b4"
}