Javascript 布尔到整数的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12358554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:48:43  来源:igfitidea点击:

Boolean to integer conversion

javascriptboolean-expression

提问by user1661099

I have 3 separate Boolean variables, bit1, bit2& bit3and I need to calculate the decimal integer equivalent in JavaScript?

我有 3 个单独的布尔变量,bit1, bit2&bit3我需要计算 JavaScript 中的十进制整数等价物吗?

回答by yckart

+true //=> 1
+false //=> 0

+!true //=> 0
+!false //=> 1

回答by Samuel

Ternary operator is a quick one line solution:

三元运算符是一种快速的单行解决方案:

var intVal = bit1 ? 1 : 0;

If you're unfamiliar with the ternary operator, it takes the form

如果您不熟悉三元运算符,则它采用以下形式

<boolean> ? <result if true> : <result if false>

From Sime Vidas in the comments,

来自 Sime Vidas 的评论,

var intVal = +bit1;

works just as well and is faster.

工作得一样好,而且速度更快。

回答by Tim

Number(true) // => 1
Number(false) // => 0

回答by Ethan Brown

If what you're asking is how to get the 3-bit integer value based on bit1 (MSB), bit2, and bit3 (LSB), you can just do the following:

如果您要问的是如何根据位 1 (MSB)、位 2 和位 3 (LSB) 获取 3 位整数值,您可以执行以下操作:

var intval = bit1 << 2 | bit2 << 1 | bit3;

The left shifts (<<) will automatically convert the booleans to their corresponding int values.

左移 ( <<) 将自动将布尔值转换为其相应的 int 值。

Live demo:http://jsfiddle.net/DkYqQ/

现场演示:http : //jsfiddle.net/DkYqQ/

回答by Otman Bouchari

Use ~~ :

使用~~:

bit1 = ~~bit1; // bit1 = true will return 1 or bit1 = false rtuen 0
bit2 = ~~bit2;
bit3 = ~~bit3;

sum = bit1 + bit2 + bit3;

回答by MypKOT-1992

function boolToInt(bool){ return bool ? 1 : 0 }
function boolToInt(bool){ return bool | 0 }
function boolToInt(bool){ return bool & 1 }
function boolToInt(bool){ return ~~bool }
function boolToInt(bool){ return +bool }

choose!

选择!

回答by C_sharp

You have the option of using ternary operator, It will look something like this:

您可以选择使用三元运算符,它看起来像这样:

var i = result ? 1 : 0;

回答by KeithS

Well, this could be two things. Do you want the bitwise decimal equivalent, as if you'd squished bit1, bit2 and bit3 together in adjacent significant bits? Or do you want the hamming weight, where you count how many bits are set?

嗯,这可能是两件事。您是否想要按位十进制等效值,就好像您在相邻的有效位中将 bit1、bit2 和 bit3 压缩在一起一样?或者你想要汉明重量,你计算设置了多少位?

If you want the bitwise equivalent, basically you want to use a combination of bit-shifting and OR-summing, which is pretty common. In pseudocode:

如果您想要按位等效,基本上您想使用位移位和 OR 求和的组合,这很常见。在伪代码中:

var intResult = 0;
for each bit in {bit1, bit2, bit3}
{
   int bitVal = 0;
   if(bit) bitVal = 1;
   intResult = (intResult << 1) | bitVal;
}

If you want the hamming weight, then simply increment the result for each bit that is set:

如果你想要汉明权重,那么只需为设置的每个位增加结果:

var intResult = 0;
for each bit in {bit1, bit2, bit3}
   if(bit) intResult++;

If your language allows the use of booleans as integer values (true = 1, false = 0) these get easier:

如果您的语言允许使用布尔值作为整数值(true = 1,false = 0),这些会变得更容易:

//bit-concatenation
var intResult = 0;
for each bit in {bit1, bit2, bit3}
   intResult = (intResult << 1) | bit;

//hamming weight
var intResult = 0;
for each bit in {bit1, bit2, bit3}
   intResult += bit;

回答by KeithS

function boolsToInt(){
  var s = "";
  for(var i in arguments)s+=arguments[i]?1:0;
  return parseInt(s,2);
}

input can represented as bool or int:

输入可以表示为 bool 或 int:

console.log(boolsToInt(true,false,true,true));
console.log(boolsToInt(1,0,1,1));