The right decision would be to make
var bit = Math.Log(a ^ b, 2);
Although, of course, this leaves open the question of what will happen if, for whatever reason, more than one bit is different.
you can use
var bit = (int)Math.Log(a ^ b, 2);
to get the index of the highest different bits, if slightly different.
Caution: For correctness, any such function should also verify that the two arguments a and b are actually different before attempting to provide a result. Otherwise, you will get either a meaningless result, or just an exception. This applies to all solutions presented here, including this one.
source share