David Wolfskill
2011-Feb-14 22:45 UTC
[R] Is there a way to force counters to be treated as "unsigned?"
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>
jim holtman
2011-Feb-15 14:55 UTC
[R] Is there a way to force counters to be treated as "unsigned?"
This is handled simply with an AND operator. You use a mask the size of the counter:> require(bitops) > 3 - 14[1] -11> bitAnd(3 - 14, 0xF)[1] 5> >On Mon, Feb 14, 2011 at 5:45 PM, David Wolfskill <david at catwhisker.org> wrote:> 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. > > ______________________________________________ > 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. > >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Petr Savicky
2011-Feb-15 20:28 UTC
[R] Is there a way to force counters to be treated as "unsigned?"
On Mon, Feb 14, 2011 at 02:45:49PM -0800, David Wolfskill wrote:> 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?The arithmetic of unsigned integers between 0 and 2^k - 1 is the same as the arithmetic mod 2^k. This suggests the following approach (3 - 14) %% 16 [1] 5 Hope this helps. Petr Savicky.