What Is Bitwise And Other Operators In C Language?

What Is Bitwise And Other Operators In C Language?


Hello Programmer,

Today we will discuss bitwise and other operators in c language. In the last blog we studied Increment and Decrement operators but today we will discuss 2 more important operators which are used in c language.


1) Bitwise Operators:


Bitwise operators are that type of operators which work on binary level or we can say that bitwise operators are used to performing an operation on the bit.

You used bitwise operators for testing the bits or shifting the bit right or left. Whenever you use bitwise operators one thing must remember that bitwise operators are not applicable to float or double.

In c language the are some bitwise operators:


1)Bitwise And(&): Bitwise And operators give value 1 if corresponding bits of two operands is 1. If any corresponding bit is 0 of two operands then the Bitwise And operators return value 0.

Example:    

5 = 0101
6=  0110

after using  Bitwise And it returns result 0100 in binary form. 


2)Bitwise OR(|): Bitwise OR operators give value 1 at least one corresponding bits of two operands is 1. If both bits of corresponding operands is 0 then it gives value 0.

Example:    

5 = 0101
6=  0110

after using  Bitwise OR it returns result 0111 in binary form. 


3)Bitwise Exclusive-OR(^): Bitwise Exclusive-OR or Bitwise XOR operators give value 1 if bits of both corresponding operands are opposite.

Example:    

5 = 0101
6=  0110

after using  Bitwise XOR it returns result 0011 in binary form.  


4)Left Shift(<<): Left shift operators are used to shift all bits toward the left side and it shifts a number of bits according to the number of bits specified.

Example:

5 = 0101 (In binary)
5<<1 = 01010 (In binary) [Left shift by one bit]

5<<4 = 01010000 [Left shift by four bit]


5)Right Shift(>>): Right shift operators are used to shifting all bits toward the right side and it shifts a number of bits according to the number of bits specified.

Example:

212 = 11010100 (In binary)

212>>4 = 00001101 [Right shift by four bit]


6)Complement operators(~): Bitwise complement operators are also known as unary operators and it is work on only one operand. It changes 0 to 1 and 1 to 0.

Example:

5 = 0101

after bitwise complement operators, it returns 1010 value.


2) Other Operators: 


In c language, there are also 2 more most important operators which help you to make your programming easy.

(I) sizeof operators: In c language, you can use sizeof operators to find the number of bytes occupied by a variable or data type in computer memory.

Example:


sizeof(float) returns 4

int m, x[30];
sizeof(m) return 2;
sizeof(x) return 60;


(II) Comma operators: In c language, you can use comma(,) to link the related expressions together. A comma linked list of expression is evaluated left to right and the value of right-most Exp is the value of a combined expression.

Example:

value=(x=10, y=18, x+y)


Sharing Is Caring...

Comments :

Post a Comment