Explain bitwise operator with example.

 

Bitwise operators are used to manipulate the individual bits of a binary number. They are commonly used in computer programming for tasks such as data compression, encryption, and optimization.

The bitwise operators include:

  1. Bitwise AND (&): The bitwise AND operator compares the corresponding bits of two numbers and returns a 1 if both bits are 1, otherwise it returns 0. For example:1010 & 1100 = 1000

  2. Bitwise OR (|): The bitwise OR operator compares the corresponding bits of two numbers and returns a 1 if either of the bits are 1, otherwise it returns 0. For example:1010 | 1100=1000


  3. Bitwise XOR (^): The bitwise XOR operator compares the corresponding bits of two numbers and returns a 1 if the bits are different, otherwise it returns 0. For example:1010 ^ 1100 = 0110


  4. Bitwise NOT (~): The bitwise NOT operator flips all the bits of a number. For example:~1010 = 0101

  5. Left shift (<<): The left shift operator shifts the bits of a number to the left by a specified number of positions. For example:1010 << 2 = 101000


  6. Right shift (>>): The right shift operator shifts the bits of a number to the right by a specified number of positions. For example:1010 >> 2 = 0010

These operators are commonly used in programming languages such as C, C++, Java, and Python for a variety of tasks, such as setting and clearing individual bits, encoding and decoding data, and optimizing code for efficiency.

Post a Comment

Previous Post Next Post