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.
Check out our carefully selected list of basic and advanced Bit Manipulation questions and answers to be well-prepared for your tech interviews in 2025.
ππΌ You can also find all answers here: Devinterview.io - Bit Manipulation
The 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.
Computers 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).
In the binary system:
- Bit: Represents 0 or 1
- Nibble: Comprises 4 bits, representing 16 values (0-15 in decimal)
- Byte: Contains 8 bits, representing 256 values (0-255 in decimal)
For instance, the decimal number 5 is depicted as
Bits 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.
Considering two 8-bit numbers:
An 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
Although 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.
A 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.
Each 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:
Place Value | Bit Position |
---|---|
128 | 7 |
64 | 6 |
32 | 5 |
16 | 4 |
8 | 3 |
4 | 2 |
2 | 1 |
1 | 0 |
Setting all bits to 1 yields the byte's maximum value of 255.
To find the decimal equivalent of a byte, sum the place values of bits set to 1. For a byte with all bits as 1:
Here is the Python code:
def byte_to_decimal(byte_str):
# Reverse the string for right-to-left calculation
byte_str = byte_str[::-1]
# Sum up place values for bits set to 1
return sum(int(byte_str[i]) * 2 ** i for i in range(len(byte_str)))
# Example usage
byte_value = "11111111" # All bits are 1
decimal_value = byte_to_decimal(byte_value)
print(decimal_value) # Output: 255
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.
-
Speed: Executing bitwise operations is often faster than using standard arithmetic or logical operations.
-
Memory Efficiency: Operating at the bit level allows the storage of multiple flags or small integers within a single data unit, optimizing memory usage.
-
Low-Level Programming: These operations are crucial in embedded systems and microcontroller programming.
-
Data Manipulation: Bitwise operations can selectively alter or extract specific bits from a data unit.
-
AND (
&
): Yields1
if corresponding bits are both1
; otherwise,0
.- Example:
$5$ &$3 = 1$ .
- Example:
-
OR (
|
): Yields1
if one or both corresponding bits are1
; otherwise,0
.- Example:
$5 | 3 = 7$ .
- Example:
-
XOR (
^
): Yields1
when corresponding bits differ; otherwise,0
.- Example:
$5 \oplus 3 = 6$ .
- Example:
-
NOT (
~
): Inverts all bits.- Example:
$~5$ becomes$-6$ in 2's complement.
- Example:
-
Left Shift (
<<
): Moves bits to the left and fills in with0
.- Example:
$5 \text{ << 2 } = 20$ .
- Example:
-
Right Shift (
>>
): Moves bits to the right and fills in based on the sign of the number.- Example:
$5 \text{ >> 2 } = 1$ .
- Example:
-
Zero Fill Right Shift (>>>): Shifts bits right, filling in zeros.
- Example:
$-5 \text{ >>> 2 } = 1073741823$ .
- Example:
-
Ones' Complement: Similar to NOT but restricted to 32 bits.
- Example:
$(-5)$ &$0xFFFFFFFF$ .
- Example:
-
Twos' Complement: Used for representing negative numbers in two's complement arithmetic.
- Example:
$~5 + 1 = -6$ .
- Example:
-
Flag Management: Bits can represent on/off flags, and bitwise operators can be used to toggle these.
-
Data Compression: These operations play a role in compression algorithms.
-
Encryption: Bitwise manipulation is used in cryptographic algorithms.
Here is the Python code:
# Define flags with binary representation
FLAG_A, FLAG_B, FLAG_C, FLAG_D = 0b0001, 0b0010, 0b0100, 0b1000
# Set flags B and D
flags = FLAG_B | FLAG_D
# Check if Flag C is set
print("Flag C is set" if flags & FLAG_C else "Flag C is not set")
Bitwise operators play a critical role across various domains, from data storage to algorithms and security.
Here's an overview of their versatile applications:
-
Compact Storage with Bit Fields: Efficiently store multiple Boolean values or flags, commonly seen in settings like user permissions or system flags.
-
Data Compression: Bitwise operations facilitate algorithms like Run-Length Encoding.
-
Error Detection: Tools like Parity Bits and CRC utilize bitwise logic to detect and correct errors in data transmission.
-
Efficiency in Computation: Fast multiplication or division by powers of two using left and right shift operations.
-
Solving Puzzles: Techniques like 'finding the single non-duplicate in an array' lean on XOR properties.
-
Subset Enumeration: Bit manipulations can generate all subsets of a set, useful in combinatorial problems.
-
Bitmaps: Representing images with a sequence of bits, enabling pixel-level operations.
-
Color Manipulation: Use bitwise logic for operations like blending or masking colors.
-
Encryption Algorithms: DES, AES, and others deploy bitwise functions in their operations.
-
Quick Membership Checks: Bloom filters, used in databases and caches, harness bitwise logic for fast set membership tests.
-
Device Register Manipulation: Directly control device hardware by setting or clearing specific bits in memory-mapped IO.
-
Memory Management: Techniques like bit masking can assist in memory partitioning in older systems.
- IP Address Operations: IP subnetting and CIDR notations, essential for routing, involve bitwise manipulations.
ππΌ Check out all 10 answers here: Devinterview.io - Bit Manipulation
ππΌ Check out all 10 answers here: Devinterview.io - Bit Manipulation
ππΌ Check out all 10 answers here: Devinterview.io - Bit Manipulation
ππΌ Check out all 10 answers here: Devinterview.io - Bit Manipulation
ππΌ Check out all 10 answers here: Devinterview.io - Bit Manipulation
ππΌ Check out all 10 answers here: Devinterview.io - Bit Manipulation