- An 8-bit unsigned integer type for R based upon the
raw
type. - Each value is stored in a single byte (whereas
integer
values in R are stored in 4 bytes), so there can be memory savings. - Stores only values 0-255
- No
NA
,NaN
, orInf
allowed - Basic arithmethic
- An experiment to see if there's more useful data type for working with 8-bit data
(without dropping down to the level of
Rcpp
). - The aim is to have similar behaviour to the
uint8_t
type in C.
devtools::install_github('coolbutuseless/uint8')
- Each
uint8
is stored in a single byte (but there is, of course, overhead associated with atomic vector metainfo). - Using
uint8
can result in 25% of the memory use of Rinteger
values. e.g.pryr::object_size(1:10000)
=> 40 kBpryr::object_size(uint8(1:10000))
=> 10.2 kB
- Specific S3 methods written for:
uint8()
(data creation)+
,-
,*
,/
%%
,%/%
:
(sequence creation)any()
,all()
,as.bitstring()
,abs()
,order()
,sign()
- A lot of things come for free because of the use of the built-in R
raw
typeas.integer()
,as.numeric()
,as.character()
%in%
<
,<=
,==
,!=
,>=
,>
- Can't use a
uint8
for subscripts (similar limitation in thebit64
package forinteger64
) - The
:
operator only dispatches on its first argument, souint8(1):10
will work, but1:uint8(10)
won't.
raw
allows for 8-bit data, but doesn't support any arithmetic operatorsuint8
support basic arithmeticraw
accepts values outside the range 0-255 by setting them to zerouint8
accepts values outside the range 0-255 by calculating the value modulo 256. e.g.uint8(-10)
is246
raw
acceptsNA
,NaN
,Inf
by setting them to zero.uint8
disallowsNA
,NaN
andInf
b02 <- as.uint8( 2)
b0a <- as.uint8( 10)
bff <- as.uint8(255)
# Addition is modulo 256
bff + b0a # (255 + 10) %% 256 => 9
## [1] 9
# Division result trucated to integer value
b0a/b02 # 10/2 => 5
## [1] 5
bff/b0a # 255/10 => 25.5 => 25
## [1] 25
# exponentiation is modulo 256
b02 ^ b0a # 2^10 => 1024. 1024 %% 256 => 0
## [1] 0
# initialisation is modulo 256
as.uint8(seq(200, 300, 5))
## [1] 200 205 210 215 220 225 230 235 240 245 250 255 4 9 14 19 24
## [18] 29 34 39 44
- Flesh out the supported functions as needed
- Investigate if there's any point to an actual
Rcpp
backend for speed or accuracy of behaviour.