Array cyclic displacement K-bit

Source: Internet
Author: User

from the programming Zhu Ji Nanxiong, the array is treated as a vector x=ab, so the array becomes BA when the loop is moved right. Considering from the angle of the vector(a-1b-1) -1=ba 

/* Do not use temporary variable Exchange A[begin] and A[end] Two memory unit contents */
Void stringreverse (char a[], int  Begin, int end)
{
    for (;  begin < end; begin++,  end--) {
        a[begin] ^= a[end];
        a[end] ^= a[begin];
        a[begin] ^= a[end];
    }
}

Void looprightshift (char a[], int n, int k)
{
     k %= n;
    
    stringreverse (a, 0, n-k-1);  /* first to A[0 ... N-k-1] This (n-k) character in reverse order */
    stringreverse (a, n-k, n-1);  /* again to a[n-k...n-1] This k-character is reversed */
    stringreverse (a, 0, n-1);    /* ends with the entire string in reverse order */
}

Array loop shift K-bit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.