# [R] Output of binary representation

Gabor Grothendieck ggrothendieck at gmail.com
Sun May 17 19:48:00 CEST 2009

```gsubfn of the gsubfn package is like gsub but can take a function,
list or proto object
as the replacement instead of a character string and with a list it
can be used to

> library(gsubfn)
> binary.digits <-
+ list("0"= "0000", "1"= "0001", "2"= "0010", "3"= "0011",
+      "4"= "0100", "5"= "0101", "6"= "0110", "7"= "0111",
+      "8"= "1000", "9"= "1001", "A"= "1010", "B"= "1011",
+      "C"= "1100", "D"= "1101", "E"= "1110", "F"= "1111")
>
> gsubfn("[0-9A-F]", binary.digits, "0X1.921FB54442D18P+1")
[1] "0000X0001.1001001000011111101101010100010001000010110100011000P+0001"

On Sun, May 17, 2009 at 1:23 PM, Ted Harding
<Ted.Harding at manchester.ac.uk> wrote:
> I am interested in studying the binary representation of numerics
> (doubles) in R, so am looking for possibilities of output of the
> internal binary representations. sprintf() with format "a" or "A"
> is halfway there:
>
>  sprintf("%A",pi)
> # [1] "0X1.921FB54442D18P+1"
>
> but it is in hex.
>
> The following illustrate the sort of thing I want:
>
> 1.1001 0010 0001 1111 1011 0101 0100 0100 0100 0010 1101 0001 1000
> times 2
>
> 11.0010 0100 0011 1111 0110 1010 1000 1000 1000 0101 1010 0011 000
>
> 0.1100 1001 0000 1111 1101 1010 1010 0010 0010 0001 0110 1000 1100 0
> times 4
>
> (without the spaces -- only put in above for clarity).
>
> While I could take the original output "0X1.921FB54442D18P+1" from
> sprintf() and parse it out into binary using gsub() or the like,
> of submit it to say an 'awk' script via an external file, this would
>
> Is there some function already in R which outputs the bits in the
> binary representation directly?
>
> I see that Dabid Hinds asked a similar question on 17 Aug 2005:
> "Raw data type transformations"
>
>  http://finzi.psych.upenn.edu/R/Rhelp02/archive/59900.html
>
> (without, apparently, getting any response -- at any rate within
> the following 3 months).
>
> With thanks for any suggestions,
> Ted.
>
> --------------------------------------------------------------------
> E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
> Fax-to-email: +44 (0)870 094 0861
> Date: 17-May-09                                       Time: 18:23:49
> ------------------------------ XFMail ------------------------------
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help