## Bitwise Operators

On most occasions we need to deal with operations on bits. A bit is either a 0 or 1(Binary).Now think that there is an 8bit char(Chars are always 8bits), you need to modify the first bit, you can't "char a[1] = 0;".Now this is where we need to use bitwise operations.Specially in hardware ,there are lot to deal with bits.These operators compare bit by bit.

Take an 8bit variable, char.

01111010 -This is an eight bit variable.The number of binary digits the variable can hold give the number of bits

Here we have 8 binary numbers so there are 8 bits.

For now, consider we need to flip the bits of this variable,then we need to perform a bitwise operation ,we will use One's Compliment Operator, "~".

var = 01111010

~var = 10000101

Compare the result ,the operator "~" just flips the binary values.Let's look in detail on these operators now. Don't worry if nothing goes into your brain early on but you will certainly understand later on.

Now that we have completed the preliminaries, lets take a look at the available bitwise operators in C.

& Bitwise AND Operator

| Bitwise OR Operator

^ Bitwise Exclusive OR Operator(XOR)

~ One's Complement

<< Left Shift

>> Right Shift

~10011101

~abc

abc & abc

abc | abc

When two values are ANDed in C are compared bit by bit.There needs to be two operands in AND operator. The BItwise AND operator is denoted by the symbol "&" in C. When the two operands given to an AND both contain 1's in their corresponding bit position, the final result will be 1, rest all give out 0.You can understand easily in the example:

0 AND 0 = 0

0 AND 1 = 0

1 AND 0 = 0

1 AND 1 = 1

Result = Operand_1 & Operand_2;

1111001001110100 - Operand_1

0011010101100101 - Operand_2

----------------------

0011000001100100 - Result after ANDing Operand 1 and 2.

Observe the examples operands bits and compare it with the bit below it.We observe that where there is 1 and 1 at corresponding bit positions result in 1 where as the rest will have the result as 0.

Bitwise AND and Logical AND are different, you cannot use a logical AND in place of bitwise AND ,same way you cannot use a bitwise AND instead of Logical AND.

This is similar to Bitwise AND but a tiny difference.Here all bits change to 1 if 1 is present in any of the operands.Only when the both the corrosponding bits are 0 they result in 0.This bitwise operator is denoted by the symbol "|".Just a single one not two.Two would make up to Logical OR("||").

Result = Operand_1 | Operand_2;

1111001001110100 - Operand_1

0011010101100101 - Operand_2

----------------------

1111011101110101 - Result after ORing Operand 1 and 2.

Observe the example and compare bit by bit.We observe where both the corrosponding bits are 0 and 0 lead to a 0 whereas where there is a 1 there will be a result

As same as the above but this time 1 and 1 also makes a 0.Simple!!.It is denoted by the symbol "^".Exclusive OR is also referred as XOR.

1 XOR 1 = 0

1 XOR 0 = 1

0 XOR 1 = 1

0 XOR 0 = 0

Result = Operand_1 ^ Operand_2;

1111001001110100 -Operand_1

0011010101100101 -Operand_2

----------------------

1100011100010001 -Result after XORing Operand 1 and 2.

Again observe and compare bit by bit.You observe that where both the corrosponding bits are same then that results in an zero and the rest corresponding bits having different binary value result in 1.

This is an unary operator as it has only one operand.This operator just flips the binary values of the bits.1 changes to 0 and 0 changes to 1.This operator is denoted by by the symbol "~".

1 = 0

0 = 1

Result = ~Operand_1;

1000100110101001 -Operand_1

0111011001010110 -Result after ones complimenting Operand_1

Huh! Again ,Observe and compare bit by bit.We can see that the binary value of the bit is just flipped.

Did you think of what would happen if one operand was of 32bits and other of 16bits?

When a bitwise operation is performed between two values that are differnet in thier size, the system aligns the operands on the right.If the shorter of the two data is a signed variable(A Variable which can be negative as well as positive is known as signed variable),and the value is negative , then the sign is extended to the left to match the number of bits contained in the larger-sized value.So if the shorter value is negative, 1s are filled in on the left, and if the value is positive ,then the 0s are filled in.

There are two bitwise shift operators namely Left Shift and Right Shift and are denoted by symbol "<<" and ">>" respectively.These operators do as what their name say.

When the Left Shift is performed on data or a value ,the bits of the data or the value is shifted to the left.The symbol denoted to this is "<<".

000 110 << 1 - Operand left shifted by 1

001 100 - Result

000 011<< 1 - Operand left shifted by 1

000 110 - Result

000 001<< 1 - Operand left shifted by 1

000 010 - Result

We observe that the value in the operand_1 is left shifted by 1 ,which shifts the bits of the operand_1 by 1.

When the Right Shift is performed on data or a value, the bits present in the data or the value is shifted to the right.This is denoted by the symbol ">>".

Example:

000 110>> 1 - Operand right shifted by 1

000 011 - Result

000 100>> 1 - Operand right shifted by 1

000 010 - Result

**Example:**Take an 8bit variable, char.

01111010 -This is an eight bit variable.The number of binary digits the variable can hold give the number of bits

Here we have 8 binary numbers so there are 8 bits.

For now, consider we need to flip the bits of this variable,then we need to perform a bitwise operation ,we will use One's Compliment Operator, "~".

var = 01111010

~var = 10000101

Compare the result ,the operator "~" just flips the binary values.Let's look in detail on these operators now. Don't worry if nothing goes into your brain early on but you will certainly understand later on.

**Bit Operators:**Now that we have completed the preliminaries, lets take a look at the available bitwise operators in C.

& Bitwise AND Operator

| Bitwise OR Operator

^ Bitwise Exclusive OR Operator(XOR)

~ One's Complement

<< Left Shift

>> Right Shift

**Before we start , we need to know some terms:****Unary Operator:**The operators which take only one operand are known as unary operator.~10011101

~abc

**Binary Operator:**The operators which take tow operands are known as binary operator.abc & abc

abc | abc

**Binary:**This is a 2 base number system that computers use to transfer data.It has 2 digits 1 and 0.**Bit:**A bit can hold a number either 1 or 0.__Bitwise AND Operator__When two values are ANDed in C are compared bit by bit.There needs to be two operands in AND operator. The BItwise AND operator is denoted by the symbol "&" in C. When the two operands given to an AND both contain 1's in their corresponding bit position, the final result will be 1, rest all give out 0.You can understand easily in the example:

__Principle:__0 AND 0 = 0

0 AND 1 = 0

1 AND 0 = 0

1 AND 1 = 1

__Example:__Result = Operand_1 & Operand_2;

1111001001110100 - Operand_1

0011010101100101 - Operand_2

----------------------

0011000001100100 - Result after ANDing Operand 1 and 2.

Observe the examples operands bits and compare it with the bit below it.We observe that where there is 1 and 1 at corresponding bit positions result in 1 where as the rest will have the result as 0.

Bitwise AND and Logical AND are different, you cannot use a logical AND in place of bitwise AND ,same way you cannot use a bitwise AND instead of Logical AND.

__Bitwise OR Operator__This is similar to Bitwise AND but a tiny difference.Here all bits change to 1 if 1 is present in any of the operands.Only when the both the corrosponding bits are 0 they result in 0.This bitwise operator is denoted by the symbol "|".Just a single one not two.Two would make up to Logical OR("||").

__Principle:__*1 OR 1 = 1**1 OR 0 = 1**0 OR 1 = 1**0 OR 0 = 0**Example:*Result = Operand_1 | Operand_2;

1111001001110100 - Operand_1

0011010101100101 - Operand_2

----------------------

1111011101110101 - Result after ORing Operand 1 and 2.

Observe the example and compare bit by bit.We observe where both the corrosponding bits are 0 and 0 lead to a 0 whereas where there is a 1 there will be a result

__Bitwise Exclusive OR Operator__As same as the above but this time 1 and 1 also makes a 0.Simple!!.It is denoted by the symbol "^".Exclusive OR is also referred as XOR.

__Principle:__1 XOR 1 = 0

1 XOR 0 = 1

0 XOR 1 = 1

0 XOR 0 = 0

__Example:__Result = Operand_1 ^ Operand_2;

1111001001110100 -Operand_1

0011010101100101 -Operand_2

----------------------

1100011100010001 -Result after XORing Operand 1 and 2.

Again observe and compare bit by bit.You observe that where both the corrosponding bits are same then that results in an zero and the rest corresponding bits having different binary value result in 1.

__One's Compliment Operator__This is an unary operator as it has only one operand.This operator just flips the binary values of the bits.1 changes to 0 and 0 changes to 1.This operator is denoted by by the symbol "~".

__Principle:__1 = 0

0 = 1

__Example:__Result = ~Operand_1;

1000100110101001 -Operand_1

0111011001010110 -Result after ones complimenting Operand_1

Huh! Again ,Observe and compare bit by bit.We can see that the binary value of the bit is just flipped.

__Bit Operations on different size data__Did you think of what would happen if one operand was of 32bits and other of 16bits?

When a bitwise operation is performed between two values that are differnet in thier size, the system aligns the operands on the right.If the shorter of the two data is a signed variable(A Variable which can be negative as well as positive is known as signed variable),and the value is negative , then the sign is extended to the left to match the number of bits contained in the larger-sized value.So if the shorter value is negative, 1s are filled in on the left, and if the value is positive ,then the 0s are filled in.

__Bitwise Shift Operators__There are two bitwise shift operators namely Left Shift and Right Shift and are denoted by symbol "<<" and ">>" respectively.These operators do as what their name say.

**Left Shift Operator**When the Left Shift is performed on data or a value ,the bits of the data or the value is shifted to the left.The symbol denoted to this is "<<".

__Example:__000 110 << 1 - Operand left shifted by 1

001 100 - Result

000 011<< 1 - Operand left shifted by 1

000 110 - Result

000 001<< 1 - Operand left shifted by 1

000 010 - Result

We observe that the value in the operand_1 is left shifted by 1 ,which shifts the bits of the operand_1 by 1.

__Right Shift Operator__When the Right Shift is performed on data or a value, the bits present in the data or the value is shifted to the right.This is denoted by the symbol ">>".

Example:

000 110>> 1 - Operand right shifted by 1

000 011 - Result

000 100>> 1 - Operand right shifted by 1

000 010 - Result