【发布时间】:2015-03-30 05:19:23
【问题描述】:
所以我知道我的问题是关于掩蔽。我看到了 javascript here 的屏蔽文档。
所以我尝试了:
var PEN = 1;
var CHAIR = 2;
var TABLE = 4;
> PEN | CHAIR
3
但是,如果我拥有的是 3,我如何仅从那个号码中获取我拥有的东西呢?
原始问题
说,我有以下常数:
1 | 2 | 4
这些数字对应一个东西。
假设:1 是笔,2 是椅子,4 是桌子。
可能性:
If I have the #1 it means I have a pen but NO chair and table.
If I have the #2 it means I have a chair but NO pen and table.
If I have the #4 it means I have a table but NO pen and chair.
If I have the #3 it means I have a pen and a chair but NO table.
If I have the #6 it means I have a chair, a table but NO pen.
If I have the #7 it means I have a pen, a chair and a table.
问题:现在说我只知道数字 6。我如何以编程方式破译 6 表示 2 和 4,或者我有一把椅子和一张桌子?
对不起,这也让我感到困惑。我正在尝试将游戏的技能列表算法重新实现为 javascript。如果我有 6 则意味着我有 2 和 3 技能,但没有 1 技能。
还有这种方法叫什么?
【问题讨论】:
-
那么,您要查找一个数字的所有质数分隔符吗?
-
@MarcB 好像是我要找的那个。
-
嗯...二进制
1 == 0b00000001、2 == 0b00000010、4 == 0b00000100、6 == 0b00000110...希望你明白了...
标签: javascript algorithm