Skip to content

Instantly share code, notes, and snippets.

@kalebr
Created June 11, 2014 15:03
Show Gist options
  • Save kalebr/a21437f2198d05f12116 to your computer and use it in GitHub Desktop.
Save kalebr/a21437f2198d05f12116 to your computer and use it in GitHub Desktop.
encoding a set of flags as an int in R
gen_id <- function( v, silent=FALSE ) {
v = sort(v)
z <- unlist(lapply( which( chars %in% v) - 1, FUN=function(x) { bitwShiftL(1, x)}))
total <- sum(z)
if(!silent) {
print(v)
print(z)
print(total)
}
}
chars <- c('a', 'b', 'c', 'd', 'e', 'f', 'g')
# 1 2 4 8 16 32 64
gen_id( sample(chars, sample(1:7, 1) ) )
# benchmark(gen_id( sample(chars, sample(1:7, 1) ) ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment