Interview

10 Bit Manipulation in C Interview Questions and Answers

Prepare for technical interviews with this guide on bit manipulation in C. Enhance your skills with curated questions and answers.

Bit manipulation in C is a fundamental skill for optimizing performance and resource usage in low-level programming. It involves using bitwise operators to directly manipulate individual bits within data types, enabling efficient data processing and memory management. Mastery of bit manipulation is crucial for tasks such as embedded systems programming, cryptography, and performance-critical applications.

This article provides a curated selection of bit manipulation questions and answers to help you prepare for technical interviews. By understanding these concepts and practicing the provided examples, you will enhance your problem-solving abilities and demonstrate your proficiency in handling complex, low-level programming challenges.

Bit Manipulation in C Interview Questions and Answers

1. Write a function to perform bitwise AND, OR, and XOR operations on two integers.

Bitwise operations allow manipulation of individual bits in data. In C, the &, |, and ^ operators perform AND, OR, and XOR operations, respectively. These operations are essential in low-level programming, such as systems programming and hardware interfacing.

Here’s a function in C that performs these operations on two integers:

#include <stdio.h>

void bitwise_operations(int a, int b) {
    int and_result = a & b;
    int or_result = a | b;
    int xor_result = a ^ b;

    printf("AND: %d\n", and_result);
    printf("OR: %d\n", or_result);
    printf("XOR: %d\n", xor_result);
}

int main() {
    int a = 5; // 0101 in binary
    int b = 3; // 0011 in binary

    bitwise_operations(a, b);

    return 0;
}

2. Write a function to count the number of set bits (1s) in an integer.

To count the number of set bits (1s) in an integer, use bitwise operations to repeatedly check the least significant bit and shift the number right until all bits are checked.

Example:

#include <stdio.h>

int countSetBits(int n) {
    int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}

int main() {
    int num = 29; // Binary: 11101
    printf("Number of set bits: %d\n", countSetBits(num));
    return 0;
}

3. Implement a function to check if a given integer is a power of two.

To check if an integer is a power of two, leverage the property that a power of two has exactly one bit set. A number n is a power of two if n > 0 and (n & (n - 1)) == 0.

Here’s a concise implementation in C:

#include <stdbool.h>

bool isPowerOfTwo(int n) {
    return n > 0 && (n & (n - 1)) == 0;
}

4. Implement a function to isolate the rightmost set bit of an integer.

To isolate the rightmost set bit of an integer, use a bitwise AND operation between the integer and its two’s complement. This operation leaves only the rightmost set bit as 1.

Example:

#include <stdio.h>

int isolate_rightmost_set_bit(int n) {
    return n & -n;
}

int main() {
    int n = 18; // Binary: 10010
    printf("Rightmost set bit: %d\n", isolate_rightmost_set_bit(n)); // Output: 2
    return 0;
}

5. Write a function to reverse the bits of an integer.

Reversing the bits of an integer involves flipping the order of the bits. This can be achieved using bitwise operations.

Here’s a function to reverse the bits of an integer in C:

unsigned int reverseBits(unsigned int n) {
    unsigned int reversed = 0;
    int bitCount = sizeof(n) * 8; // Total number of bits in the integer

    for (int i = 0; i < bitCount; i++) {
        if (n & (1 << i)) {
            reversed |= 1 << ((bitCount - 1) - i);
        }
    }

    return reversed;
}

6. Write a function to toggle specific bits in an integer.

Toggling specific bits in an integer means flipping the bits from 0 to 1 or from 1 to 0. This can be achieved using the XOR (^) operator.

Example:

#include <stdio.h>

int toggleBits(int num, int mask) {
    return num ^ mask;
}

int main() {
    int num = 29; // 00011101 in binary
    int mask = 5; // 00000101 in binary
    int result = toggleBits(num, mask);
    printf("Result: %d\n", result); // Output will be 24 (00011000 in binary)
    return 0;
}

7. Implement a function to determine the parity (even or odd) of the number of set bits in an integer.

To determine the parity of the number of set bits in an integer, count the number of 1s and check if this count is even or odd.

Here’s a simple implementation in C:

#include <stdio.h>

int parity(unsigned int n) {
    int count = 0;
    while (n) {
        count ^= (n & 1);
        n >>= 1;
    }
    return count;
}

int main() {
    unsigned int num = 7; // Binary: 111
    printf("Parity of %u is %s\n", num, parity(num) ? "odd" : "even");
    return 0;
}

8. Write a function to find the next higher number with the same number of set bits as a given integer.

To find the next higher number with the same number of set bits as a given integer, manipulate the bits by identifying the rightmost set bit, flipping it, and rearranging the bits.

Here’s a concise implementation in C:

unsigned int nextHigherWithSameBits(unsigned int n) {
    unsigned int c = n;
    unsigned int c0 = 0;
    unsigned int c1 = 0;

    while ((c & 1) == 0 && c != 0) {
        c0++;
        c >>= 1;
    }

    while ((c & 1) == 1) {
        c1++;
        c >>= 1;
    }

    if (c0 + c1 == 31 || c0 + c1 == 0) {
        return -1;
    }

    unsigned int pos = c0 + c1;
    n |= (1 << pos);
    n &= ~((1 << pos) - 1);
    n |= (1 << (c1 - 1)) - 1;

    return n;
}

9. Write a function to find the longest sequence of consecutive 1s in the binary representation of an integer.

To find the longest sequence of consecutive 1s in the binary representation of an integer, iterate through the bits and track the current and maximum sequences of 1s.

Here’s a concise implementation in C:

#include <stdio.h>

int longestConsecutiveOnes(int n) {
    int maxCount = 0;
    int currentCount = 0;

    while (n != 0) {
        if (n & 1) {
            currentCount++;
            if (currentCount > maxCount) {
                maxCount = currentCount;
            }
        } else {
            currentCount = 0;
        }
        n = n >> 1;
    }

    return maxCount;
}

int main() {
    int n = 29; // Binary: 11101
    printf("Longest sequence of consecutive 1s: %d\n", longestConsecutiveOnes(n));
    return 0;
}

10. Write a function to rotate bits of an integer left or right.

Bit rotation involves shifting the bits of an integer to the left or right, with the bits that fall off being reintroduced at the opposite end.

Here’s a function to rotate bits of an integer in C:

#include <stdio.h>

unsigned int rotate_left(unsigned int n, unsigned int d) {
    return (n << d) | (n >> (32 - d));
}

unsigned int rotate_right(unsigned int n, unsigned int d) {
    return (n >> d) | (n << (32 - d));
}

int main() {
    unsigned int n = 16; // Example number
    unsigned int d = 2;  // Number of positions to rotate

    printf("Left Rotation of %u by %u is %u\n", n, d, rotate_left(n, d));
    printf("Right Rotation of %u by %u is %u\n", n, d, rotate_right(n, d));

    return 0;
}
Previous

15 Business Analytics Interview Questions and Answers

Back to Interview
Next

15 SAP ABAP HANA Interview Questions and Answers