Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Each element of c# array is shifted back one bit, for example, {123456}, and the output is {6 12345}?
Each element of c# array is shifted back one bit, for example, {123456}, and the output is {6 12345}?
It can be implemented with the following code:

Explanation:

First, define an integer array arr and assign it to {1, 2, 3, 4, 5, 6}. Then, save the last element into the temporary variable temp.

Next, each element is iterated through a for loop, starting from the end of the array, and the value of the current element is set to the value of the previous element. The reverse loop is needed here, because if you loop in sequence, it will happen that each element is assigned to the element before it.

Finally, set the value of the first element of the array to temp, which is the value of the last element of the original array.

Extended example:

For example, you can consider a more general case, that is, the operation of moving any number of digits. For any given integer n, moving each element in the array back by n bits can be achieved as follows:

This allows you to arbitrarily specify the number of digits to move backwards.