I wrote some code I am unit testing right now and for that I made the code below with the help of the internet.
//shows changed bit
//0010 0101 XOR 0010 0111 = 0000 0010
uint8_t XOR = value ^ result;
int count;
//count amount of 1's in XOR
for (count = 0; XOR; XOR >>= 1)
{
count += XOR & 1;
}
Value is the original value, and result is the value but with ONE bit toggled.
I just want to make sure I understand this correctly but please correct me if I am wrong.
The 2nd statement in the for loop the "XOR;" what I think it does is run the for loop while XOR is not 0. And the XOR >>= 1 is that it sets to its self but with the 1 shifted one place to the right.
And the count does a +1 whenever there is a 1 on the last position ?
That's what I make out of it.