bitwise operator in c language | Allinonetuts

Bitwise Operator in C language

Bitwise operators of C language are the operators, using which those types of operations are performed, which is generally, performed through low-level languages. As the name indicates, using bitwise operators, The bit of any number can be manipulated. For bit manipulation, C language provides following operators:

bitwise operators | Allinonetuts

  1. Bitwise AND: It is denoted by & (ampersand).
    Example :
    int a = 4, b = 3;
    binary value of 4 = 0100
    binary value of 3 =0011
    a = 0100
    b = 0011
    a & b = 0000
  2. Bitwise OR : It is denoted by | (pipeline)
    Example :
    a = 0100
    b = 0011
    a | b = 0111
  3. Bitwise Exclusive-OR (XOR) : It is denoted by a ^ (caret).
    Example :
    a = 0100
    b = 0011
    a a b = 0111
  4. Bitwise Complement : It is denoted by  ~  (complement).
    Example :
    int a = 10, b;
    Binary value of a = 1010
    b = ~ a
    b = ~ ( 1010 )
    b = 0101
  5. Bitwise Left Shifting : It is denoted by <<  (Double less than).
    Example :
    a  <<  1, a << 2
  6. Bitwise Right Shifting : It is denoted by >> double greater than).
    Example :
    a >>  2, a >>  1

The general format of bitwise shifting is :

< variable > < shift operator > <nob >; Where, variable = an int or char element Shift operator = left shift or right shift operator nob = number of bits to be shifted.

Working of Bitwise Operator with Example

Some applications require the manipulation of individuals bits within a word of memory. Assembly language or machine language is normally required for operations of this type. However, C remains several operators that allow such bitwise operations to be carried out easily and efficiently. These bitwise operators can be divided into three general categories: The one’s complement operator, the logical bitwise operators, and assignment. C also contains several operators that combine bitwise operations with the ordinary assignment.

The One’s Complement Operator:

The one’s complement operator (-) is a unary operator that causes the bits of its operator to be -.-verted so that Is become Os and Os become Is. This operator always precedes its operand.

The Logical Bitwise Operators:

There are three logical bitwise operators: bitwise and (&), bitwise exclusive or ( ^ ) and bitwise or Each of these operators require two integer type operands. The operations are carried out independently on each pair of corresponding bits within the operands. Thus, the least significant bits within the two operands will be compared, then the next least significant bits, and so on, until all of the bits have been compared.

The result of these comparisons are:

  • A bitwise and expression will return an I if both have a value of 1. Otherwise, it will return a value of 0.
  • A bitwise exclusive or expression will return a 1 if one of the bits has a value of 1 and the other has a value ofO. Otherwise, it will return a value of 0.
  • A bitwise or expression will return a 1 if one or more of the bits have a value of 1. Otherwise, it will return a value of 0.

Example :

Suppose a and b are unsigned integer variables whose values are 0 x 6db7 and Ox a726, res p.
The result of a & b is

-a =    0 x 9248

-b =    0 x 58d9

a & b = 0x 2526

Hello Viewers,
welcome to the Allinonetuts a Technical tutorial and Programming Blog Maintained by Vikas Kardam, a Web Developer and Blogger From Delhi, India.
What is Bitwise Operator? Explain its working with Example was last modified: July 9th, 2017 by Vikas Kardam
Submit your review

Create your own review

Average rating:  
 1 reviews
by Shubham Agarwal on Bitwise Operator in C language

Nice Article written by Mr. Vikas Kardam

Leave a Reply

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