Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0857a8b

Browse files
implemented program to count bits to flip
1 parent 57aaa4b commit 0857a8b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

‎Bit Manipulation/CountBitsToFlipToconvertAtoB.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,27 @@
44
using namespace std;
55

66
//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+
}
724

825
int main()
926
{
27+
cout<<CountFlip(7,10);
1028

1129
return 0;
1230
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /