【问题标题】:How can I use the binary system (1, 2, 4) as an algorithm?如何使用二进制系统(1、2、4)作为算法?
【发布时间】:2013-03-11 16:10:51
【问题描述】:

类似于 chmod,我想使用 1、2、4 作为通知方式。

1 = Email 
2 = SMS 
4 = Push notifications

是否有可以输入数字(上图)并可以返回对象的函数(最好是javascript):

{
    email: true,
    sms: true,
    push: false
}

我希望函数不被硬编码。我不想要一堆 if 语句来检查每个组合。我想要一种“聪明”的方式来做到这一点。

【问题讨论】:

  • 您需要查看按位运算符 - &, |, <<, >>, ^。有一些很好的教程介绍了如何查找设置的位,这就是您想要的。
  • opts = { email: 1 << 0, sms: 1 << 1, push: 1 << 2 } - 或用于生成值的任何合适的替代品。加上| 用于构建和& 用于测试:x = opts.email | opts.sms; if (x & opts.email) { /* here */ }; if (x & opts.push) { /* not here */ } ..

标签: javascript algorithm math binary


【解决方案1】:

这样的事情怎么样:

var commsMode = {
    email: 1,
    sms: 2,
    push: 4,
    toObject: function(val){
        var rt = {};

        for (e in commsMode) {
            rt[e] = (val & commsMode[e]) > 0;
        }

        return rt;
    }
};

var obj = commsMode.toObject(3);

Working example here.

扩展示例:

var commsMode = {
    email: 1,
    sms: 2,
    push: 4,
    mode4: 8,
    mode5: 16,
    mode6: 32,
    mode7: 64,
    mode8: 128,
    toObject: function(val){
        var rt = {};

        for (e in commsMode) {
            rt[e] = (val & commsMode[e]) > 0;
        }

        return rt;
    }
};

var obj = commsMode.toObject(233);

for (p in obj) {
    alert(p + ': ' + obj[p])
}

Working example here

【讨论】:

  • @TIMEX。必然是。比你想象的更简单?
  • 它将工作到maximum supported JS number。语言不特定:32 位(有符号)将支持 31 个条目,64 位(有符号)将支持 63 个条目。
  • 我不明白-在您的示例中,您将 3 发送到该函数,然后您会收到 email->true、sms->true、push->false ?!我在这里错过了什么吗?这不是他要求的……
  • 这是一个位掩码。考虑到值 1、2 和 4,我认为这很明显。
【解决方案2】:

试试这样 - 抱歉,这是 Python,但 JavaScript 翻译应该非常相似:

def notifications(mask):
    return {
        'email' : mask & 4 != 0,
        'sms'   : mask & 2 != 0,
        'push'  : mask & 1 != 0
    }

notifications(0b111) # same as notifications(7), or notifications(email+sms+push)
=> {'push': True, 'sms': True, 'email': True}

在上面,我们说通知二进制位掩码是0b111(或 7,如果您更喜欢使用 base-10),这意味着所有三个通知都已启用。类似地,如果我们作为位掩码 0b010(或 base-10 中的 2)传递,那么答案将是:

notifications(0b010) # same as notifications(2), or notifications(sms)
=> {'push': False, 'sms': True, 'email': False}

【讨论】:

  • OP 要求提供通用方法。这需要为每个新模式换行。
  • @flem 不,它没有,只需将answer 打包到一个接收notification 作为参数的函数中,然后返回结果映射
  • “我希望函数不被硬编码。我不想要一堆 if 语句来检查每个组合。我想要一种“智能”的方式来做到这一点。”
【解决方案3】:

将多个参数发送到函数的解决方案:

var states = {
  '1': 'email',
  '2': 'sms',
  '4': 'push'
};

function getStates(){
 var o = {};
 for(var val in states){
   o[states[val]] = false;   
 }

   for(var i=0;i<arguments.length;i++){
     var arg = arguments[i];  
     var state = states[arg];
      if(state){
        o[state] = true;
      }  
  }

  return o;
}

console.log(getStates()); // return {email: false, sms: false, push: false}
console.log(getStates(1,2)); // return {email: true, sms: true, push: false} 

http://jsfiddle.net/ZTA6E/

【讨论】:

    猜你喜欢
    • 2014-08-03
    • 2012-11-05
    • 2011-03-10
    • 2018-09-15
    • 2016-10-16
    • 2019-05-31
    • 2016-09-05
    • 2018-12-18
    • 2017-01-24
    相关资源
    最近更新 更多