• 说下按位异或(^)和按位与(&)的一点知识(没技术含量的)


    先看下面这段代码和结果

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace 双目运算符
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Console.WriteLine(
    "按位异或\"^\",结果是:{0}"100 ^ 5);
                Console.WriteLine(
    "按位异或\"^\",结果是:{0}"5 ^ 5);
                Console.WriteLine(
    "按位与\"&\",结果是:{0}"100 & 5);
                Console.WriteLine(
    "按位与\"&\",结果是:{0}"5 & 5);
                Console.ReadKey();
            }
        }
    }

     结果是:

     

    好了,现在看下MSDN是怎么说的。

    二元 ^ 运算符是为整型和 bool 类型预定义的。对于整型,^ 将计算操作数的按位“异或”。对于 bool 操作数,^ 将计算操作数的逻辑“异或”;也就是说,当且仅当只有一个操作数为 true 时,结果才为 true 

     & 运算符(C# 参考)

    一元 & 运算符返回操作数的地址(要求 unsafe 上下文)。

    为整型和 bool 类型预定义了二进制 & 运算符。对于整型,& 计算操作数的逻辑按位“与”。对于 bool 操作数,& 计算操作数的逻辑“与”;也就是说,当且仅当两个操作数均为 true 时,结果才为 true 

    代码
                Console.WriteLine("按位异或\"^\",结果是:{0}"100 ^ 5);
                
    /*
                 100的二进制是:01100100
                   5的二进制是:00000101
                     异或后得:01100001  异或(可以理解为:不一样为1,一样为0)
                      转换为十进制就是:97
                 
    */
                Console.WriteLine(
    "按位异或\"^\",结果是:{0}"5 ^ 5);//两个相同就是0


                Console.WriteLine(
    "按位与\"&\",结果是:{0}"100 & 5);
                
    /*
                 100的二进制是:01100100
                   5的二进制是:00000101
                    按位与后得:00000100  按位与(只同时为1时才为1)
                      转换为十进制就是:4
                 
    */
                Console.WriteLine(
    "按位与\"&\",结果是:{0}"5 & 5);//为自己 

    呵呵,更多请自己看MSDN,很详细。 

  • 相关阅读:
    smtplib.py
    淘宝链接中的spm参数
    with 上下文管理
    python RecursionError: maximum recursion depth exceeded while calling
    GraphQL两年实战
    Exception 异常处理
    Simple decorator that intercepts connection errors and ignores these if settings specify this.
    namedtuple
    服务治理在猫眼娱乐的演进之路
    路由、限流、熔断 微服务治理
  • 原文地址:https://www.cnblogs.com/SeaSun/p/1631736.html
Copyright © 2020-2023  润新知