Take the 2-minute tour ×

Suppose I have an array

unsigned char arr[]= {0,1,2,3,4,5,6,7,8,9};

Is there a way to perform shift operation on them besides just copying them all into another array. We can easily do it using linked lists but I was wondering if we can use a shift operator and get work done faster.

Note: The data in this question is just an example. The answer should be irrecpective of the data in the array.

share|improve this question
What are you trying to accomplish, duplicating the array? –  Dave Swersky Dec 14 '09 at 20:15
Maybe it's just me, but I'm a bit unclear -- do you want to bitwise shift each element in the array, or circular shift the entire array? –  Andy Dec 14 '09 at 20:16
Are you shifting the elements the array, i.e arr[0] = arr[1], etc, or are you bitshifting each element of the array, i.e. arr[0] = arr[0] << 2? If the former, remove the "bitwise" tag. –  mch Dec 14 '09 at 20:22
@Andy: trying to achieve a circular shift –  tomkaith13 Dec 14 '09 at 20:25
add comment

6 Answers

up vote 17 down vote accepted

If you want a circular shift of the elements:

std::rotate(&arr[0], &arr[1], &arr[10]);

... will do the trick. You'll need to #include the algorithm header.

share|improve this answer
Quick link cplusplus.com/reference/algorithm/rotate –  wardw Apr 16 '13 at 19:16
add comment

As long as the array is modifiable, you can use memmove to shift them (but don't mistakenly use memcpy as memcpy is not meant for overlapping regions):

memmove(&arr[0], &arr[1], sizeof(arr) - sizeof(*arr));

(sizeof(arr) - sizeof(*arr) is the size in bytes of all but 1 element of the array).

share|improve this answer
This completely loses the value of arr[0] - for a circular shift you have to save that value and store it in the last element after the memmove. –  caf Dec 14 '09 at 22:38
sweet and compact answer –  ssj Apr 18 '11 at 5:59
add comment

If you're the only person with a pointer to the array, just increment the pointer and decrement the length.

Just remember to keep the original pointer around for when you free it.

share|improve this answer
add comment

If you're looking for a pure C solution, here it is, including a driver program. It turns out to be pretty simple: to rotate by n, you:

  1. reverse the first n elements in-place,
  2. reverse the remaining elements in-place, and
  3. reverse the whole array in-place.

This requires one element worth of extra storage (for reversing).

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

/* print an array */
static void print_array(unsigned char *arr, size_t n, const char *prefix)
    size_t i;

    if (prefix) {
        printf("%s: ", prefix);
        printf("%02x ", (unsigned int)arr[i]);

/* reverse 'arr', which has 'narr' elements */
static void reverse(unsigned char *arr, size_t narr)
    size_t i;

    for (i=0; i < narr / 2; ++i) {
        unsigned char tmp = arr[i];
        arr[i] = arr[narr-i-1];
        arr[narr-i-1] = tmp;

/* rotate 'arr' of size 'narr' by 'shift' */
static void rotate(unsigned char *arr, size_t narr, unsigned long shift)
    reverse(arr, shift);
    reverse(arr + shift, narr - shift);
    reverse(arr, narr);

/* driver program */
int main(int argc, char *argv[])
    unsigned char arr[]= {0,1,2,3,4,5,6,7,8,9,10};
    size_t narr = sizeof arr / sizeof arr[0];
    unsigned long shift = 2;

    if (argc > 1) {
        char *eptr;
        shift = strtoul(argv[1], &eptr, 0);
        if (*eptr || errno == ERANGE) {
            return EXIT_FAILURE;
    print_array(arr, narr, "before shift");
    rotate(arr, narr, shift);
    print_array(arr, narr, "after shift");
    return EXIT_SUCCESS;
share|improve this answer
@Alok: Thanks for this man ... looks really helpful –  tomkaith13 Dec 15 '09 at 19:19
add comment

If you really want BLAZING speed look at the assembler shift with carry operations. http://en.wikibooks.org/wiki/X86%5FAssembly/Shift%5Fand%5FRotate#Shift%5FWith%5FCarry%5FInstructions Coupled with a loop you can bitshift an array in milliseconds.

share|improve this answer
@Jay: Thanks for that.. will keep this in mind –  tomkaith13 Dec 14 '09 at 20:46
milliseconds!? ouch –  ysth Dec 15 '09 at 6:07
add comment

I wonder if perhaps you should be using a std::valarray.

share|improve this answer
add comment

Your Answer


