1

I want to write a program to get the number of 1 bits in comparing two numbers. I want compare the bits between any two numbers to find where the binary numbers are different in the 1's and 0's. In other words "Exclusive OR" (XOR) relationship.

Like if 22 (which has 10110 binary) and compare it with 15 (which has 01111 binary)

The first one is 10110. The second one is01111.

The result: 11001.

And the answer would be 25 but what I want to get is 3 where there is three 1s and 0s that are different.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Sagun Panthi
  • 47
  • 1
  • 2
  • 1
    Take a look at [Bitwise operator](https://en.wikipedia.org/wiki/Bitwise_operations_in_C), try to implement something and, if you will have problems, post it an we will give you a hand.. – LPs Jan 15 '16 at 15:08
  • Take your pick of any number of bit counting algorithms: http://stackoverflow.com/questions/109023/how-to-count-the-number-of-set-bits-in-a-32-bit-integer – Michael Burr Jan 15 '16 at 15:21
  • And a reference for a whole bunch of bit twiddling hacks (including counting bits): https://graphics.stanford.edu/~seander/bithacks.html – Michael Burr Jan 15 '16 at 15:24
  • 'int count_bit(unsigned int x, unsigned int y){ int x1[31], y1[31]; unsigned int x_count = sizeof(x) * 8 - 1; /*x is of "unsigned int" type*/ unsigned int y_count = sizeof(y) * 8 -1; for (int i = 0; i<=x_count; i++){ x1[i] = x << i;} for (int j = 0; j<= y_count;j++){ y1[j] = y << j;} while( (i<= x_count) && (j<= y_count)){ if( x1[i] != y1[j]){ count++; i++; j++; return count;} else return 0; }' – Sagun Panthi Jan 15 '16 at 16:16

2 Answers2

5

To find the bits that are different, you need to XOR the values:

unsigned int first  = 22;
unsigned int second = 15;
unsigned int result = first ^ second; // only the bits that are different
                                      // will be set to 1 in result

To count the 1-bits in result you can use:

unsigned int CountBits(unsigned int n) {

   unsigned int count = 0;
   while(n) {
       count += n & 0x01; // checks the least significant bit of n
                          // if the bit is 1, count is incremented
       n >>= 1; // shift all bits of n one to the right
                // if no 1 bits are left, n becomes 0 and the loop ends
   }
   return count;
}

unsigned int count = CountBits(result);

To do it in one step:

unsigned int count = CountBits(first ^ second);

On some systems, the POPCNT instruction could be used instead.


Update - Full example:

#include <stdio.h>

unsigned int CountBits(unsigned int n) {

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

int main(void) {

    unsigned int first  = 22;
    unsigned int second = 15;
    unsigned int result = first ^ second;

    unsigned int count = CountBits(result);

    printf("result: %u - count: %u\n", result, count);

    return 0;
}

Prints:

result: 25 - count: 3

Or, with an extra function:

#include <stdio.h>

unsigned int CountBits(unsigned int n) {

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

unsigned int CountDifferentBits(unsigned int n1, unsigned int n2) {

    return CountBits(n1 ^ n2);
}

int main(void) {

    unsigned int count = CountDifferentBits(22, 15);

    printf("Different bits count: %u\n", count);

    return 0;
}
Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • yeah i know for getting 1 bits. i have to compare two number looking at their bits in respective positions.. suppose if two numbers are same then their bits are also same..have to return zero in that case.. – Sagun Panthi Jan 15 '16 at 16:25
  • @SagunPanthi - Yes, as you said in you question, you need to `XOR` them: `int count = CountBits(first ^ second);` will give you the bitcount of the XOR-ed values (and return 0 if they are the same). (`^` is the XOR operator) – Danny_ds Jan 15 '16 at 16:27
  • @SagunPanthi - See my update for a full example. – Danny_ds Jan 15 '16 at 16:45
  • @Danny_ds For fun, here's an alternate version of the `while` loop in your `CountBits` function: `while (n) { count++; n &= n - 1; }`. – Ian Abbott Jan 15 '16 at 18:10
0
int count_bit(unsigned int x, unsigned int y){
int x1[31], y1[31];

unsigned int x_count = sizeof(x) * 8 - 1; /*x is of "unsigned int" type*/
unsigned int y_count = sizeof(y) * 8 -1;

for (int i = 0; i<=x_count; i++){
    x1[i] = x << i;}
for (int j = 0; j<= y_count;j++){
    y1[j] = y << j;}

while( (i<= x_count) && (j<= y_count)){
    if( x1[i] != y1[j]){
        count++;
        i++;
        j++;
        return count;}
    else
        return 0;
}
LPs
  • 16,045
  • 8
  • 30
  • 61
Sagun Panthi
  • 47
  • 1
  • 2