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:
-
AND (
&&
): Returns1
(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)
-
OR (
||
): Returns1
(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)
-
NOT (
!
): Returns1
(true) if the operand is0
; 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:
-
Addition (
+
): Adds two operands.
Example:
int a = 5, b = 3;
int result = a + b; // result is 8
-
Subtraction (
-
): Subtracts the second operand from the first.
Example:
int a = 10, b = 4;
int result = a - b; // result is 6
-
Multiplication (
*
): Multiplies two operands.
Example:
int a = 2, b = 6;
int result = a * b; // result is 12
-
Division (
/
): Divides the first operand by the second (integer division).
Example:
int a = 15, b = 3;
int result = a / b; // result is 5
-
Modulus (
%
): Returns the remainder of division between two operands.
Example:
int a = 17, b = 5;
int result = a % b; // result is 2
-
Increment (
++
): Increments the value of the operand by 1.
Example:
int a = 5;
a++; // a is now 6
-
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:
-
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
-
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
-
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
-
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
-
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
-
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