Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated code #2922

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ehtashammd07
Copy link

Bit manipulation : Count number of bits to be flipped to convert A to B

Fixes #2921

Approach
Use XOR (^) to Identify Differing Bits

The XOR operation returns 1 when bits are different and 0 when they are the same.
xor_value = start ^ goal gives a number where 1s represent the bits that need to be flipped.
Count the Number of 1s in xor_value

We count the number of 1s in the binary representation of xor_value.
This is done using xor_value & 1 (to check the last bit) and xor_value >>= 1 (to shift right and check the next bit).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[OTHER] Bit manipulation : Count number of bits to be flipped to convert A to B
1 participant