Opposite of "bitwAND" function?

I found this function in R that can generate the "power set" for a set of elements:

f <- function(set) { 
    n <- length(set)
    masks <- 2^(1:n-1)
    lapply( 1:2^n-1, function(u) set[ bitwAnd(u, masks) != 0 ] )

results = f((LETTERS[1:5])

results = sapply(results, paste, collapse = " ")

I learned how to interact with very large "power sets" that the computer can not load into memory. For example - suppose I wanted to make the "power set" for all 26 letters in the English alphabet (this set would contain 2^26 = 67108864 elements). I could find out the "13626980"th element in this list without actually generating the list (since it would be impossible to generate/store such a big list):

LETTERS[bitwAnd(13626980, 2^(1:26-1)) != 0]
 [1] "C" "F" "G" "J" "K" "L" "N" "O" "P" "Q" "R" "S" "T" "W" "X"
  • I had the following question : Is it possible to do the "opposite" of this task?

For example, given the number "13626980" - can some function determine which sequence of letters ("C" "F" "G" "J" "K" "L" "N" "O" "P" "Q" "R" "S" "T" "W" "X") corresponds to? Is there some hypothetical function like:

> hypothetical_function(c("C" "F" "G" "J" "K" "L" "N" "O" "P" "Q" "R" "S" "T" "W" "X")) 


Is this possible?

Thank you!

This seems to work:

target <- c("C" ,"F" ,"G", "J", "K", "L", "N" ,"O" ,"P" ,"Q", "R", "S" ,"T" ,"W" ,"X")
source <- LETTERS

masks <- 2^(1:length(source)-1)
sum(masks[which(source %in% target)])
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.