As a result, I got 2 functions:
Shift left
public static byte[] ShiftLeft(byte[] src, int val) { var res = new byte[src.Length]; var byteShift = val >> 3; if (byteShift >= src.Length) return res; val ^= byteShift << 3; for (var i = 1; i < src.Length - byteShift; i++) res[i + byteShift] = (byte)((src[i] << val) | src[i - 1] >> 8 - val); res[byteShift] = (byte)(src[0] << val); return res; }
And right shift
public static byte[] ShiftRight(byte[] src, int val) { var res = new byte[src.Length]; var byteShift = val >> 3; if (byteShift >= src.Length) return res; val ^= byteShift << 3; for (var i = byteShift; i < src.Length - 1; i++) res[i - byteShift] = (byte)((src[i] >> val) | src[i + 1] << 8 - val); res[src.Length - byteShift - 1] = (byte)(src[src.Length - 1] >> val); return res; }
Functions "as is" suggest proper use. Speed ββis very important to me, so I did not check for negative values. Ideally, a negative left shift is a right shift by the same number modulo.
It was also possible to add checks for zero shift, a shift multiple of 8, and so on. Perhaps this will speed up the overall process, but, as practice has shown, such cases are less frequent than in every eighth shift, and each condition and operation adds additional execution time.
And yes, Regent said something about byte order. I did not begin to understand this, because the option that exists now completely suits me. If you get a set of bytes from a number of type long and perform a shift with my function, you will get the same result as if you first perform the same shift for a number, and then get a set of bytes from the result. I get a set of bytes from a number with the BitConverter.GetBytes(theLong) function
At the moment, on my computer, my functions perform 10 million shifts for 700-900 ms with a shift value of 0-7 and time is inversely proportional to the shift value divided by 8. That is, the same number of shifts by 8-15 will be faster than 0- 7, and at 16-23 even faster and so on ...
UPD
The specified execution time is for Debug. In the release, everything works 3-4 times faster.