Compare Bits in C

This article will explain several methods of how to compare bits in C.

Implement a Custom Function using Bitwise XOR and AND Operations for Bit Comparison in C

Generally, bit comparison entails accessing single bit values and conducting needed operations, such as implementing bit-field using union and struct keywords. Although, bitwise operations offer a more efficient method of comparing specified bits in numbers. In this case, we implement a separate function suited for the u_int32_t type, which is guaranteed to have 32-bit width.

The example program takes three integers as command-line arguments, the first two of which are the numbers that are compared, while the third integer specifies the n-th bit. Notice that we convert argv elements using strtol, thus losing some precision when storing the return values in u_int32_t type. The compareBits function declares two local variables storing intermediate values - mask and tmp. We set the n-th bit in the mask by shifting the 1 left by nth - 1 positions. Then, both user input numbers are XOR-ed to get the bit differences in each position with a set bit in result denoting different values. Finally, we need to extract the bit from the n-th position and check if the value is 0.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool compareBits(u_int32_t n1, u_int32_t n2, u_int32_t nth) {
    u_int32_t mask, tmp;

    mask = 1 << (nth - 1);
    tmp = n1 ^ n2;

    if ((tmp & mask) == 0)
        return true;
    else
        return false;
}

int main(int argc, char *argv[]) {
    u_int32_t num1, num2, bit;

    if (argc != 4) {
        fprintf(stderr, "Usage: %s integer1 integer2 nth_bit \n", argv[0]);
        exit(EXIT_FAILURE);
    }

    num1 = strtol(argv[1], NULL, 0);
    num2 = strtol(argv[2], NULL, 0);
    bit = strtol(argv[3], NULL, 0);

    compareBits(num1, num2, bit) ? printf("bits equal!\n") : printf("bits not equal!\n");

    exit(EXIT_SUCCESS);
}

sample File Format:

./program 1234 1231 1

Input File Format:

bits not equal!

As an example, the integers 1234 and 1231 are represented in binary as 00000000000000000000010011010010 and 00000000000000000000010011001111, respectively. Thus, XOR-ing these two result in 00000000000000000000000000011101 binary representation, which is finally AND-ed with the mask 00000000000000000000000000000010 to extract the single bit position value. If the result is all zeros, it implies that compared bits are equal; otherwise, it is the opposite.

Alternatively, though, we can shorten the compareBits function and move XOR/AND operations to the return statement as shown in the following example code. Note that the function returns the logical opposite of bitwise XOR/AND operations, as we output the corresponding messages in the caller function using the ? : tenary statement.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool compareBits(u_int32_t n1, u_int32_t n2, u_int32_t nth) {
    u_int32_t mask;

    mask = 1 << (nth - 1);

    return !((n1 ^ n2) & mask);
}

int main(int argc, char *argv[]) {
    u_int32_t num1, num2, bit;

    if (argc != 4) {
        fprintf(stderr, "Usage: %s integer1 integer2 nth_bit \n", argv[0]);
        exit(EXIT_FAILURE);
    }

    num1 = strtol(argv[1], NULL, 0);
    num2 = strtol(argv[2], NULL, 0);
    bit = strtol(argv[3], NULL, 0);

    compareBits(num1, num2, bit) ? printf("bits equal!\n") : printf("bits not equal!\n");

    exit(EXIT_SUCCESS);
}

sample File Format:

./program 1234 1231 2

Input File Format:

bits equal!
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.