我在R的工作。我需要找到一个快速的功能,将掩盖整数的最高设置位。例如:屏蔽R中最高位的位?
# 6 binary is 110, this should turn into 010 which is 2
function_mask(6) = 2
# 8 in binary is 1000, this should turn into 0000
function_mask(8) = 0
这相当于减去两个最接近的较低幂。如果我能找到一个能够找到最接近的两个较低幂的快速函数,我会很高兴。例如:
# 6 in binary is 110, the MSB is 100
function_power_two(6) = 4
function_mask(6) = 6 - function_power_two(6) = 2
# 8 in binary is 1000, the MSB is 1000 which is 8 in base 10
function_power_two(8) = 8
function_mask(8) = 8 - function_power_two(8) = 0
我在R中发现了按位运算:例如bitwShiftL和bitwShiftR。但是,我不知道如何在R中实现解决方案。
我已经看到其他语言的解决方案:Java,C和C++。但是,我不知道如何在R中实现这些解决方案。
在C++中有使用Rcpp的解决方案,但是Rcpp不支持大于32位的整数。我需要比那更大的整数。