Teach you how to use bit arithmetic in C language to realize circular shift

Source: Internet
Author: User
Tags arithmetic

The cyclic shift is different from the general displacement of the displacement without the loss of digits. When the loop moves to the left, the left side of the bit padding is used to remove the right end of the word, and when the loop moves to the right, it uses the left side of the padding word that is moved from the right. This situation is used in system programs, and in some control programs.

A data description is provided:

A=01111011, loop left 2-bit correct result: 11101101

Process:

The b=a>> (8-2) is used to obtain the correct position of the normal left-shift loss and the b=00000001 after cyclic displacement.

a=a<<2; left Shift a=11101100

a=a|b; A=11101101

If not with intermediate variable a= (a>> (8-2)) | (A<<2)

Total length N (8 16 32)

Loop left N (a>> (n-n)) | (a>>n)

Loop Right N (a<< (n-n)) | (a>>n)

The bit operation function of C language is one of the distinguishing features of most advanced programming languages, and it can be used to realize some special functions, and the flexible mastery is the basis of programming system program with C program.

Related Article

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.