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
3 Answers
Reset to default 4You 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条)