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 <da...@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 da...@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@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? ______________________________________________ R-help@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.