Created
June 11, 2014 15:03
-
-
Save kalebr/a21437f2198d05f12116 to your computer and use it in GitHub Desktop.
encoding a set of flags as an int in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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