• 位运算符【&和|】在C#中的运行结果


    本文讨论C#中bool类型在位运算符 &(与运算) 和 |(或运算)的结果

    &(与运算)在数值中运算的过程是先把 数字 转换成二进制, 同位均为1 则该位为1
    |(或运算)在数值中运算的过程是先把 数字 转换成二进制, 同位中有1 则该位为1

                bool bol1 = false, bol2 = false;
    
                bool Result = bol1 & bol2;
                if (Result == true)
                {
                    Debug.WriteLine("False & False = True");
                }
                if (Result == false)
                {
                    Debug.WriteLine("False & False = False");
                }
    
                Result = bol1 | bol2;
                if(Result==true)
                {
                    Debug.WriteLine("False | False = True");
                }
                if (Result == false)
                {
                    Debug.WriteLine("False | False = False");
                }
    
                bol1 = false; bol2 = true;
    
                Result = bol1 & bol2;
                if (Result == true)
                {
                    Debug.WriteLine("False & True = True");
                }
                if (Result == false)
                {
                    Debug.WriteLine("False & True = False");
                }
    
    
                Result = bol1 | bol2;
                if (Result == true)
                {
                    Debug.WriteLine("False | True = True");
                }
                if (Result == false)
                {
                    Debug.WriteLine("False | True = False");
                }
    
    
                bol1 = true; bol2 = false;
    
                Result = bol1 & bol2;
                if (Result == true)
                {
                    Debug.WriteLine("True & False = True");
                }
                if (Result == false)
                {
                    Debug.WriteLine("True & False = False");
                }
    
                Result = bol1 | bol2;
                if (Result == true)
                {
                    Debug.WriteLine("True | False = True");
                }
                if (Result == false)
                {
                    Debug.WriteLine("True | False = False");
                }
    
    
                bol1 = true; bol2 = true;
                Result = bol1 & bol2;
                if (Result == true)
                {
                    Debug.WriteLine("True & True = True");
                }
                if (Result == false)
                {
                    Debug.WriteLine("True & True = False");
                }
    
                Result = bol1 | bol2;
                if (Result == true)
                {
                    Debug.WriteLine("True | True = True");
                }
                if (Result == false)
                {
                    Debug.WriteLine("True | True = False");
                }
    

    VS 中OutPut输出结果

    False & False = False
    False | False = False
    False & True = False
    False | True = True
    True & False = False
    True | False = True
    True & True = True
    True | True = True
    

    继续探讨,如果不要用变量能够在If中时用吗,代码如下

            bool bol1 = false, bol2 = false;
    
            if (bol1 & bol2 == true)
            {
                Debug.WriteLine("if(False & False = True)    CondictionExecuted");
            }
            if (bol1 & bol2 == false)
            {
                Debug.WriteLine("if(False & False = False)    CondictionExecuted");
            }
    
            if (bol1 | bol2 == true)
            {
                Debug.WriteLine("if(False | False = True)      CondictionExecuted");
            }
            if (bol1 | bol2 == false)
            {
                Debug.WriteLine("if(False | False = False)      CondictionExecuted");
            }
    
            bol1 = false; bol2 = true;
            if (bol1 & bol2 == true)
            {
                Debug.WriteLine("if(False & True = True)        CondictionExecuted");
            }
            if (bol1 & bol2 == false)
            {
                Debug.WriteLine("if(False & True = False)       CondictionExecuted");
            }
    
            
            if (bol1 | bol2 == true)
            {
                Debug.WriteLine("if(False | True = True)        CondictionExecuted");
            }
            if (bol1 | bol2 == false)
            {
                Debug.WriteLine("if(False | True = False)       CondictionExecuted");
            }
    
    
            bol1 = true; bol2 = false;
            if (bol1 & bol2 == true)
            {
                Debug.WriteLine("if(True & False = True)        CondictionExecuted");
            }
            if (bol1 & bol2 == false)
            {
                Debug.WriteLine("if(True & False = False)       CondictionExecuted");
            }
    
    
            if (bol1 | bol2 == true)
            {
                Debug.WriteLine("if(True | False = True)        CondictionExecuted");
            }
            if (bol1 | bol2 == false)
            {
                Debug.WriteLine("if(True | False = False)       CondictionExecuted");
            }
    
    
            bol1 = true; bol2 = true;
            if (bol1 & bol2 == true)
            {
                Debug.WriteLine("if(True & True = True)         CondictionExecuted");
            }
            if (bol1 & bol2 == false)
            {
                Debug.WriteLine("if(True & True = False)        CondictionExecuted");
            }
            
            if (bol1 | bol2 == true)
            {
                Debug.WriteLine("if(True | True = True)         CondictionExecuted");
            }
            if (bol1 | bol2 == false)
            {
                Debug.WriteLine("if(True | True = False)        CondictionExecuted");
            }
    

    此时VS OutPut窗口输出:多么痛的领悟。。。。。

    if(True | False = True) CondictionExecuted
    if(True | False = False) CondictionExecuted
    if(True | True = True) CondictionExecuted
    if(True | True = False) CondictionExecuted

    if(False | False = False)      CondictionExecuted
    if(False | True = True)        CondictionExecuted
    if(True & False = False)       CondictionExecuted
    if(True | False = True)        CondictionExecuted
    if(True | False = False)       CondictionExecuted
    if(True & True = True)         CondictionExecuted
    if(True | True = True)         CondictionExecuted
    if(True | True = False)        CondictionExecuted
    

    上面不是&运算符符合逻辑码, 接下来对3个bool进行&运算,结果很是失望!

        bool bol1, bol2, bol3;
               // 全为True
          bol1 = bol2 = bol3 = true;
          if (bol1 & bol2 & bol3 == true)
          {
              Debug.WriteLine("True & True & True = True!");
          }
          if (bol1 & bol2 & bol3 == false)
          {
              Debug.WriteLine("True & True & True = False!");
          }
         
    
          //1个为False
          bol1 = false; bol2 = true; bol3 = true;
          if (bol1 & bol2 & bol3 == true)
          {
              Debug.WriteLine("False & True & True = True!");
          }
          if (bol1 & bol2 & bol3 == false)
          {
              Debug.WriteLine("False & True & True = False!");
          }
          bol1 = true; bol2 = false; bol3 = true;
          if (bol1 & bol2 & bol3 == true)
          {
              Debug.WriteLine("True & False & True = True!");
          }
          if (bol1 & bol2 & bol3 == false)
          {
              Debug.WriteLine("True & False & True = False!");
          }
    
          bol1 = true; bol2 = true; bol3 = false;
          if (bol1 & bol2 & bol3 == true)
          {
              Debug.WriteLine("True & True & False = True!");
          }
          if (bol1 & bol2 & bol3 == false)
          {
              Debug.WriteLine("True & True & False = False!");
          }
    
          //2个False
          bol1 = false; bol2 = false; bol3 = true;
          if (bol1 & bol2 & bol3 == true)
          {
              Debug.WriteLine("False & False & True = True!");
          }
          if (bol1 & bol2 & bol3 == false)
          {
              Debug.WriteLine("False & False & True = False!");
          }
    
          bol1 = false; bol2 = true; bol3 = false;
          if (bol1 & bol2 & bol3 == true)
          {
              Debug.WriteLine("False & True & False = True!");
          }
          if (bol1 & bol2 & bol3 == false)
          {
              Debug.WriteLine("False & True & False = False!");
          }
    
          bol1 = true; bol2 = false; bol3 = false;
          if (bol1 & bol2 & bol3 == true)
          {
              Debug.WriteLine("True & False & False = True!");
          }
          if (bol1 & bol2 & bol3 == false)
          {
              Debug.WriteLine("True & False & False = False!");
          }
    
          //全为False
          bol1 = false; bol2 = false; bol3 = false;
          if (bol1 & bol2 & bol3 == true)
          {
              Debug.WriteLine("False & False & False = True!");
          }
          if (bol1 & bol2 & bol3 == false)
          {
              Debug.WriteLine("False & False & False = False!");
          }
    

    输出结果如下,这结果很是肯爹啊,一定有其他原因

    True & True & True = True!
    True & True & False = False!
    

    然后脑海中又有了运算符优先级的问题

        bool bol1, bol2, bol3;
        // 全为True
        bol1 = bol2 = bol3 = true;
        if ((bol1 & bol2 & bol3) == true)
        {
            Debug.WriteLine("(True & True & True) = True!");
        }
        if ((bol1 & bol2 & bol3) == false)
        {
            Debug.WriteLine("(True & True & True) = False!");
        }
    
    
        //1个为False
        bol1 = false; bol2 = true; bol3 = true;
        if ((bol1 & bol2 & bol3) == true)
        {
            Debug.WriteLine("(False & True & True) = True!");
        }
        if ((bol1 & bol2 & bol3) == false)
        {
            Debug.WriteLine("(False & True & True) = False!");
        }
        bol1 = true; bol2 = false; bol3 = true;
        if ((bol1 & bol2 & bol3) == true)
        {
            Debug.WriteLine("(True & False & True) = True!");
        }
        if ((bol1 & bol2 & bol3) == false)
        {
            Debug.WriteLine("(True & False & True) = False!");
        }
    
        bol1 = true; bol2 = true; bol3 = false;
        if ((bol1 & bol2 & bol3) == true)
        {
            Debug.WriteLine("(True & True & False) = True!");
        }
        if ((bol1 & bol2 & bol3) == false)
        {
            Debug.WriteLine("(True & True & False) = False!");
        }
    
        //2个False
        bol1 = false; bol2 = false; bol3 = true;
        if ((bol1 & bol2 & bol3) == true)
        {
            Debug.WriteLine("(False & False & True) = True!");
        }
        if ((bol1 & bol2 & bol3) == false)
        {
            Debug.WriteLine("(False & False & True) = False!");
        }
    
        bol1 = false; bol2 = true; bol3 = false;
        if ((bol1 & bol2 & bol3) == true)
        {
            Debug.WriteLine("(False & True & False) = True!");
        }
        if ((bol1 & bol2 & bol3) == false)
        {
            Debug.WriteLine("(False & True & False) = False!");
        }
    
        bol1 = true; bol2 = false; bol3 = false;
        if ((bol1 & bol2 & bol3) == true)
        {
            Debug.WriteLine("(True & False & False) = True!");
        }
        if ((bol1 & bol2 & bol3) == false)
        {
            Debug.WriteLine("(True & False & False) = False!");
        }
    
        //全为False
        bol1 = false; bol2 = false; bol3 = false;
        if ((bol1 & bol2 & bol3) == true)
        {
            Debug.WriteLine("(False & False & False) = True!");
        }
        if ((bol1 & bol2 & bol3) == false)
        {
            Debug.WriteLine("(False & False & False) = False!");
        }
    

    Visual Studio输出如下,符合预期:

    (True & True & True) = True!
    (False & True & True) = False!
    (True & False & True) = False!
    (True & True & False) = False!
    (False & False & True) = False!
    (False & True & False) = False!
    (True & False & False) = False!
    (False & False & False) = False!
    

    然后修改了上面 if(bool 位运算符 bool == true)格式代码为if((bool 位运算符 bool) == true)

        bool bol1 = false, bol2 = false;
        if ((bol1 & bol2) == true)
        {
            Debug.WriteLine("if((False & False) = True)    CondictionExecuted");
        }
        if ((bol1 & bol2) == false)
        {
            Debug.WriteLine("if((False & False) = False)    CondictionExecuted");
        }
    
        if ((bol1 | bol2) == true)
        {
            Debug.WriteLine("if((False | False) = True)      CondictionExecuted");
        }
        if ((bol1 | bol2) == false)
        {
            Debug.WriteLine("if((False | False) = False)      CondictionExecuted");
        }
    
        bol1 = false; bol2 = true;
        if ((bol1 & bol2) == true)
        {
            Debug.WriteLine("if((False & True) = True)        CondictionExecuted");
        }
        if ((bol1 & bol2) == false)
        {
            Debug.WriteLine("if((False & True) = False)       CondictionExecuted");
        }
    
    
        if ((bol1 | bol2) == true)
        {
            Debug.WriteLine("if((False | True) = True)        CondictionExecuted");
        }
        if ((bol1 | bol2) == false)
        {
            Debug.WriteLine("if((False | True) = False)       CondictionExecuted");
        }
    
    
        bol1 = true; bol2 = false;
        if ((bol1 & bol2) == true)
        {
            Debug.WriteLine("if((True & False) = True)        CondictionExecuted");
        }
        if ((bol1 & bol2) == false)
        {
            Debug.WriteLine("if((True & False) = False)       CondictionExecuted");
        }
    
    
        if ((bol1 | bol2) == true)
        {
            Debug.WriteLine("if((True | False) = True)        CondictionExecuted");
        }
        if ((bol1 | bol2) == false)
        {
            Debug.WriteLine("if((True | False) = False)       CondictionExecuted");
        }
    
    
        bol1 = true; bol2 = true;
        if ((bol1 & bol2) == true)
        {
            Debug.WriteLine("if((True & True) = True)         CondictionExecuted");
        }
        if ((bol1 & bol2) == false)
        {
            Debug.WriteLine("if((True & True) = False)        CondictionExecuted");
        }
    
        if ((bol1 | bol2) == true)
        {
            Debug.WriteLine("if((True | True) = True)         CondictionExecuted");
        }
        if ((bol1 | bol2) == false)
        {
            Debug.WriteLine("if((True | True) = False)        CondictionExecuted");
        }
    

    VS此时输出如下,果然是运算符的问题

    if((False & False) = False)    CondictionExecuted
    if((False | False) = False)      CondictionExecuted
    if((False & True) = False)       CondictionExecuted
    if((False | True) = True)        CondictionExecuted
    if((True & False) = False)       CondictionExecuted
    if((True | False) = True)        CondictionExecuted
    if((True & True) = True)         CondictionExecuted
    if((True | True) = True)         CondictionExecuted
    

    位运算符&和|的运算结果能够给另一个变量, 但是放到if可能会有问题(已知运算符优先级),所以If要用&&和||出错的概率比较少

    bool和int可以直接相互转换
    false强转int 值就是0
    true 强转int 值就是1

    C#中如何将bool转Int32

      bool b01 = true;
      Console.WriteLine(Convert.ToInt32(b01));
      b01 = false;
      Console.WriteLine(Convert.ToInt32(b01));
    

    没有标记转载的情况下,如果有更好的优化麻烦电邮1808937496@qq.com或者留言答复,Thank You
  • 相关阅读:
    网摘:微软暗示将推出iPad版本Office
    Android的缘分和FarMap新版发布
    过年发生的,WinM7推出,MeeGo诞生,iPhone香肠
    gPhone体验和思考(一)
    位图的像素操作练习(20120525)
    远图 安致版上线(FarMap Android)
    iPhone的创意,iPhone应用的覆盖
    201871010101陈来弟《面向对象程序设计(java)》第一周学习总结 201871010101
    sdk&mfc&vcl
    ASP.NET中的Web.config详解
  • 原文地址:https://www.cnblogs.com/wandia/p/14807591.html
Copyright © 2020-2023  润新知