**Definition:****A bitwise operator is an operator used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits.***Bitwise operators are used in communication stack.*

**Operations can be performed on a bit level using bitwise operators. We use bitwise operators to**

__manipulate data__at its lowest bit level.bitwise operators works on each bit of the data.

*There are six bitwise operators are in C programming:*

**1. Bitwise AND(&):**

In C takes two numbers as operands and does

__AND__on every bit of two numbers. The result of

__AND__is 1 only if both bits are 1.

[It is used to check perticular bit of data is ON(1) or OFF (0).]

Suppose we will take two intiger variables a & b, where

**a=9**and

**b=10**.

__8 bit__binary representation of a & b is...

a= 0000 1001

b= 0000 1010

It will be evaluated as-

So

**a & b**is evaluated as 0000 1000 which is

__in decimal.__

**8**

**2. Bitwise OR(|):**

In C takes two numbers as operands and does

__OR__on every bit of two numbers. The result of

__OR__is 1 if any of the two bits is 1.

[ It is commonly used to set

__flag bit__values.]

Suppose we will take two intiger variables a and b, where a=4 and b=11.

8 bit binary representation of a | b is...

a= 0000 0100

b= 0000 1011

It is evaluated as-

**So**

**a | b**is evaluated as 0000 1111 which is

**in decimal.**

__15__

**3. Bitwise XOR(^):**

In C takes two numbers as operands and does

__XOR__on every bit of two numbers. The result of

__XOR__is 1 if the two bits are different.

Suppose we will take two intiger variables a and b, where a=6 and b=13.

8 bit binary representation of a^b is....

a= 0000 0110

b= 0000 1101

It is evaluated as-

So

**a^b**is evaluated as 0000 1011 which is

**in decimal.**

__11__**4. Bitwise Complement or NOT (~):**

In C takes one number and inverts all bits of it. Means it sets bit value 1 only if corresponding bit of operand is 0.

Suppose we take intiger variable a, where a=4.

8 bit binary representation of a is-

a= 0000 0100

It is evaluated as-

So

**a~**is evaluated as 1111 1011 which is

__in decimal.__

**124**

**5. Bitwise left shift(<<):**

In C takes two numbers and

__left shift__the bits of the first operand, the second operand decides the number of places to shift.

Suppose we take an intiger variable a=15.

8 bit binary representation of a is-

a= 0000 1111

It is evaluated as-

Here,

**ex:**a<<1 means it shifts bit one times to left which is

**30**in decimal. another one example is a<<2 means it shifts bit two times to left which is

**60**in decimal.

Left shifting of bit causes insertion of zero from right.

**6. Bitwise right shift (>>):**

In C take two numbers and

__right shift__the bits of the first operand, the second operand decides the number of places to shift.

Suppose we take intiger variable a=15.

8 bit binary representation of a is-

a= 0000 1111

It is evaluated as-

Here,

**ex:**a>>1 means it shifts bit one time to right which is

**7**in decimal. another one example is a>>2 means it shifts bit two time to right which is 3 in decimals.

## No comments:

## Post a Comment

If you have any doubts, please discuss here...