Taking a memory array [1,2,3], is it possible to us shr/shl to make the array [2,3,1]?
1 Answer
shr
and shl
operations are used for bitwise shifting of individual values which is not what you want.
You should use inline assembly to load and store values at the corresponding memory locations. Here is an example:
pragma solidity ^0.8.0;
contract ArrayShift {
function shiftArray(uint[] memory arr) public pure returns (uint[] memory) {
uint arrayLength = arr.length;
if (arrayLength > 1) {
uint firstElement = arr[0];
for (uint i = 0; i < arrayLength - 1; i++) {
arr[i] = arr[i + 1];
}
arr[arrayLength - 1] = firstElement;
}
return arr;
}
}