February 22, 2014
Will, not sure why you seem not to prefer to use them in your code examples. They aren't as fast as a conditional check because mod operators are really remainder operators and perform a division each increment. Also, maybe you don't use them to avoid confusion?
However, in any case, I found this bit of code (in your oscillators chapter, pg 307), to be needlessly obfuscating:
int nReadIndexNext = (nReadIndex + 1) > 1023 ? 0 : nReadIndex + 1;
A more clear way to do this would be:
int nReadIndexNext = (nReadIndex + 1) % 1024;
mod operators have some downsides of course (i.e. division is more expensive than an if-statement) and they can fail if you expect them to work on negative numbers.
Since the buffer here is a power of 2, we can do the same mod-operator line with:
int nReadIndexNext = (nReadIndex + 1) & 1023; // or maxbuffersize - 1
This guy has no performance issues and is just as clear if you're used to bit operations.
Most Users Ever Online: 36
Currently Browsing this Page:
Guest Posters: 1
Newest Members:Matt, dspstudent, strings4v, TheSmile, semihyavuzz, alfredLue, danioc, midnightskate3, Alia5, Shamal Sundar
Moderators: W Pirkle: 208
Administrators: Tom: 67, JD Young: 80, Will Pirkle: 0, W Pirkle: 208