javascript 二元运算不适用于选项
javascript Binary operation don't work for options
这是我的...
某些数组项具有二进制选项
喜欢
- $item[0] = 0010
- $item[1] = 1000
- $item[2] = 0110
- $item[3] = 1101
每一位代表一些选项
我需要的是将其与客户请求选项进行比较
假设它是 0010
所以我只需要显示 $item[0] 和 $item[2] 的逻辑,因为第二个字节是 1。但是,当没有客户选项时检查:0000 我必须全部显示它们
只有在显示某些选项时才必须有过滤器...
我应该多听数学class...我现在很无能,请帮忙!
注意:
一切都来自这里:http://jsfiddle.net/yHxue/
但我不明白这一行:
markers[m].setMap(((markers[m].props & props)>>>0===props)? ((props)?map:null): null);
,所以我重写了,我的不行!
如果您的数据是实际数字,则只需对每个成员使用按位 &
运算符。
var customerOption = parseInt("0010", 2)
$item.forEach(function(n, i) {
if (customerOption === 0 || ((n & customerOption) == customerOption))
alert(i); // show this item
});
如果它们是字符串,那么您需要先将每个项目转换为数字。
同上,但是...
parseInt(n, 2);
var $item = [];
$item[0] = "0010"
$item[1] = "1000"
$item[2] = "0110"
$item[3] = "1101"
var customerOption = parseInt("0010", 2)
$item.forEach(function(n, i) {
if (customerOption === 0 || ((parseInt(n, 2) & customerOption) == customerOption))
document.querySelector("pre").textContent += "\n0010 matched index: " + i;
});
document.querySelector("pre").textContent += "\n\n"
var customerOption = parseInt("0110", 2)
$item.forEach(function(n, i) {
if (customerOption === 0 || ((parseInt(n, 2) & customerOption) == customerOption))
document.querySelector("pre").textContent += "\n0110 matched index: " + i;
});
<pre></pre>
使用 parseInt(string, 2)
.
将所有字符串(项目以及掩码)转换为数字
如果mask
是0
,拿走所有物品。如果不是:
然后使用&
运算符查看公共1
位:
var common = item & mask;
然后测试common
:
- 如果你想要任何掩码位,那么
common
不应该是 0
。
- 如果您想要所有掩码位,那么
common
应该等于 mask
。
编辑:
markers[m].setMap(
((markers[m].props & props) >>> 0 === props) ?
((props) ? map : null) :
null
);
markers[m].props & props
寻找常见的 1
位,正如我在上面解释的那样; >>> 0
确保数字为正数。结果根据 props
进行测试,确保公共位是 props
中的所有位(如我上面的第二个选项)。如果设置了所有 props
位,并且 props
不为零,则 markers[m]
被 setMap
-ped 为 map
;否则,null
.
var selections = ['0010', '1000', '0110', '1111'];
var getRequiredSelections = function (customerSelection) {
var customerSelectionInt = parseInt(customerSelection, 2);
if (customerSelectionInt) {
return selections.filter(function (binaryString) {
return parseInt(binaryString, 2) & customerSelectionInt;
});
} else {
return selections;
}
};
你可以这样使用它:
getRequiredSelections('0010');
BUT, when there is no customer options check : 0000 i must show them all
在2的补码中,-1是二进制的全部1
if (!props) props = -1;
精确测试二进制文件,同时使用 &
和 ===
needle === (needle & haystack)
这意味着在 haystack = 0001
中寻找 needle = 1001
是 false
,即使 1001 & 0001
是 truthy 0001
您可能不想在您的案例中使用完全匹配
您可以编写一些代码来处理您的数组
function applyByFlag(flag, callbackTrue, callbackFalse) {
if (!flag)
flag = -1;
function nop() {}
if (!callbackTrue) callbackTrue = nop;
if (!callbackFalse) callbackFalse = nop;
boxes.forEach(function (e, i, a) {
if (flag & e) // not exact test
callbackTrue.call(e, e, i, a);
else
callbackFalse.call(e, e, i, a);
});
}
当然,您可能希望调整测试以对抗 e.props
你的代码已经差不多了;只需在 props
为零时添加额外的检查:
for(var m=0; m < markers.length; m++) {
var show = (markers[m].props & props)>>>0===props || props == 0;
markers[m].setMap(show ? map : null);
}
markers[m].props & props
表达式要么是 0
(如果不是 props
的所有位都匹配),要么是 props
本身的值(如果 props
匹配)。
额外的>>>0
会将表达式变成一个无符号的32位整数值,但是如果你使用下面的表达式就没有必要了:
var show = (markers[m].props & props) || props == 0;
这是我的... 某些数组项具有二进制选项 喜欢
- $item[0] = 0010
- $item[1] = 1000
- $item[2] = 0110
- $item[3] = 1101
每一位代表一些选项
我需要的是将其与客户请求选项进行比较 假设它是 0010
所以我只需要显示 $item[0] 和 $item[2] 的逻辑,因为第二个字节是 1。但是,当没有客户选项时检查:0000 我必须全部显示它们
只有在显示某些选项时才必须有过滤器...
我应该多听数学class...我现在很无能,请帮忙!
注意:
一切都来自这里:http://jsfiddle.net/yHxue/
但我不明白这一行:
markers[m].setMap(((markers[m].props & props)>>>0===props)? ((props)?map:null): null);
,所以我重写了,我的不行!
如果您的数据是实际数字,则只需对每个成员使用按位 &
运算符。
var customerOption = parseInt("0010", 2)
$item.forEach(function(n, i) {
if (customerOption === 0 || ((n & customerOption) == customerOption))
alert(i); // show this item
});
如果它们是字符串,那么您需要先将每个项目转换为数字。
同上,但是...
parseInt(n, 2);
var $item = [];
$item[0] = "0010"
$item[1] = "1000"
$item[2] = "0110"
$item[3] = "1101"
var customerOption = parseInt("0010", 2)
$item.forEach(function(n, i) {
if (customerOption === 0 || ((parseInt(n, 2) & customerOption) == customerOption))
document.querySelector("pre").textContent += "\n0010 matched index: " + i;
});
document.querySelector("pre").textContent += "\n\n"
var customerOption = parseInt("0110", 2)
$item.forEach(function(n, i) {
if (customerOption === 0 || ((parseInt(n, 2) & customerOption) == customerOption))
document.querySelector("pre").textContent += "\n0110 matched index: " + i;
});
<pre></pre>
使用 parseInt(string, 2)
.
如果mask
是0
,拿走所有物品。如果不是:
然后使用&
运算符查看公共1
位:
var common = item & mask;
然后测试common
:
- 如果你想要任何掩码位,那么
common
不应该是0
。 - 如果您想要所有掩码位,那么
common
应该等于mask
。
编辑:
markers[m].setMap(
((markers[m].props & props) >>> 0 === props) ?
((props) ? map : null) :
null
);
markers[m].props & props
寻找常见的 1
位,正如我在上面解释的那样; >>> 0
确保数字为正数。结果根据 props
进行测试,确保公共位是 props
中的所有位(如我上面的第二个选项)。如果设置了所有 props
位,并且 props
不为零,则 markers[m]
被 setMap
-ped 为 map
;否则,null
.
var selections = ['0010', '1000', '0110', '1111'];
var getRequiredSelections = function (customerSelection) {
var customerSelectionInt = parseInt(customerSelection, 2);
if (customerSelectionInt) {
return selections.filter(function (binaryString) {
return parseInt(binaryString, 2) & customerSelectionInt;
});
} else {
return selections;
}
};
你可以这样使用它:
getRequiredSelections('0010');
BUT, when there is no customer options check : 0000 i must show them all
在2的补码中,-1是二进制的全部1
if (!props) props = -1;
精确测试二进制文件,同时使用 &
和 ===
needle === (needle & haystack)
这意味着在 haystack = 0001
中寻找 needle = 1001
是 false
,即使 1001 & 0001
是 truthy 0001
您可能不想在您的案例中使用完全匹配
您可以编写一些代码来处理您的数组
function applyByFlag(flag, callbackTrue, callbackFalse) {
if (!flag)
flag = -1;
function nop() {}
if (!callbackTrue) callbackTrue = nop;
if (!callbackFalse) callbackFalse = nop;
boxes.forEach(function (e, i, a) {
if (flag & e) // not exact test
callbackTrue.call(e, e, i, a);
else
callbackFalse.call(e, e, i, a);
});
}
当然,您可能希望调整测试以对抗 e.props
你的代码已经差不多了;只需在 props
为零时添加额外的检查:
for(var m=0; m < markers.length; m++) {
var show = (markers[m].props & props)>>>0===props || props == 0;
markers[m].setMap(show ? map : null);
}
markers[m].props & props
表达式要么是 0
(如果不是 props
的所有位都匹配),要么是 props
本身的值(如果 props
匹配)。
额外的>>>0
会将表达式变成一个无符号的32位整数值,但是如果你使用下面的表达式就没有必要了:
var show = (markers[m].props & props) || props == 0;