We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 57aaa4b commit 0857a8bCopy full SHA for 0857a8b
Bit Manipulation/CountBitsToFlipToconvertAtoB.cpp
@@ -4,9 +4,27 @@
4
using namespace std;
5
6
//count number of bits to be flipped in a to convert it to b
7
+int CountFlip(int a ,int b)
8
+{
9
+ int XOR = a^b; //XOR of a and b will be 1 whereever a and b bits are different
10
+
11
12
+ int count= 0 ;
13
+ while(XOR)
14
+ {
15
+ //counting number of set bits which is equal to number of bits to flip in a
16
+ if(XOR & 1) count++;
17
+ XOR /= 2;
18
19
20
+ }
21
22
+ return count;
23
+}
24
25
int main()
26
{
27
+ cout<<CountFlip(7,10);
28
29
return 0;
30
}
0 commit comments