位运算

作者: JuMinggniMuJ | 来源:发表于2022-11-22 10:53 被阅读0次
    1.按位与
    // GetBitsAnd 按位与:都为1  (0101&0011=0001)
    //0101
    //0011
    //0001
    func GetBitsAnd(a , b uint) uint {
        return a & b
    }
    
    2.按位或
    // GetBitsOr 按位或:至少一个1  (0101&0011=0111)
    //0101
    //0011
    //0111
    func GetBitsOr(a , b uint) uint {
        return a | b
    }
    
    3.按位亦或
    // GetBitsXor 按位亦或:只有一个1  (0101&0011=0110)
    //0101
    //0011
    //0110
    func GetBitsXor(a , b uint ) uint {
        return a ^ b
    }
    
    4.按位取反
    // GetBitsNot 按位取反(1元)  (^0011=1100)
    //0011
    //1100
    func GetBitsNot(a uint ) uint {
        return ^a
    }
    
    5.按位清除
    // GetBitsAndNot 按位清除  (0110&1011=0100)
    //0110
    //1011
    //0100
    func GetBitsAndNot(a , b uint ) uint {
        return a &^ b
    }
    
    6.左位移
    // GetLeftShift 左位移  (0001<<3=1000)
    //0001
    //3
    //1000
    func GetLeftShift(a ,b uint) uint {
        return a << b
    }
    
    7.右位移
    // GetRightShift 右位移  (1000>>3=0001)
    //1000
    //3
    //0001
    func GetRightShift(a ,b uint) uint {
        return a >> b
    }
    

    相关文章

      网友评论

          本文标题:位运算

          本文链接:https://www.haomeiwen.com/subject/uapoxdtx.html