what is use of &(AND) operator in C language?

This question already has an answer here:

  • Understanding the bitwise AND Operator 4 answers

  • The bitwise AND operator is a single ampersand: &. A handy mnemonic is that the small version of the boolean AND, &&, works on smaller pieces (bits instead of bytes, chars, integers, etc). In essence, a binary AND simply takes the logical AND of the bits in each position of a number in binary form.

    For instance, working with a byte (the char type):

    EX.

    01001000 & 
    10111000 = 
    --------
    00001000
    

    The most significant bit of the first number is 0, so we know the most significant bit of the result must be 0; in the second most significant bit, the bit of second number is zero, so we have the same result. The only time where both bits are 1, which is the only time the result will be 1, is the fifth bit from the left. Consequently,

    72 & 184 = 8
    

    More example

       unsigned int a = 60; /* 60 = 0011 1100 */  
       unsigned int b = 13; /* 13 = 0000 1101 */
       int c = 0;           
    
       c = a & b;       /* 12 = 0000 1100 */ 
    

    & is the bitwise AND operator. It does what that sounds like - does the and operator on every bit. In your case, if p = 2^k , a[i]&p checks if the machine's binary representation of a[i] has the k-th bit set to 1 .


    AND运算符比较两个给定的输入位,如果两个位均为1,则结果为1.否则,它给出0。

    链接地址: http://www.djcxy.com/p/72644.html

    上一篇: 在iPhone上确定用户是否启用了推送通知

    下一篇: 在C语言中使用&(AND)运算符是什么?