Bitwise Operators

Bitwise Operators are used for the manipulations at a bit level. The bitwise operators in C are as follows :

  • BITWISE AND (&) :

    This gives the numbers by performing AND on the each corresponding bit of both the operands.
    AND gives 1 ,only when both the bits are 1 otherwise 0.
    e.g.

    Output:

    Binary representations:
    (A)2=00010100
    (B)2=00101000
    AND of corresponding digits gives
    (C)2=00000000

  • BITWISE OR(|) :

    This gives the numbers by performing OR on the each corresponding bit of both the operands.
    OR gives 1 ,when atleast one of the bits is 1 ,otherwise 0.
    e.g.

    Output:

    Binary representations:
    (A)2=00010100
    (B)2=00101000
    OR of corresponding digits gives
    (C)2=00111100
    (C)10=60

  • BITWISE XOR(^)

    : This gives the numbers by performing XOR on the each corresponding bit of both the operands.
    XOR gives 1 ,when both the bits are opposite and 0, when both bits are different.
    e.g.

    Output:

    Binary representations:
    (A)2=00010100
    (B)2=00101000
    XOR of corresponding digits gives
    (C)2=00111100
    (C)10=60

  • BITWISE COMPLEMENT(~) :

    It gives the complement of any number
    But in c, we get the output as ,2’s Complement of the complement of a number because we get the signed binary no. 2’s Complement of a no. is complementing digits of a number + 1.Generally,  ~n=-(n+1)
    e.g.

    Output:

    Binary representations:
    (C)2=00100111
    Bitwise complement of C gives
    (C’)2=11011000
    (C’)10=216
    2’s complement of 216=00100111+1=00101000=-40

    Thus, Final answer is -40

  • LEFT SHIFT(<<) and RIGHT SHIFT(>>) :

    This shifts the bit of an operand by the bits specified by right operand towards left and right respectively.e.g.

    Output:

    (A)2=00100011

    Left shift by 2bits : 10001100

    Right shift by 2 bits :00001000

Leave a Reply

Your email address will not be published. Required fields are marked *