string - How to compare two binary values in javascriptnode js? - Stack Overflow

I have two stringvar a = "10001010";var b = "10110010";So What will be the functio

I have two string

var a = "10001010";
var b = "10110010";

So What will be the function to find out Similarities in two string, in that case that function will return this value;

A and B have 5 Digits in mon; which are as below;

var a = "10001010";

var b = "10110010";

How can I get this values?

I need similarities between this two strings.

I have two string

var a = "10001010";
var b = "10110010";

So What will be the function to find out Similarities in two string, in that case that function will return this value;

A and B have 5 Digits in mon; which are as below;

var a = "10001010";

var b = "10110010";

How can I get this values?

I need similarities between this two strings.

Share Improve this question edited May 6, 2019 at 13:43 Neel Kadia asked Oct 26, 2016 at 10:56 Neel KadiaNeel Kadia 771 silver badge10 bronze badges 3
  • 4 Output should be in what format? indexes or the binary data? – gurvinder372 Commented Oct 26, 2016 at 11:00
  • 3 What is the expected result and what you have tried till now? – thefourtheye Commented Oct 26, 2016 at 11:00
  • @gurvinder372 Output should be in Binary data. – Neel Kadia Commented Oct 26, 2016 at 18:23
Add a ment  | 

3 Answers 3

Reset to default 4

You could use bitwise XOR ^ with the numerical values of the strings and value of 28 - 1.

In the binary result, a single 1 means same value of a and b and 0 means not.

   value    binary  dec  ment
--------  --------  ---  ---------------------------------------
       a  10001010  138
       b  10110010  178
--------  --------  ---
       ^  00111000   56  it shows only the changed values with 1
2^^8 - 1  11111111  255
--------  --------  ---
       ^  11000111  199  result with 1 for same value, 0 for not

var a = parseInt("10001010", 2),
    b = parseInt("10110010", 2),
    result = (a ^ b) ^ (1 << 8) - 1;

console.log(result);
console.log(result.toString(2));

I think, you can just pare them like this

("10001010" > "10110010") --> false
("10001010" < "10110010") --> true
("10001010" < "00110010") --> false
("00110010" == "00110010") --> true

I have written a logic to do this enter link description here

var test = "10001010";
var test2  = "10110010";
var testArray = test.split('');
var testArray2 = test2.split('');
var resultArray = [];
for(index = 0; testArray.length > index;index++ ){
    if(testArray[index] === testArray2[index]){
     resultArray.push(testArray[index])
    }else{
      resultArray.push("*")
    }
}

console.log(resultArray.join(""));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745433937a4627521.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信