[R] Is there a way to force counters to be treated as "unsigned?"

David Wolfskill david at catwhisker.org
Mon Feb 14 23:45:49 CET 2011


I am acquiring some sampled data that is time-stamped (with a
POSIXct).  Some of the data is in the form of "counters" -- that
is, what is interesting isn't value of a given counter at a given
time, but the change in the counter from one sample to a later one.

As the counters are only incremented, they would be perceived to be
monotonically increasing -- ideally.  Unfortunately, the counters can
"wrap," with the effect being that a later value may appear to be
smaller than an earlier one.

Other code I've seen (in C) that works with these counters merely casts
the counters to (unsigned) before calculating the difference, then casts
the result as (int).  Subject to the constraint that this trick only
works if the system doing the calculation has the same size "int" as the
target system, and assumes that negative numbers are represented in
"twos-complement" form, it works well enough.

[E.g., suppose we are using 4-bit counters, 0 .. 15].  If a counter
at T0 is (say) 14 and the value of the counter at T1 is (say) 3,
the usual arithmetic would say that the difference is

	3 - 14 => -11

But if we (instead) calculate

	(int)((unsigned)3 - (unsigned)4) => 5

which works out to be correct.]

Is there a way to do something similar to this in R?

(I suppose that if I know the size of the counters in the original
environment, I could watch for a negative difference, and if seen, add
the appropriate power of 2 to the (negative) result.  I would be
disinclined to consider that "elegant," though. :-})

Peace,
david
-- 
David H. Wolfskill				david at catwhisker.org
Depriving a girl or boy of an opportunity for education is evil.

See http://www.catwhisker.org/~david/publickey.gpg for my public key.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110214/bbdbe2d4/attachment.bin>


More information about the R-help mailing list