Write a program to display the no of bit difference between
any 2 given numbers
eg: Num1 will 12->1100
Num2 will 7->0111 the difference in bits are 2.

Answer Posted / vadivelt

Hi Small Bug is there in my previous post.
That is corrected in the code written below.

#include<stdio.h>
#include<conio.h>

main()
{
int count = 0, n, i, Res = 0;
int a, b;
printf("GIVE 2 INPUT NO IS\n");
scanf("%d %d", &a, &b);
n = sizeof(int) * 8;
for(i = 0; i<n; i++)
{
Res = ((a >> i & 0x01) ^ (b >> i & 0x01)) ? 1 : 0;
if(Res == 1)
count++;
}
printf("BIT(S) DIFFERENCE: %d", count);
getch();
}

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you mean by Recursion Function?

633


Define and explain about ! Operator?

616


printf(), scanf() these are a) library functions b) userdefined functions c) system functions d) they are not functions

637


How can I ensure that integer arithmetic doesnt overflow?

609


Does sprintf put null character?

604






What is masking?

638


What is function prototype in c with example?

580


Is null valid for pointers to functions?

614


please give me some tips for the placement in the TCS.

1634


What is the purpose of the preprocessor directive error?

683


What is string length in c?

615


What is the use of sizeof () in c?

559


What functions are used in dynamic memory allocation in c?

598


What is an lvalue in c?

697


What is the correct declaration of main?

679