{"id":22814364,"url":"https://github.com/devinterview-io/bit-manipulation-interview-questions","last_synced_at":"2026-02-13T12:14:45.164Z","repository":{"id":108757550,"uuid":"332394779","full_name":"Devinterview-io/bit-manipulation-interview-questions","owner":"Devinterview-io","description":"🟣 Bit Manipulation interview questions and answers to help you prepare for your next data structures and algorithms interview in 2025.","archived":false,"fork":false,"pushed_at":"2025-05-19T17:15:08.000Z","size":29,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-11T04:46:44.492Z","etag":null,"topics":["algorithms","algorithms-and-data-structures","algorithms-and-data-structures-interview-questions","algorithms-interview-questions","bit-manipulation","bit-manipulation-interview-questions","bit-manipulation-questions","bit-manipulation-tech-interview","coding-interview-questions","data-structures","data-structures-and-algorithms","data-structures-and-algorithms-interview-questions","data-structures-interview-questions","software-developer-interview","software-engineer-interview"],"latest_commit_sha":null,"homepage":"https://devinterview.io/","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Devinterview-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-01-24T07:51:31.000Z","updated_at":"2025-08-12T18:32:39.000Z","dependencies_parsed_at":"2025-05-19T17:58:46.320Z","dependency_job_id":null,"html_url":"https://github.com/Devinterview-io/bit-manipulation-interview-questions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Devinterview-io/bit-manipulation-interview-questions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Devinterview-io%2Fbit-manipulation-interview-questions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Devinterview-io%2Fbit-manipulation-interview-questions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Devinterview-io%2Fbit-manipulation-interview-questions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Devinterview-io%2Fbit-manipulation-interview-questions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Devinterview-io","download_url":"https://codeload.github.com/Devinterview-io/bit-manipulation-interview-questions/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Devinterview-io%2Fbit-manipulation-interview-questions/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29405194,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-13T06:24:03.484Z","status":"ssl_error","status_checked_at":"2026-02-13T06:23:12.830Z","response_time":78,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["algorithms","algorithms-and-data-structures","algorithms-and-data-structures-interview-questions","algorithms-interview-questions","bit-manipulation","bit-manipulation-interview-questions","bit-manipulation-questions","bit-manipulation-tech-interview","coding-interview-questions","data-structures","data-structures-and-algorithms","data-structures-and-algorithms-interview-questions","data-structures-interview-questions","software-developer-interview","software-engineer-interview"],"created_at":"2024-12-12T13:08:22.147Z","updated_at":"2026-02-13T12:14:45.158Z","avatar_url":"https://github.com/Devinterview-io.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# ⚫ Bit Manipulation in Tech Interviews: 10 Must-Know Questions \u0026 Answers in 2026\n\n**Bit Manipulation** involves directly manipulating individual bits of data, usually using bitwise operations. In coding interviews, bit manipulation problems are often presented to evaluate a candidate's proficiency with **low-level operations** and their ability to think in terms of **binary representations**.\n\nCheck out our carefully selected list of **basic** and **advanced** Bit Manipulation questions and answers to be well-prepared for your tech interviews in 2025.\n\n![Bit Manipulation Decorative Image](https://firebasestorage.googleapis.com/v0/b/dev-stack-app.appspot.com/o/blogImg%2FbitManipulation.png?alt=media\u0026token=4b3db4f6-d4bd-4f56-8f1f-f0d682f6a28d\u0026_gl=1*1euky85*_ga*OTYzMjY5NTkwLjE2ODg4NDM4Njg.*_ga_CW55HF8NVT*MTY5ODYwNTk1NS4xOTAuMS4xNjk4NjA3MzM2LjE5LjAuMA..)\n\n👉🏼 You can also find all answers here: [Devinterview.io - Bit Manipulation](https://devinterview.io/data/bitManipulation-interview-questions)\n\n---\n\n## 🔹 1. What is _Bit_?\n\n### Answer\n\nThe term **\"bit\"** stems from \"binary\" and \"digit.\" As the basic unit of information in computing and digital communications, a bit can assume one of two values: 0 or 1.\n\n### Binary System vs Decimal System\n\nComputers operate using a **binary number system**, employing just two numerals: 0 and 1. In contrast, our day-to-day decimal system is **base-10**, utilizing ten numerals (0-9).\n\nIn the binary system:\n- **Bit**: Represents 0 or 1\n- **Nibble**: Comprises 4 bits, representing 16 values (0-15 in decimal)\n- **Byte**: Contains 8 bits, representing 256 values (0-255 in decimal)\n\nFor instance, the decimal number 5 is depicted as $0101_2$ in binary.\n\n### Bit Manipulation\n\nBits are pivotal in **bit manipulation**, a field encompassing operations like bit shifting, logical operations (AND, OR, XOR), and bit masking. These techniques find applications in data compression, encryption, and device control.\n\nConsidering two 8-bit numbers: $0010\\,1010$ and $0000\\,1100$. The logical AND operation gives:\n\n$$\n\\begin{array}{c c c c c c c c c}\n  \u0026 0 \u0026 0 \u0026 1 \u0026 0 \u0026 1 \u0026 0 \u0026 1 \u0026 0 \\\\\n\\text{AND} \u0026 0 \u0026 0 \u0026 0 \u0026 0 \u0026 1 \u0026 1 \u0026 0 \u0026 0 \\\\\n\\hline\n  \u0026 0 \u0026 0 \u0026 0 \u0026 0 \u0026 1 \u0026 0 \u0026 0 \u0026 0 \\\\\n\\end{array}\n$$\n\n### Integer Representation in Bits\n\nAn integer's representation typically occupies a **fixed** number of bits. On many systems, an integer uses **32 bits**. Thus, a 32-bit signed integer spans $-2^{31}$ to $2^{31} - 1$.\n\n### Hardware Considerations\n\nAlthough **bits** underpin computing, hardware designs can constrain their usage. A 32-bit CPU processes 32 bits simultaneously, requiring extra steps for larger numbers. This led to the adoption of \"**double words**\" and \"**quad words**\" to represent larger integers.\n\n---\n\n## 🔹 2. What is a _Byte_?\n\n### Answer\n\nA **byte** is a foundational data unit in computing and telecommunications, capable of representing 256 unique values, ranging from 0 to 255. It consists of 8 **bits**, the smallest data storage units, which can be either 0 or 1.\n\n### Bit Composition\n\nEach bit in a byte has a place value, starting from the least significant bit (LSB) on the right to the most significant bit (MSB) on the left. Their place values are: \n\n| Place Value | Bit Position |\n|-------------|--------------|\n| 128         | 7            |\n| 64          | 6            |\n| 32          | 5            |\n| 16          | 4            |\n| 8           | 3            |\n| 4           | 2            |\n| 2           | 1            |\n| 1           | 0            |\n\n\nSetting all bits to 1 yields the byte's maximum value of 255.\n\n### Converting Bytes to Decimal\n\nTo find the decimal equivalent of a byte, sum the place values of bits set to 1. For a byte with all bits as 1:\n\n$$\n1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255\n$$\n\n### Code Example: Byte to Decimal Conversion\n\nHere is the Python code:\n\n```python\ndef byte_to_decimal(byte_str):\n    # Reverse the string for right-to-left calculation\n    byte_str = byte_str[::-1]\n    \n    # Sum up place values for bits set to 1\n    return sum(int(byte_str[i]) * 2 ** i for i in range(len(byte_str)))\n\n# Example usage\nbyte_value = \"11111111\"  # All bits are 1\ndecimal_value = byte_to_decimal(byte_value)\nprint(decimal_value)  # Output: 255\n```\n\n---\n\n## 🔹 3. Explain what is a _Bitwise Operation_.\n\n### Answer\n\n**Bitwise operations** are actions applied to individual bits within binary numbers or data units like integers. These operations offer several advantages, including speed and memory efficiency, and are widely used in specific computing scenarios.\n\n### Why Use Bitwise Operations?\n\n- **Speed**: Executing bitwise operations is often faster than using standard arithmetic or logical operations.\n  \n- **Memory Efficiency**: Operating at the bit level allows the storage of multiple flags or small integers within a single data unit, optimizing memory usage.\n\n- **Low-Level Programming**: These operations are crucial in embedded systems and microcontroller programming.\n\n- **Data Manipulation**: Bitwise operations can selectively alter or extract specific bits from a data unit.\n\n### Types of Operators\n\n#### Logical Operators\n\n1. **AND (`\u0026`)**: Yields `1` if corresponding bits are both `1`; otherwise, `0`.\n   - Example: $5$ \u0026 $3 = 1$.\n  \n2. **OR (`|`)**: Yields `1` if one or both corresponding bits are `1`; otherwise, `0`.\n    - Example: $5 | 3 = 7$.\n\n3. **XOR (`^`)**: Yields `1` when corresponding bits differ; otherwise, `0`.\n    - Example: $5 \\oplus 3 = 6$.\n\n4. **NOT (`~`)**: Inverts all bits.\n    - Example: $~5$ becomes $-6$ in 2's complement.\n\n#### Shift Operators\n\n1. **Left Shift (`\u003c\u003c`)**: Moves bits to the left and fills in with `0`.\n    - Example: $5 \\text{ \u003c\u003c 2 } = 20$.\n\n2. **Right Shift (`\u003e\u003e`)**: Moves bits to the right and fills in based on the sign of the number.\n    - Example: $5 \\text{ \u003e\u003e 2 } = 1$.\n\n3. **Zero Fill Right Shift (\u003e\u003e\u003e)**: Shifts bits right, filling in zeros.\n    - Example: $-5 \\text{ \u003e\u003e\u003e 2 } = 1073741823$.\n    \n#### Specialized Operations\n\n- **Ones' Complement**: Similar to NOT but restricted to 32 bits.\n    - Example: $(-5)$ \u0026 $0xFFFFFFFF$.\n\n- **Twos' Complement**: Used for representing negative numbers in two's complement arithmetic.\n    - Example: $~5 + 1 = -6$.\n\n### Practical Applications\n\n- **Flag Management**: Bits can represent on/off flags, and bitwise operators can be used to toggle these.\n\n- **Data Compression**: These operations play a role in compression algorithms.\n  \n- **Encryption**: Bitwise manipulation is used in cryptographic algorithms.\n\n### Code Example: Flag Manipulation\n\nHere is the Python code:\n\n```python\n# Define flags with binary representation\nFLAG_A, FLAG_B, FLAG_C, FLAG_D = 0b0001, 0b0010, 0b0100, 0b1000\n\n# Set flags B and D\nflags = FLAG_B | FLAG_D\n\n# Check if Flag C is set\nprint(\"Flag C is set\" if flags \u0026 FLAG_C else \"Flag C is not set\")\n```\n\n---\n\n## 🔹 4. What are some real world applications of _Bitwise Operators_?\n\n### Answer\n\n**Bitwise operators** play a critical role across various domains, from data storage to algorithms and security.\n\nHere's an overview of their versatile applications:\n\n### Data Management\n\n- **Compact Storage with Bit Fields**: Efficiently store multiple Boolean values or flags, commonly seen in settings like user permissions or system flags.\n\n- **Data Compression**: Bitwise operations facilitate algorithms like Run-Length Encoding.\n\n- **Error Detection**: Tools like Parity Bits and CRC utilize bitwise logic to detect and correct errors in data transmission.\n\n### Algorithms\n\n- **Efficiency in Computation**: Fast multiplication or division by powers of two using left and right shift operations.\n\n- **Solving Puzzles**: Techniques like 'finding the single non-duplicate in an array' lean on XOR properties.\n\n- **Subset Enumeration**: Bit manipulations can generate all subsets of a set, useful in combinatorial problems.\n\n### Graphics and Imaging\n\n- **Bitmaps**: Representing images with a sequence of bits, enabling pixel-level operations.\n\n- **Color Manipulation**: Use bitwise logic for operations like blending or masking colors.\n\n### Security\n\n- **Encryption Algorithms**: DES, AES, and others deploy bitwise functions in their operations.\n\n- **Quick Membership Checks**: Bloom filters, used in databases and caches, harness bitwise logic for fast set membership tests.\n\n### Hardware Control\n\n- **Device Register Manipulation**: Directly control device hardware by setting or clearing specific bits in memory-mapped IO.\n\n- **Memory Management**: Techniques like bit masking can assist in memory partitioning in older systems.\n\n### Networking\n\n- **IP Address Operations**: IP subnetting and CIDR notations, essential for routing, involve bitwise manipulations.\n\n---\n## 🔹 5. Explain how _XOR (^)_ bit operator works.\n\n### Answer\n\n👉🏼 Check out all 10 answers here: [Devinterview.io - Bit Manipulation](https://devinterview.io/data/bitManipulation-interview-questions)\n\n---\n\n## 🔹 6. What is difference between _\u003e\u003e_ and _\u003e\u003e\u003e_ operators?\n\n### Answer\n\n👉🏼 Check out all 10 answers here: [Devinterview.io - Bit Manipulation](https://devinterview.io/data/bitManipulation-interview-questions)\n\n---\n\n## 🔹 7. What is _Bit Masking_?\n\n### Answer\n\n👉🏼 Check out all 10 answers here: [Devinterview.io - Bit Manipulation](https://devinterview.io/data/bitManipulation-interview-questions)\n\n---\n\n## 🔹 8. What would the _number 22_ look like as a _Byte_?\n\n### Answer\n\n👉🏼 Check out all 10 answers here: [Devinterview.io - Bit Manipulation](https://devinterview.io/data/bitManipulation-interview-questions)\n\n---\n\n## 🔹 9. How to flip all _Bits_ in an _Integer_?\n\n### Answer\n\n👉🏼 Check out all 10 answers here: [Devinterview.io - Bit Manipulation](https://devinterview.io/data/bitManipulation-interview-questions)\n\n---\n\n## 🔹 10. Flip _k_ least significant _Bits_ in an _Integer_.\n\n### Answer\n\n👉🏼 Check out all 10 answers here: [Devinterview.io - Bit Manipulation](https://devinterview.io/data/bitManipulation-interview-questions)\n\n---\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevinterview-io%2Fbit-manipulation-interview-questions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevinterview-io%2Fbit-manipulation-interview-questions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevinterview-io%2Fbit-manipulation-interview-questions/lists"}