Best way in D2 to rotate a ubyte[4] array

Kai Meyer kai at unixlords.com
Wed Mar 9 15:35:29 PST 2011


On 03/09/2011 03:41 PM, Tom wrote:
> What is the most efficient way of implement a rotation of ubyte[4] array?
>
> By rotation I mean: rotateRight([1, 2, 3, 4]) -> [4, 1, 2, 3]
>
> TIA,
> Tom;

I don't know of anything more efficient than:
     ubyte[4] bytes = [1,2,3,4];
     bytes = bytes[$-1] ~ bytes[0..$-1]; // Rotate left
     bytes = bytes[1..$] ~ bytes[0]; // Rotate right

Both static arrays and dynamic arrays (ubyte[] bytes = [1,2,3,4];) 
perform about the same between 1 and 10 milling rotations in either 
direction. I think a temporary array might be created for the rhs, and 
then have the values of the rhs array copied to the lhs array, but I 
don't know. With static arrays, I'm not sure there would be a way to get 
around it with out at least a temporary value for the one that's moving 
between the first and last positions.


More information about the Digitalmars-d-learn mailing list