0

Im trying to find the number of bits common between two binary numbers starting from MSB -> LSB. For example, I'm taking a set of binary numbers of 4 bits each i.e., 0 -> 15.

  • I'm trying to find the number of common bits between 8(1000) and 12(1100). Starting from msb we see that there is only one common bit. hence answer should be (1)
  • 0(0000) and 15(1111) = 0
  • 10(1010) and 11(1011) = 3
  • 6(0110) and 8(1000) = 0

Is there a formula to do this? Or should it be brute forced by checking each bit when writing code?

RobPratt
  • 45,619
  • What are msb and lsb? And if you're using a computer, look for "bitwise xor". It will get you part of the way. – Arthur Sep 04 '18 at 06:04
  • 1
    (Bitwise xnor is actually more correct, sorry about that. I would think you find information on both in more or less the same place.) – Arthur Sep 04 '18 at 06:11
  • @Arthur I believe it is "most significant bit" and "least significant bit". – GoodDeeds Sep 04 '18 at 06:11
  • @postmortes MSB is the left most digit and LSB is the right most digit – Bhargav Sep 04 '18 at 06:32
  • @Arthur Thanks for pointing this out. But do I still have to look for every bit which is '1' after doing xnor. Taking 10(1010) and 11(1011). XNOR(1010,1011) gives 1110. – Bhargav Sep 04 '18 at 06:36
  • @Bhargav Yes, from there you have to count the 1's. As I said, it will get you part of the way. – Arthur Sep 04 '18 at 06:52

0 Answers0