Bit wise operator and assignment operator in C

Hello coders! Welcome back!

Today will look into another two types of the operator that is bitwise operator and assignment operator.

Bitwise operator

As the name suggests, it does bitwise manipulation.

& operator → It takes two bit at a time and performs AND operation. It is a binary operator. The result is '1' when both the operands are '1'. 
Let's take an example- 

| operator, → It takes two bit at a time and performs OR operation. It is also a binary operator. The result is '1' when either of the bit is '1'. 
let's take an example- 

~ operator → It is a unary operator, it operates on only one operand. NOT operator complements each bit one by one. It makes 0 to 1 and 1 to 0.

What is the difference between a bitwise operator and logical operator? 

→ To explain the difference lets take an example-
Since the first 'if' statement gives the result '0' which means that it is false and not going to execute. The second 'if' statement gives the result '1' which means that it is true and going to execute the following set of statements. So bitwise operator performs the operation on bits and logical operators perform operations on expression or value. 

<< operator (left-shift operator)→ It is a binary operator.
                          First operand << Second operand
The first operand whose bits get left shited.
The second operand decides the number of places to shift the bits.

  1. When bits are shifted left the remaining places (trailing bits) are filled with 'zeroes'. 

     2. Left shifting is equivalent to multiplication by 2^right operand (2 raise to right operand).
>> operator (right shift operator) →  It is a binary operator.                     
                             First operand >> Second operand
The first operand whose bits get right shited.
The second operand decides the number of places to shift the bits.
  1. When bits are shifted right the remaining places (leading bits) are filled with 'zeroes'. 
  2. Right shifting is equivalent to division by 2^right operand (2 raise to right operand). 
XOR operator → First let's understand the difference between inclusive OR and Exclusive OR
In inclusive OR operator if either of the bit or both the bits are '1' the output produced is '1'. Inclusive OR means including both the combinations.
In the exclusive OR operator, the output is '1' if either of the bit is '1' and the output is '0' if both the bits are same. Exclusive OR means excluding both the combination.
                                          
                                             

Assignment operator-

An assignment operator is used to assign values to the operand. 
It is a binary operator. It requires L-value and R-value. (discussed it in the previous blog) 
It copies R-value to L-value. 
R-value should be a return value and L-value should be capable of holding the value. 

Shorthand assignment- 
Example → a+= 1 is equal to a=a+1
similarly for other operators.

This is it for this blog. Hope so you got a clear idea about these two operators. If any doubts drop a comment always ready to clear them.

Happy learning! 

Comments

Popular post