C Operators: Arithmetic, Logical, and Bitwise Examples

C Operators: Arithmetic, Logical, and Bitwise

Logical Operators

Logical operators are used to perform logical operations. Here are some examples:

  1. AND (&&): Returns 1 (true) if both operands are non-zero; otherwise, 0 (false).
    Example:
    int a = 5, b = 3, c = 2;
    int result = (a > b) && (b > c); // result is 1 (true)
  2. OR (||): Returns 1 (true) if at least one of the operands is non-zero; otherwise, 0 (false).
    Example:
    int a = 5, b = 0, c = 2;
    int result = (a > b) || (b > c); // result is 1 (true)
  3. NOT (!): Returns 1 (true) if the operand is 0; otherwise, 0 (false).
    Example:
    int a = 5, b = 0;
    int result = !a; // result is 0 (false)
    int result2 = !b; // result2 is 1 (true)

Arithmetic Operators

Arithmetic operators are used to perform arithmetic calculations. Here are some examples:

  1. Addition (+): Adds two operands.
    Example:
    int a = 5, b = 3;
    int result = a + b; // result is 8
  2. Subtraction (-): Subtracts the second operand from the first.
    Example:
    int a = 10, b = 4;
    int result = a - b; // result is 6
  3. Multiplication (*): Multiplies two operands.
    Example:
    int a = 2, b = 6;
    int result = a * b; // result is 12
  4. Division (/): Divides the first operand by the second (integer division).
    Example:
    int a = 15, b = 3;
    int result = a / b; // result is 5
  5. Modulus (%): Returns the remainder of division between two operands.
    Example:
    int a = 17, b = 5;
    int result = a % b; // result is 2
  6. Increment (++): Increments the value of the operand by 1.
    Example:
    int a = 5;
    a++; // a is now 6
  7. Decrement (--): Decrements the value of the operand by 1.
    Example:
    int a = 5;
    a--; // a is now 4

Bitwise Operators

In C programming, bitwise operators are used to manipulate individual bits of a data value at the bit level. These operators perform logical operations on the binary representation of the operands. The bitwise operators are primarily used for low-level programming, such as working with hardware or system programming, as well as for optimizing certain algorithms.

Here are the bitwise operators in C with examples:

  1. Bitwise AND (&): This operator performs a boolean AND operation on each pair of corresponding bits in the operands. The result is 1 if both bits are 1; otherwise, it is 0.
    Example:
    int a = 5; // Binary: 0101
    int b = 3; // Binary: 0011
    int c = a & b; // c = 0001 (Binary)
    printf("%d", c); // Output: 1
  2. Bitwise OR (|): This operator performs a boolean OR operation on each pair of corresponding bits in the operands. The result is 1 if either of the bits is 1; otherwise, it is 0.
    Example:
    int a = 5; // Binary: 0101
    int b = 3; // Binary: 0011
    int c = a | b; // c = 0111 (Binary)
    printf("%d", c); // Output: 7
  3. Bitwise XOR (^): This operator performs a boolean XOR (exclusive OR) operation on each pair of corresponding bits in the operands. The result is 1 if the bits are different; otherwise, it is 0.
    Example:
    int a = 5; // Binary: 0101
    int b = 3; // Binary: 0011
    int c = a ^ b; // c = 0110 (Binary)
    printf("%d", c); // Output: 6
  4. Bitwise Complement (~): This is a unary operator that flips the bits of the operand. It changes 1 to 0 and 0 to 1.
    Example:
    int a = 5; // Binary: 0000 0000 0000 0000 0000 0000 0000 0101
    int b = ~a; // b = 1111 1111 1111 1111 1111 1111 1111 1010 (Binary)
    printf("%d", b); // Output: -6
  5. Left Shift (<<): This operator shifts the bits of the left operand to the left by the number of positions specified by the right operand. Vacant positions on the right are filled with 0s.
    Example:
    int a = 5; // Binary: 0000 0000 0000 0000 0000 0000 0000 0101
    int b = a << 2; // b = 0000 0000 0000 0000 0000 0000 0000 0101 << 2 = 0000 0000 0000 0000 0000 0000 0001 0100 (Binary)
    printf("%d", b); // Output: 20
  6. Right Shift (>>): This operator shifts the bits of the left operand to the right by the number of positions specified by the right operand. Vacant positions on the left are filled with 0s for positive numbers and sign bits (1 for negative numbers) for negative numbers.
    Example:
    int a = 10; // Binary: 0000 0000 0000 0000 0000 0000 0000 1010
    int b = a >> 2; // b = 0000 0000 0000 0000 0000 0000 0000 0010 (Binary)
    printf("%d", b); // Output: 2