2-9按位运算符

位运算符

学到这我就很纠结了,因为位运算符,还需要电子电路的常识。 diandidianlu

如果你不是造轮子的话,一般用不到。

但是看过很多API的内部实现的话,会大量发现位运算,比如最经典的用“>>1”代替“/2”,因为API要被大量调用能提高性能,当然在造“汽车”的工程当中因为可读性差不用。还有很多非专业人员看不懂的特殊的算法实现,比如加密解密算法、哈希算法、置位等等,都要用到位运算。

什么叫好处是什么,从上古时代还没有你们这些花花绿绿的工具的时候就有位运算了,计算机归根结底是用二进制位运算而不是字节,为了数学运算方便才把二进制位分组成字节的,可是又不是所有的逻辑都是数学运算啊,布尔代数也很重要的。把打包到字节里面的二进制位取出来操作的运算就是位运算。 你们用的IP地址、子网掩码,这都是位运算啊;标志位字段,留了一堆bit存不同的标志,没用完的将来还能扩展;压缩编码这些没对齐到字节边界的,你不用位运算做一个试试……你们现在用的这套东西哪个底层没有位运算支撑起来的,把你们一个个惯得眼界狭窄自以为是,还说位运算没有用,所以说你们这些JAVA程序员啊……

C语言提供了6个位操作运算符。这些运算符只能作用于整型操作数,即只能作用于带符号或无符号的char,short ,int 与long类型:

& 按位与(and)

| 按位或(or

^ 按位异或(xor)

<<左移

/>>右移

~ 按位求反

bitwist operator

what are bitwist operators

bit masking

位掩码

common use

Logical_conjunction

NOT[edit]

The bitwise NOT, or complement, is a unary operation that performs logical negation on each bit, >forming the ones' complement of the given binary value. Bits that are 0 become 1, and those that >are 1 become 0. For example:

    NOT 0111  (decimal 7)
      = 1000  (decimal 8)

AND[edit]

A bitwise AND takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits, by multiplying them. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1 (1 × 1 = 1); otherwise, the result is 0 (1 × 0 = 0 and 0 × 0 = 0). For example:

        0101 (decimal 5)
    AND 0011 (decimal 3)
      = 0001 (decimal 1)

结果非0,就得到5不能整除3,为奇数 由于该属性,通过检查最低值位的值来检查二进制数的奇偶性变得容易

    0110 (decimal 6)
AND 0001 (decimal 1)
  = 0000 (decimal 0)

Because 6 AND 1 is zero, 6 is divisible by two and therefore even

OR[edit]

A bitwise OR takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of corresponding bits. The result in each position is 0 if both bits are 0, while otherwise the result is 1. For example: 0101 (decimal 5) OR 0011 (decimal 3) = 0111 (decimal 7) The bitwise OR may be used to set to 1 the selected bits of the register described above. For example, the fourth bit of 0010 (decimal 2) may be set by performing a bitwise OR with the pattern with only the fourth bit set: 0010 (decimal 2) OR 1000 (decimal 8) = 1010 (decimal 10)

如果两个位都为0,则每个位置的结果为0,否则结果为1

XOR[edit]

A bitwise XOR takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. In this we perform the comparison of two bits, being 1 if the two bits are different, and 0 if they are the same. For example:

          0101 (decimal 5)
      XOR 0011 (decimal 3)
        = 0110 (decimal 6)```

如果只有第一位为1或只有第二位为1,则每个位置的结果为1,但如果两者都为0或两者都为1,则结果为0

左移 右移 这个可以多用用

  int main(){
    unsigned char x='3';  // 这是51
    x = x<<2;
    printf("x=%d",x);  #位往左前进了2位,值位204

}```

~按位求反 这个很简单,首先十进制数7的2进制是1010,按位求反就是0101,二进制的101转换成10进制就是5,因此答案是5 (7)10=(1010)2 按位求反后得0101 (101)2=(5)10

results matching ""

    No results matching ""