Hi, Is there a function in R that lets one represent an integer in binary format for a given number of bits? So an example would be.... > binary.function(num=5, num.of.bits=8) > "00000101" Or, is this something I have to write myself? Any help would be appreciated. Cheers, Sam.
Hi, I just wrote a little C program (and interfaced it with R) that does it for me, but I will take a look at that function. Cheers, Sam. Uwe Ligges wrote:> Samuel Edward Kemp wrote: > >> Hi, >> >> Is there a function in R that lets one represent an integer in binary >> format for a given number of bits? So an example would be.... >> >> > binary.function(num=5, num.of.bits=8) >> > "00000101" >> >> Or, is this something I have to write myself? >> >> Any help would be appreciated. > > > binary() in package "wle". > > Uwe Ligges > > > > >> Cheers, >> >> Sam. >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://www.stat.math.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide! >> http://www.R-project.org/posting-guide.html > > >
Samuel Edward Kemp wrote:> Hi, > > Is there a function in R that lets one represent an integer in binary > format for a given number of bits? So an example would be.... > > > binary.function(num=5, num.of.bits=8) > > "00000101" > > Or, is this something I have to write myself? > > Any help would be appreciated.binary() in package "wle". Uwe Ligges> Cheers, > > Sam. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
Hi Sam. Try: as.binary <- function(n,base=2 , r=FALSE) { out <- NULL while(n > 0) { if(r) { out <- c(out , n%%base) } else { out <- c(n%%base , out) } n <- n %/% base } return(out) } HTH robin>Hi, > >Is there a function in R that lets one represent an integer in >binary format for a given number of bits? So an example would be.... > >> binary.function(num=5, num.of.bits=8) >> "00000101" > >Or, is this something I have to write myself? > >Any help would be appreciated. > >Cheers, > >Sam. > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- Robin Hankin Uncertainty Analyst Southampton Oceanography Centre SO14 3ZH tel +44(0)23-8059-7743 initialDOTsurname at soc.soton.ac.uk (edit in obvious way; spam precaution)
>>>>> "Samuel" == Samuel Edward Kemp <sam.kemp2 at ntlworld.com> >>>>> on Wed, 14 Apr 2004 22:20:24 +0100 writes:Samuel> Hi, Is there a function in R that lets one represent Samuel> an integer in binary format for a given number of Samuel> bits? So an example would be.... >> binary.function(num=5, num.of.bits=8) "00000101" Samuel> Or, is this something I have to write myself? no. In package "sfsmisc", there's also>> digitsBase package:sfsmisc R Documentation >> >> Digit/Bit Representation of Integers in any Base >> >> Description: >> >> Compute the vector of "digits" A of the 'base' b representation of >> a number N, N = sum(k = 0:M ; A[M-k] * b^k). >> >> Usage: >> >> digitsBase(x, base = 2, ndigits = 1 + floor(log(max(x), base)))e.g.,> library(sfsmisc) # after installing it> digitsBase(5, base= 2, 10)[,1] [1,] 0 [2,] 0 [3,] 0 [4,] 0 [5,] 0 [6,] 0 [7,] 0 [8,] 1 [9,] 0 [10,] 1> empty.dimnames(digitsBase(0:33, 2)) # binary0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 where you see that it does work vectorized.