{integer包含一些2的幂}函数

hed*_*red 0 r

我有一个数字代表一个对象的状态,我想检查其中一个状态.例如,如果数字为"22",则在检查16,4或2时应返回true,对其他任何内容应返回false.我的功能是

containsOrderType <- function(orderType, state) #returns whether the bitmask translates to containing that order type
{
  state <- as.numeric(state)
  if(orderType>state) return(FALSE)
  binState <- as.integer(state)
  class(binState) <- "binmode"
  binState <- as.character(binState)
  dim(binState) <- dim(state)
  X<-log2(orderType)+1
  if(str_sub(binState,-X,-X)==1) return(TRUE)
  return(FALSE)
}
Run Code Online (Sandbox Code Playgroud)

直到今天这个工作正常一个月,我很确定问题是昏暗(状态)正在变暗([整数]),似乎总是"空".这发生在R 2.15.3和R 3.0.1中.我得到它,如果那是一致的,但这个功能完全按照预期工作了一段时间,现在它没有.这是R.Utils中的intToBin函数,它与我的函数的第3-6行相同.

function (x) 
{
    y <- as.integer(x)
    class(y) <- "binmode"
    y <- as.character(y)
    dim(y) <- dim(x)
    y
}
Run Code Online (Sandbox Code Playgroud)

>dim
function (x)  .Primitive("dim")
> class
function (x)  .Primitive("class")
Run Code Online (Sandbox Code Playgroud)

所以那些没有被包裹或任何奇怪的东西覆盖.

Mar*_*gan 5

bitwAnd例如,尝试基数R中的函数

> bitwAnd(22, 2^(0:10))
 [1]  0  2  4  0 16  0  0  0  0  0  0
> bitwAnd(1:22, 16)
 [1]  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 16 16 16 16 16 16 16
> bitwAnd(4, 2060)
[1] 4
> (bitwAnd(8, 2060) != 0) == containsOrderType(8, 2060)
> TRUE
Run Code Online (Sandbox Code Playgroud)

或者bitAndbitops包中.