[R] Bitwise operations on large numbers
William Dunlap
wdunlap at tibco.com
Fri Jun 6 00:31:04 CEST 2014
> Double allows me to
> store large numbers but I can not perform bitwise shifting.
It is a nuisance that bitwShiftL and bitwShitR don't just shift (and
wrap around to negative) for integers so you could use all 32 bits.
You could use doubles and multiply (*) and divide (%/%) by 2^n instead
of shifting left and right by n bits. That would give you 52 bits.
Bitwise 'and' and 'or' with doubles is harder to do efficiently at the
R level but you can get it done with the following:
shiftL <- function(x, n) x * 2^n
shiftR <- function(x, n) x %/% 2^n
or <- function(x, y) .bitOp(x, y, bitwOr)
and <- function(x, y) .bitOp(x, y, bitwAnd)
bitSlice <- function(x, from, length) shiftR(x, from) %% 2^(from+length)
.bitOp <- function(x, y, bitwOp) bitwOp(bitSlice(x, 0, 26),bitSlice(y,
0, 26)) + shiftL(bitwOp(bitSlice(x, 26, 26), bitSlice(y, 26, 26)), 26)
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Thu, Jun 5, 2014 at 11:43 AM, Koustubh Bagade <kou.genious at gmail.com> wrote:
> Hi
>
> I am working on numbers between 0 - 2^32. I want to perform bit-wise shift
> operations on these numbers. Integer allows me to do bitwise shift
> operations but number range is limited to 0 - 2^31. Double allows me to
> store large numbers but I can not perform bitwise shifting.
>
> I can not change programming language as it is part of larger project.
>
> Is there a way to solve this problem? Perhaps using data types similar to
> unsigned integer or long integer?
>
> Thanks
>
> Koustubh Bagade
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list