[R] binary representation of an integer?

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Feb 15 21:16:22 CET 2000


Thomas Lumley <thomas at biostat.washington.edu> writes:

> And this one works up to 2^32-1 but returns a string with leading zeros
> 
> binarys<-function(i) {
> a<-2^(31:0)
> b<-2*a
> sapply(i,function(x) paste(as.integer((x %% b)>=a),collapse=""))
> }
> 
> Either could be improved, but it's probably not worth much effort.

And there's also:

bb<-function(i) if (i) paste(bb(i %/% 2), i %% 2, sep="") else ""

which works for all integer 0 < i < 2^54 on IEEE machines. 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list