Operators and expressions (arithmetic, logical, bitwise)

Here’s a code example that demonstrates the different types of operators and expressions in C: arithmetic, logical, and bitwise. This example performs various operations on two integers and displays the results.

#include <stdio.h>

int main() {
    int a = 12, b = 5;
    int arithmeticResult, bitwiseResult;
    int logicalResult;
    
    // Arithmetic operators
    arithmeticResult = a + b;     // Addition
    printf("Addition: %d + %d = %d\n", a, b, arithmeticResult);

    arithmeticResult = a - b;     // Subtraction
    printf("Subtraction: %d - %d = %d\n", a, b, arithmeticResult);

    arithmeticResult = a * b;     // Multiplication
    printf("Multiplication: %d * %d = %d\n", a, b, arithmeticResult);

    arithmeticResult = a / b;     // Division (integer division)
    printf("Division: %d / %d = %d\n", a, b, arithmeticResult);

    arithmeticResult = a % b;     // Modulus (remainder)
    printf("Modulus: %d %% %d = %d\n", a, b, arithmeticResult);

    // Logical operators
    logicalResult = (a > b) && (b > 0);  // Logical AND
    printf("Logical AND (a > b && b > 0): %d\n", logicalResult);

    logicalResult = (a < b) || (b > 0);  // Logical OR
    printf("Logical OR (a < b || b > 0): %d\n", logicalResult);

    logicalResult = !(a == b);           // Logical NOT
    printf("Logical NOT (!(a == b)): %d\n", logicalResult);

    // Bitwise operators
    bitwiseResult = a & b;               // Bitwise AND
    printf("Bitwise AND: %d & %d = %d\n", a, b, bitwiseResult);

    bitwiseResult = a | b;               // Bitwise OR
    printf("Bitwise OR: %d | %d = %d\n", a, b, bitwiseResult);

    bitwiseResult = a ^ b;               // Bitwise XOR
    printf("Bitwise XOR: %d ^ %d = %d\n", a, b, bitwiseResult);

    bitwiseResult = ~a;                  // Bitwise NOT
    printf("Bitwise NOT: ~%d = %d\n", a, bitwiseResult);

    bitwiseResult = a << 1;              // Left shift
    printf("Left Shift: %d << 1 = %d\n", a, bitwiseResult); bitwiseResult = a >> 1;              // Right shift
    printf("Right Shift: %d >> 1 = %d\n", a, bitwiseResult);

    return 0;
}

Explanation of Operators and Expressions

Arithmetic Operators

These operators perform mathematical operations:

  • Addition (+): Adds two operands (a + b).
  • Subtraction (-): Subtracts one operand from another (a - b).
  • Multiplication (*): Multiplies two operands (a * b).
  • Division (/): Divides one operand by another and gives the quotient (a / b). In integer division, the result is an integer.
  • Modulus (%): Returns the remainder of division (a % b).

Logical Operators

These operators are used for boolean logic, returning either 1 (true) or 0 (false):

  • Logical AND (&&): Returns true if both conditions are true (a > b && b > 0).
  • Logical OR (||): Returns true if at least one condition is true (a < b || b > 0).
  • Logical NOT (!): Inverts the value of a condition (!(a == b)).

Bitwise Operators

These operators work at the bit level, manipulating individual bits in the integers:

  • Bitwise AND (&): Sets each bit to 1 if both corresponding bits are 1 (a & b).
  • Bitwise OR (|): Sets each bit to 1 if at least one of the corresponding bits is 1 (a | b).
  • Bitwise XOR (^): Sets each bit to 1 if only one of the corresponding bits is 1 (a ^ b).
  • Bitwise NOT (~): Inverts each bit in the operand (~a).
  • Left Shift (<<): Shifts all bits to the left, adding 0s on the right (a << 1). Each shift left doubles the number.
  • Right Shift (>>): Shifts all bits to the right, removing bits from the right (a >> 1). Each shift right halves the number.

Summary

This example covers essential operators and their uses:

  • Arithmetic operators perform basic calculations.
  • Logical operators evaluate conditions and are used in control flow.
  • Bitwise operators work directly on bits, enabling fine-grained control over binary data.

The output of this program will display the results of each operation, helping to understand how each operator works.

Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!

Refresh