021-bit operation 1 (keep it up)

Source: Internet
Author: User
Given two 32-bit numbers, N and M, there are also two numbers of indication bits, I and j.
Write a program to make the value of the I-th to the J-th in n the same as that of M (that is, M becomes a substring of N and is located between the I-th and J-th in N)
Example:
Input: N = 10000000000, M = 10101, I = 2, j = 6
Output: N = 10001010100.
Solution:
First, we shift m left to VI bit to get the MV;
Then we keep the 0-to-I bits of N: SI = N & (1 <I)-1)
Then we set all the 0-j bits of N to 0: N = (n> (J + 1) <(J + 1 );

Finally, we add up the above numbers.

Int changebit (int vn, int Vm, int VI, int VJ) {If (VI> vj | Vi> 31 | VJ> 31) return 0; int move_m_ I = VM <VI; int last_n_ I = VN & (1 <VI)-1); VN = (VN> (vj + 1 )) <(vj + 1); int result = Vn | move_m_ I | last_n_ I ;}


021-bit operation 1 (keep it up)

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.