From simon.urbanek at r-project.org  Tue Mar 29 16:06:44 2011
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Tue, 29 Mar 2011 10:06:44 -0400
Subject: [Rd] Reading 64-bit integers
In-Reply-To: <AANLkTinndjFuoV17Th4PRUdpbrY-7RjTbyXwY6T6n+cd@mail.gmail.com>
References: <AANLkTinndjFuoV17Th4PRUdpbrY-7RjTbyXwY6T6n+cd@mail.gmail.com>
Message-ID: <10962CE3-2F14-4BCE-8E78-AE18814C3F81@r-project.org>


On Mar 29, 2011, at 8:46 AM, Jon Clayden wrote:

> Dear all,
> 
> I see from some previous threads that support for 64-bit integers in R
> may be an aim for future versions, but in the meantime I'm wondering
> whether it is possible to read in integers of greater than 32 bits at
> all. Judging from ?readBin, it should be possible to read 8-byte
> integers to some degree, but it is clearly limited in practice by R's
> internally 32-bit integer type:
> 
>> x <- as.raw(c(0,0,0,0,1,0,0,0))
>> (readBin(x,"integer",n=1,size=8,signed=F,endian="big"))
> [1] 16777216
>> x <- as.raw(c(0,0,0,1,0,0,0,0))
>> (readBin(x,"integer",n=1,size=8,signed=F,endian="big"))
> [1] 0
> 
> For values that fit into 32 bits it works fine, but for larger values
> it fails. (I'm a bit surprised by the zero - should the value not be
> NA if it is out of range?

No, it's not out of range - int is only 4 bytes so only 4 first bytes (respecting endianness order, hence LSB) are used.


> ) The value can be represented as a double,
> though:
> 
>> 4294967296
> [1] 4294967296
> 
> I wouldn't expect readBin() to return a double if an integer was
> requested, but is there any way to get the correct value out of it?

Trivially (for your unsigned big-endian case):

y <- readBin(x, "integer", n=length(x)/4L, endian="big")
y <- ifelse(y < 0, 2^32 + y, y)
i <- seq(1,length(y),2)
y <- y[i] * 2^32 + y[i + 1L]

Cheers,
Simon


> I
> suppose one could read the bytes into a raw vector and then
> reconstruct the number manually from that, but is there a more elegant
> or built-in solution that I'm not aware of?
> 
> This is R 2.12.1 on Mac OS X.6.7 - .Machine$sizeof.long is 8.
> 
> Many thanks,
> Jon
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



[Rd] Reading 64-bit integers
Simon Urbanek