Home > Backend Development > C#.Net Tutorial > Use of bitwise negation operator

Use of bitwise negation operator

藏色散人
Release: 2019-06-03 14:21:49
Original
8847 people have browsed it

Use of bitwise negation operator

The use of bitwise negation operator

The bitwise negation operator is based on each binary Bit inversion, such as byte type, the result of ~0 is 255.

This function can do some reversal operations in the mask

In the following code, a stores three values ​​​​2, 4, and 8. Use the bitwise negation '~' operator to reverse

The printed result is false,flase,false,true,true. 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();
    }
}
Copy after login

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;
    }
}
Copy after login

Use of bitwise negation operator

The above is the detailed content of Use of bitwise negation operator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template