C # & #39 ;~ & #39; use of bitwise inverse Operators,

Source: Internet
Author: User

C #'~ 'Use of bitwise anti-Operators,

Bitwise inversion operator is the inverse of each bitwise of binary, such as byte type ,~ The result of 0 is 255.

This feature allows you to perform some reverse operations in the mask.

 

The following code stores values of 2, 4, and 8 in. Use bitwise inversion '~ 'Operator Inversion

The print result is false, flase, false, true, or true. The Mask has been reversed.

Class Program {static void Main (string [] args) {byte a = 2 | 4 | 8; byte B = (byte )~ A; Console. writeLine (B & 2) = 2); Console. writeLine (B & 4) = 4); Console. writeLine (B & 8) = 8); Console. writeLine (B & 16) = 16); Console. writeLine (B & 32) = 32); Console. read ();}}

 

For example, in the unity engine, this operation can be used on LayerMask:

Public class LayerMaskTest: MonoBehaviour {public LayerMask layerMask; void OnEnable () {layerMask. value = ~ LayerMask. value ;}}

 

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.