{"id":19624348,"url":"https://github.com/sk-azraf-sami/bit-manipulation","last_synced_at":"2025-02-26T19:35:34.594Z","repository":{"id":195892160,"uuid":"693734032","full_name":"Sk-Azraf-Sami/bit-manipulation","owner":"Sk-Azraf-Sami","description":"Bit manipulation is much important in problem solving context. But this topic is so much confusing.So, I created this note as a overview on bit manipulation","archived":false,"fork":false,"pushed_at":"2023-09-20T00:21:10.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-09T11:58:14.812Z","etag":null,"topics":["bit-manipulation","bitmanipulation","c","contest-programming","puzzle"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sk-Azraf-Sami.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-19T15:50:14.000Z","updated_at":"2023-09-20T00:27:06.000Z","dependencies_parsed_at":"2023-09-20T02:15:15.321Z","dependency_job_id":null,"html_url":"https://github.com/Sk-Azraf-Sami/bit-manipulation","commit_stats":null,"previous_names":["sk-azraf-sami/bit-manipulation"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sk-Azraf-Sami%2Fbit-manipulation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sk-Azraf-Sami%2Fbit-manipulation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sk-Azraf-Sami%2Fbit-manipulation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sk-Azraf-Sami%2Fbit-manipulation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sk-Azraf-Sami","download_url":"https://codeload.github.com/Sk-Azraf-Sami/bit-manipulation/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240923369,"owners_count":19879295,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bit-manipulation","bitmanipulation","c","contest-programming","puzzle"],"created_at":"2024-11-11T11:37:48.568Z","updated_at":"2025-02-26T19:35:34.556Z","avatar_url":"https://github.com/Sk-Azraf-Sami.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Bit Manipulation in 'C' Programming\n\n## Basic \nBitwise operators are fundamental operators in computer programming that work on individual bits (0s and 1s) within binary representations of numbers. These operators are commonly used in low-level programming, hardware manipulation, and various other applications. There are several bitwise operators:\n\n1. **Bitwise AND (`\u0026`):** This operator compares each bit position of two integers. If both bits are 1, the result is 1; otherwise, it's 0.\n\n   Example:\n   ```\n   5 (binary: 0101)\n   \u0026 3 (binary: 0011)\n   = 1 (binary: 0001)\n   ```\n\n2. **Bitwise OR (`|`):** This operator compares each bit position of two integers. If either of the bits is 1, the result is 1; otherwise, it's 0.\n\n   Example:\n   ```\n   5 (binary: 0101)\n   | 3 (binary: 0011)\n   = 7 (binary: 0111)\n   ```\n\n3. **Bitwise XOR (`^`):** This operator compares each bit position of two integers. If the bits are different (one is 0, the other is 1), the result is 1; otherwise, it's 0.\n\n   Example:\n   ```\n   5 (binary: 0101)\n   ^ 3 (binary: 0011)\n   = 6 (binary: 0110)\n   ```\n\n4. **Bitwise NOT (`~`):** This operator negates each bit. It flips 0s to 1s and 1s to 0s.\n\n   Example:\n   ```\n   ~5 (binary: 0101)\n   = -6 (binary: 1010)\n   ```\n\n5. **Left Shift (`\u003c\u003c`):** This operator shifts the bits of a number to the left by a specified number of positions. It effectively multiplies the number by 2 for each position shifted.\n\n   Example:\n   ```\n   5 \u003c\u003c 2\n   = 20\n   ```\n\n6. **Right Shift (`\u003e\u003e`):** This operator shifts the bits of a number to the right by a specified number of positions. It effectively divides the number by 2 for each position shifted.\n\n   Example:\n   ```\n   20 \u003e\u003e 2\n   = 5\n   ```\n\nBitwise operators are often used in scenarios such as:\n\n- Manipulating individual bits within a number.\n- Efficiently implementing data structures like bitsets and flags.\n- Performing optimizations in low-level code.\n- Interacting with hardware registers.\n- Encoding and decoding data in specific formats.\n\nUnderstanding bitwise operators is crucial when working on embedded systems, system-level programming, and tasks requiring precise control over data at the bit level.\n\n**Difference between ~ and ! of bitwise operation**\n\nThe `~` and `!` operators are both used in bitwise operations, but they serve different purposes and have different behaviors:\n\n1. `~` (Bitwise NOT Operator):\n   - `~` is the bitwise NOT operator, also known as the bitwise complement operator.\n   - It inverts each bit in its operand. If a bit is 0, it becomes 1, and if it's 1, it becomes 0.\n   - Example: If you apply `~` to the binary representation of 5 (which is `0101` in 4 bits), you get `-6` (which is `1010` in 4 bits in two's complement representation).\n   - It's used for bitwise negation.\n\n2. `!` (Logical NOT Operator):\n   - `!` is the logical NOT operator.\n   - It's used in boolean logic, not bitwise operations.\n   - It takes a boolean value (true or false) as an operand and returns the opposite boolean value.\n   - Example: `!true` returns `false`, and `!false` returns `true`.\n   - It's used for boolean negation, such as in conditional statements and logical expressions.\n\nIn summary, `~` is a bitwise operator used for inverting individual bits in a binary representation, while `!` is a logical operator used for boolean negation in conditional logic. They have different purposes and applications.\n\n## Some Puzzles on Bit Manipulation\n**Problem-1**\n\nbitAnd - x\u0026y using only ~ and |\n *   Example: bitAnd(6, 5) = 4\n *   Legal ops: ~ |\n *   Max ops: 8\n *   Rating: 1\n```C\nint bitAnd(int x, int y)\n{\n\n    int temp = ~(~x | ~y);\n    return temp;\n}\n```\n**Problem-2**\n\nbitOr - x|y using only ~ and \u0026\n *   Example: bitOr(6, 5) = 7\n *   Legal ops: ~ \u0026\n *   Max ops: 8\n *   Rating: 1\n```C\nint bitOr(int x, int y)\n{\n    int temp = ~(~x \u0026 ~y);\n    return 2;\n\n}\n```\n**Problem-3**\n\nisZero - returns 1 if x == 0, and 0 otherwise\n *   Examples: isZero(5) = 0, isZero(0) = 1\n *   Legal ops: ! ~ \u0026 ^ | + \u003c\u003c \u003e\u003e\n *   Max ops: 2\n *   Rating: 1\n```C\nint isZero(int x)\n{\n    return !x;\n}\n```\n**Problem-4**\n\nminusOne - return a value of -1\n *   Legal ops: ! ~ \u0026 ^ | + \u003c\u003c \u003e\u003e\n *   Max ops: 2\n *   Rating: 1\n```C\nint minusOne(void)\n{\n    return ~(1 \u003c\u003c 31);\n}\n```\n**Problem-5**\n\nTMax - return maximum two's complement integer\n *   Legal ops: ! ~ \u0026 ^ | + \u003c\u003c \u003e\u003e\n *   Max ops: 4\n *   Rating: 1\n```C\nint tmax(void)\n{\n    // Shift 1 to the left by 31 bits to set the sign bit to 0\n    // All other bits will be set to 1\n    return (1 \u003c\u003c 31) ^ ~(1 \u003c\u003c 31);\n}\n```\n**Problem-6**\n\nbitXor - x^y using only ~ and \u0026\n *   Example: bitXor(4, 5) = 1\n *   Legal ops: ~ \u0026\n *   Max ops: 14\n *   Rating: 2\n```C\nint bitXor(int x, int y) {\n    int not_x = ~x;\n    int not_y = ~y;\n    int and_x_y = x \u0026 y;\n    int and_not_x_not_y = not_x \u0026 not_y;\n    int result = ~(and_x_y \u0026 and_not_x_not_y);\n    return result;\n}\n```\n**Problem-7**\n\ngetByte - Extract byte n from word x\n *   Bytes numbered from 0 (LSB) to 3 (MSB)\n *   Examples: getByte(0x12345678,1) = 0x56\n *   Legal ops: ! ~ \u0026 ^ | + \u003c\u003c \u003e\u003e\n *   Max ops: 6\n *   Rating: 2\n```C\nint getByte(int x, int n)\n{\n    // Shift x right by 8 * n bits to get the desired byte in the least significant position\n    int shifted = x \u003e\u003e (n \u003c\u003c 3);\n\n    // Mask the result to keep only the least significant byte\n    int result = shifted \u0026 0xFF;\n\n    return result;\n}\n```\n**Problem-8**\n\nisEqual - return 1 if x == y, and 0 otherwise\n *   Examples: isEqual(5,5) = 1, isEqual(4,5) = 0\n *   Legal ops: ! ~ \u0026 ^ | + \u003c\u003c \u003e\u003e\n *   Max ops: 5\n *   Rating: 2\n```C\nint isEqual(int x, int y)\n{\n    // XOR x and y, if they are equal, the result will be 0\n    int result = !(x ^ y);\n\n    return result;\n}\n```\n**Problem-9**\n\nnegate - return -x\n *   Example: negate(1) = -1.\n *   Legal ops: ! ~ \u0026 ^ | + \u003c\u003c \u003e\u003e\n *   Max ops: 5\n *   Rating: 2\n```C\nint negate(int x)\n{\n    // Negate x by taking its two's complement\n    return (~x) + 1;\n}\n```\n**Problem-10**\n\nisPositive - return 1 if x \u003e 0, return 0 otherwise\n *   Example: isPositive(-1) = 0.\n *   Legal ops: ! ~ \u0026 ^ | + \u003c\u003c \u003e\u003e\n *   Max ops: 8\n *   Rating: 3\n\n```C\nint isPositive(int x)\n{\n    // Shift x 31 bits to the right to extract its sign bit\n    int sign = (x \u003e\u003e 31) \u0026 1;\n\n    // If x is greater than 0, sign will be 0, and we negate it to get 1\n    // If x is less than or equal to 0, sign will be 1, and we negate it to get 0\n    return !sign;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsk-azraf-sami%2Fbit-manipulation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsk-azraf-sami%2Fbit-manipulation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsk-azraf-sami%2Fbit-manipulation/lists"}