I'm working in R. I need to find a fast function that will mask the highest set bit of an integer. For example:
# 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
This is equivalent to subtracting the closest lower power of two. I will be happy if I can find a fast function that will simply find the closest lower power of two. For example:
# 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
I have found bitwise operations in R: for example, bitwShiftL and bitwShiftR. However, I do not know how to implement a solution in R.
I have seen solutions in other languages: Java, C, and C++. However, I do not know how to implement these solutions in R.
There are solutions in C++ using Rcpp, however Rcpp does not support integers larger than 32-bit. I need larger integers than that.