A/C + + Shift operation makes the value of an expression the same as the nth bit of a variable

Source: Internet
Author: User

1, problem description

There are two int variables A and n require that an expression be written so that the value of the expression is the same as the nth bit of a

2, not comprehensive implementation

In the microcontroller program is often seen in the implementation is

(A & (1 << N)) >> n

However, it is important to note that the implementation when n is 31, if it is a negative number, then the sign bit is 1, the right shift 31 bit after the result is not 1!

3. Correct solution

1

This method is established in the case of 0 <= N <= 31.

A/C + + Shift operation makes the value of an expression the same as the nth bit of a variable

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.