Bitwise Operator in C? with example | Types of bitwise operators

 

Bitwise Operator in C

The bitwise operators are the operators used to perform the operations on the data at the bit-level. When we perform the bitwise operations, then it is also known as bit-level programming. It consists of two digits, either 0 or 1. It is mainly used in numerical computations to make the calculations faster.



Bitwise operators in C are used to manipulate individual bits of data within integer types. They are essential for low-level programming tasks, such as working with hardware or optimizing certain algorithms. In this explanation, we'll cover the most commonly used bitwise operators in C: AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). We'll provide examples and explanations for each operator.

We have different types of bitwise operators in the C programming language. The following is the list of the bitwise operators:
OperatorMeaning of operator
&Bitwise AND operator
|Bitwise OR operator
^Bitwise exclusive OR operator
~One's complement operator (unary operator)
<<Left shift operator
>>Right shift operator

Let's look at the truth table of the bitwise operators.

XYX&YX|YX^Y
00000
01011
10011
11111
  1. Bitwise AND (&):

    The bitwise AND operator compares each bit of two integers and produces a new integer where each corresponding bit is set to 1 only if both input bits are 1; otherwise, it sets the bit to 0.

    Example:

    • c
      int a = 12; // binary: 1100 int b = 6; // binary: 0110 int result = a & b; // binary result: 0100 (decimal: 4)
  2. Bitwise OR (|):

    The bitwise OR operator compares each bit of two integers and produces a new integer where each corresponding bit is set to 1 if at least one of the input bits is 1.

    Example:
    • c
      int a = 12; // binary: 1100 int b = 6; // binary: 0110 int result = a | b; // binary result: 1110 (decimal: 14)
  3. Bitwise XOR (^):

    The bitwise XOR operator compares each bit of two integers and produces a new integer where each corresponding bit is set to 1 if the input bits are different (one is 0, and the other is 1).

    Example:

    • c
      int a = 12; // binary: 1100 int b = 6; // binary: 0110 int result = a ^ b; // binary result: 1010 (decimal: 10)
  4. Bitwise NOT (~):

    The bitwise NOT operator inverts each bit of an integer. It changes 1s to 0s and 0s to 1s.

    Example:
    • c
      int a = 12; // binary: 1100 int result = ~a; // binary result: 0011 (decimal: -13, depending on the data type)
  5. Left Shift (<<):

    The left shift operator shifts the bits of an integer to the left by a specified number of positions. This effectively multiplies the number by 2 to the power of the shift count.

    Example:
    • c
      a = 5; // binary: 0101 int result = a << 2; // binary result: 10100 (decimal: 20)
  6. Right Shift (>>):

    The right shift operator shifts the bits of an integer to the right by a specified number of positions. This effectively divides the number by 2 to the power of the shift count.

    Example
    • c
      int a = 20; // binary: 10100 int result = a >> 2; // binary result: 0010 (decimal: 5)
    • Bitwise operators are often used in low-level programming, such as working with hardware registers, optimizing algorithms for bit-level operations, or implementing custom data structures. They can be powerful tools when used appropriately, but care must be taken to ensure that the desired results are achieved, as manipulating individual bits can be error-prone if not used carefully.

Post a Comment

0 Comments