Paul Terwilliger - 1 year ago 75
R Question

# Masking Highest Set Bit in R?

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

# 8 in binary is 1000, this should turn into 0000
``````

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.

For an R solution:

``````function_mask <- function(x) {
xb <-intToBits(x)
highestbit <- length(xb) - match("01", rev(xb))
x-2L^highestbit
}
``````

Comparing speeds to the other answers, we see this one is fastest, so far.

``````function_mask1 <- function(x) {
bits = intToBits(x)                 # Convert integer to binary vector
ii = tail(which(bits > 0), n=1)     # Identify most significant bit
bits[ii] = as.raw(0)                # Mask the most significant bit
out = packBits(bits,type='integer') # Convert binary back to integer
return(out)
}