How to find the number of msb bits common between two binary numbers

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












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?










share|cite|improve this question





















  • 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 4 at 6: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 4 at 6:11











  • @Arthur I believe it is "most significant bit" and "least significant bit".
    – GoodDeeds
    Sep 4 at 6:11











  • @postmortes MSB is the left most digit and LSB is the right most digit
    – Bhargav
    Sep 4 at 6: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 4 at 6:36














up vote
0
down vote

favorite












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?










share|cite|improve this question





















  • 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 4 at 6: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 4 at 6:11











  • @Arthur I believe it is "most significant bit" and "least significant bit".
    – GoodDeeds
    Sep 4 at 6:11











  • @postmortes MSB is the left most digit and LSB is the right most digit
    – Bhargav
    Sep 4 at 6: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 4 at 6:36












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|cite|improve this question













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?







binary-operations binary-programming






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Sep 4 at 5:59









Bhargav

11




11











  • 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 4 at 6: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 4 at 6:11











  • @Arthur I believe it is "most significant bit" and "least significant bit".
    – GoodDeeds
    Sep 4 at 6:11











  • @postmortes MSB is the left most digit and LSB is the right most digit
    – Bhargav
    Sep 4 at 6: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 4 at 6:36
















  • 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 4 at 6: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 4 at 6:11











  • @Arthur I believe it is "most significant bit" and "least significant bit".
    – GoodDeeds
    Sep 4 at 6:11











  • @postmortes MSB is the left most digit and LSB is the right most digit
    – Bhargav
    Sep 4 at 6: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 4 at 6:36















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 4 at 6:04





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 4 at 6:04





1




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 4 at 6:11





(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 4 at 6:11













@Arthur I believe it is "most significant bit" and "least significant bit".
– GoodDeeds
Sep 4 at 6:11





@Arthur I believe it is "most significant bit" and "least significant bit".
– GoodDeeds
Sep 4 at 6:11













@postmortes MSB is the left most digit and LSB is the right most digit
– Bhargav
Sep 4 at 6:32




@postmortes MSB is the left most digit and LSB is the right most digit
– Bhargav
Sep 4 at 6: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 4 at 6:36




@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 4 at 6:36















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "69"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2904680%2fhow-to-find-the-number-of-msb-bits-common-between-two-binary-numbers%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2904680%2fhow-to-find-the-number-of-msb-bits-common-between-two-binary-numbers%23new-answer', 'question_page');

);

Post as a guest













































































這個網誌中的熱門文章

tkz-euclide: tkzDrawCircle[R] not working

How to combine Bézier curves to a surface?

1st Magritte Awards