Search Placementyogi.com
 

Bitwise Operators in C

A bit is the basic unit of information in computing and digital communications. Data are stored in memory in form of bits (0 or 1).The operator that are used to manipulate the bits are called bit wise operators.

  • Bit-wise  AND        &
  • Bit-wise OR           |
  • Bit-wise XOR         ^
  • Left shift              <<
  • Right Shift            >>
  • Bit-wise Negate    ~

Bit-wise AND "&" Operator

The bitwise AND operator is a single ampersand: & and remember it different from logical AND (&&). This operator operates on bits of opearads. Both the operands should be of same type (int, char etc.). Below table shows the operations of bit-wise AND operator

First Bit Second Bit First Bit & Second Bit
0 0 0
0 1 0
1 0 0
1 1 1

 

By looking at above truth table we can say that AND(&) of two bits is 1 only when both bits are 1 else its 0. In first 3 rows AND of both bits are 0 because either both or at least one bit is 0. In last row both bits are 1 so the result is 1.  

Example:

Lets assume, char a=1, b=3; So what will be a & b = ?  

Solution:

Binary of a = 0 0 0 0 0 0 0 1
Binary of b = 0 0 0 0 0 1 0 1
--------------------------------
      a & b = 0 0 0 0 0 0 0 1
--------------------------------

Bit-wise OR "|" Operator

Bit-wise OR (|) works exactly almost same as bit-wise AND. The only difference is that any one bit out of two should be 1 to get the result 1.

First Bit Second Bit First Bit | Second Bit
0 0 0
0 1 1
1 0 1
1 1 1

 

In above truth table only first row result is 0 because both the bits being OR'ed are 0.

For Example:

char a=1, b=3; So what will be a & b = ?  

Solution:

Binary of a = 0 0 0 0 0 0 0 1
Binary of b = 0 0 0 0 0 1 0 1
--------------------------------
      a & b = 0 0 0 0 0 1 0 1
--------------------------------

Bit-wise XOR "^" Operator

XOR operator is represented by ^ and is also called an Exclusive OR operator. It results in 0 if both the bits are same ( both 0 or both 1).

First Bit Second Bit First Bit | Second Bit
0 0 0
0 1 1
1 0 1
1 1 0

 

In above table, first and last row have both bits same that why result is 0.

For Example:

char a=1, b=3; So what will be a & b = ?  

Solution:

Binary of a = 0 0 0 0 0 0 0 1
Binary of b = 0 0 0 0 0 1 0 1
--------------------------------
      a & b = 0 0 0 0 0 1 0 0
--------------------------------

Left shift "<<" Operator

The left shift operator is represented by <<. It shifts each bit of operand to left by the number of bits specified.

Syntax: integer_constant_variable << number_of_bits_to_be_shifted ;

Example: x << 1 ;

Lets assume x=65, So x<<1 will shift each bit of x (0100 0001) by 1 bit to left and right most bit is filled with zero. See below diagram

 

Right Shift ">>" Operator

The right shift operator is represented by >>. It shifts each bit of operand to right by the number of bits specified.

Syntax: integer_constant_variable >> number_of_bits_to_be_shifted ;

Example: x >> 1 ;

Lets assume x=65, So x>>1 will shift each bit of x (0100 0001) by 1 bit to right and left most bit is filled with zero or one.if left most bit is 0 then filled with 0, if left most bit is 1 then filled with 1. See below diagram

 

Bit-wise Negate "~" Operator

It is also called one's complement of a number. On taking one's complement of a number, all 1's present in the number are changed to 0's and all 0's are changed to 1's.

For example :

1's complement of 0011 0111 is 1100 1000.

 

Banner to LogicGuns.com

Hide Page Information
Bitwise Operators in c