【发布时间】:2019-08-09 01:51:43
【问题描述】:
所以我最终要尝试优化我的代码,我正在做一堆标志检查。我试图通过简单地使用 8 位来表示一个八边形来开始简单,我将有一个8 位数字表示“已使用边”和 8 位数字表示“未使用边”
非常直截了当,因为一个八边形很方便地有 8 个东西:D
所以我在 javascript 中拥有的是
let sidesNotInUse = 0b11111111;
let sidesInUse = 0b00000000;
我希望能够写出以下函数
function inUse(sideIndex) {
// checks that the 1 flag at index is set in sidesInUse
}
function use(sideIndex) {
if(!(inUse(sideIndex))) {
// operator to set flag at index in sidesInUse to 1 and sidesNotInUse to 0
}
}
function unuse(sideIndex) {
if(inUse(sideIndex)) {
// operator to set flag at index in sidesInUse to 0 and sidesNotInUse to 1
}
}
不确定我是否看错了,但任何帮助都会很有用
【问题讨论】:
标签: javascript binary bit-manipulation bitwise-operators