这是星期五,我的思绪变得糊里糊涂! [英] It's Friday and my mind has turned to mush!

查看:63
本文介绍了这是星期五,我的思绪变得糊里糊涂!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个标志位(1,2,4,...,128,256)我如何确定代表什么位?b $ b?换句话说,2 =第2位,128 =第8位。


只是不确定如何在不使用循环的情况下在JS中执行此操作。

史蒂夫

Given a bit flag (1, 2, 4, ..., 128, 256) how can I determine what bit
is represented? In other words, 2 = bit 2, 128 = bit 8.

Just not sure how to do it simply in JS without resorting to a loop.
Steve

推荐答案

sn **** @ mxlogic.com 写道:
sn****@mxlogic.com wrote:
给定一个标志(1,2,4,...,128,256)我怎么能确定什么位
有代表吗?换句话说,2 =第2位,128 =第8位。
Given a bit flag (1, 2, 4, ..., 128, 256) how can I determine what bit
is represented? In other words, 2 = bit 2, 128 = bit 8.




如果要计算它,请使用:

bit =( flag> 0)? 1 + Math.log(flag)/Math.log(2):0;


我宁愿预先定义一个数组...

var i,bits = []; for(i = 0; i <8; i ++)位[1<< i] = i + 1;

....并在需要时使用该数组:

bit = bits [flag];


ciao,dhgm



If you want to calculate it, use:
bit = (flag > 0)? 1 + Math.log(flag)/Math.log(2) : 0;

I would rather pre-define an array ...
var i, bits = []; for (i=0; i<8; i++) bits[1 << i] = i + 1;
.... and use that array when needed:
bit = bits[flag];

ciao, dhgm


Dietmar Meier写道:
Dietmar Meier wrote:
sn **** @ mxlogic.com 写道:
sn****@mxlogic.com wrote:
给出一个标志(1,2,4,...,128,256) )我怎样才能确定代表什么位?换句话说,2 =第2位,128 =第8位。
Given a bit flag (1, 2, 4, ..., 128, 256) how can I determine what bit
is represented? In other words, 2 = bit 2, 128 = bit 8.



如果要计算它,请使用:
bit =(flag> 0)? 1 + Math.log(flag)/Math.log(2):0;

我宁愿预先定义一个数组......
var i,bits = []; for(i = 0; i <8; i ++)位[1<< i] = i + 1;
...并在需要时使用该数组:
bit = bits [flag];

ciao,dhgm



If you want to calculate it, use:
bit = (flag > 0)? 1 + Math.log(flag)/Math.log(2) : 0;

I would rather pre-define an array ...
var i, bits = []; for (i=0; i<8; i++) bits[1 << i] = i + 1;
... and use that array when needed:
bit = bits[flag];

ciao, dhgm



或者,您可以这样做:


function which_bit(mask){

return(mask& 1) )? 0 //设置0位

:(掩码& 2)? 1 //位1设置

:(掩码& 4)? 2 //位2设置

:(掩码& 8)? 3 //第3位设置

:(掩码& 16)? 4 //第4位设置

:(掩码& 32)? 5 //第5位设置

:(掩码& 64)? 6 //第6位设置

:(掩码& 128)? 7 //第7位设置

:-1; //没有设置位

}



Alternatively, you could do something like this:

function which_bit(mask) {
return (mask & 1)? 0 // bit 0 is set
:(mask & 2)? 1 // bit 1 is set
:(mask & 4)? 2 // bit 2 is set
:(mask & 8)? 3 // bit 3 is set
:(mask & 16)? 4 // bit 4 is set
:(mask & 32)? 5 // bit 5 is set
:(mask & 64)? 6 // bit 6 is set
:(mask & 128)? 7 // bit 7 is set
: -1; // no bits are set
}




< sn **** @ mxlogic.com>在消息中写道

news:11 ******************** @ c13g2000cwb.googlegrou ps.com ...

<sn****@mxlogic.com> wrote in message
news:11********************@c13g2000cwb.googlegrou ps.com...
给定一个标志(1,2,4,...,128,256),我如何确定代表什么位?换句话说,2 =第2位,128 =第8位。

只是不确定如何在JS中完成它而不采用循环。
Steve
Given a bit flag (1, 2, 4, ..., 128, 256) how can I determine what bit
is represented? In other words, 2 = bit 2, 128 = bit 8.

Just not sure how to do it simply in JS without resorting to a loop.
Steve


或使用递归:

var x = 1;

函数theBit(theFlag){

if(Math.pow( 2,x)== theFlag){

返回x +1;

}否则{

x ++;

返回theBit(theFlag);

}

}


var num = 8;

document.write(" number" + num +" is bit" + theBit(num))


or use recursion:
var x = 1;
function theBit(theFlag){
if (Math.pow(2, x) == theFlag){
return x +1;
}else {
x++;
return theBit(theFlag);
}
}

var num = 8;
document.write("The number " + num + " is bit " + theBit(num))


这篇关于这是星期五,我的思绪变得糊里糊涂!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆