"C #" about the use of left/right shift operators

Source: Internet
Author: User

Spit Groove First ~ Why my teacher didn't teach me this stuff in college--

Continue to send chestnuts:

such as "(1+2) <<3"

You guess it's a few ~

Debug.Log ((1+2) <<3) After the output is "24".

Why?

' Cause yes '

The "left shift/Right shift" refers to the corresponding binary to shift left, the execution of 1+2=3, it becomes the 3<<3. That is: Move the 3 corresponding binary to the left 3 bits.

3 corresponding binary: 11 shift to the left 3 bits into 11000

Binary 11000 is not a decimal 24 ~ ~ ~ "

Keep expanding your mind.

3>>1<<1 =? The average person will want to move to the right and left to move back, the result must be 3 will not change AH ~ ~ ~ True • Pattern Tucson broken!

If you count it, you'll find:

Once a number is moved to the right, the binary's last digit, whether 1 or 0, is meaningless because it's gone. Then, moving to the left one, the magical and added one again with 0 fill up.

So, 3 becomes 2 binary "11" to move right one to "1", then move left one becomes "10", that is 2.

Oh, in view of this moment accidentally found this big secret, and then ask for less than equal to a number of even, you can write like this ~

Of course, you can continue to open the brain hole to expand, until you play bad it.

"C #" about the use of left/right shift operators

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.