<na*******@yahoo.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
>
Would this expression round an integer n to the nearest power of 4 ?
((n-1)|3) + 1
You need to define what you mean by "power" and by "round" and by "nearest".
Did you actually mean "multiple" (0, 4, 8, 12, 16, etc.)?
Or did you actually mean "power" (1, 4, 16, 64, etc.)?
Looking at your code ...
0 --0
1 --4
2 --4
3 --4
4 --4
5 --8
6 --8
So in some sense it does round to a near muliple (not power!) of 4. But not
the nearest one.
There are an endless variety of code constructs, depending on how efficient
one needs to be. Certainly
switch (n & 3)
comes to mind as a starting point.
Dave.
--
David T. Ashley (dt*@e3ft.com)
http://www.e3ft.com (Consulting Home Page)
http://www.dtashley.com (Personal Home Page)
http://gpl.e3ft.com (GPL Publications and Projects)