9. Operators in C

Table of Contents
< All Topics
Print

9. Operators in C

Bitwise Operators

  • Bitwise operators perform operations at bit level. These operators include: bitwise AND, bitwise OR, bitwise XOR and shift operators.
  • The bitwise AND operator (&) is a small version of the boolean AND (&&) as it performs operation on bits instead of bytes, chars, integers, etc.
  • The bitwise OR operator (|) is a small version of the boolean OR (||) as it performs operation on bits instead of bytes, chars, integers, etc.
  • The bitwise NOT (~), or complement, is a unary operation that performs logical negation on each bit of the operand. By performing negation of each bit, it actually produces the ones’ complement of the given binary value.
  • The bitwise XOR operator (^) performs operation on individual bits of the operands.
  • The result of all bitwise AND, OR and XOR operations are shown in the table below.
OperatorDescription
& (Binary AND Operator)
If a bit exists in the two operands, it copies a bit to the result.
| (Binary OR Operator.)Only if the bit exists in one of the operands, it copies a bit.
^ (Binary XOR Operator)If a bit is set in one operand rather than both, it copies the bit.
~ (Binary One’s Complement Operator)It is a unary operator that has the effect of ‘flipping’ bits.
<< (Binary Left Shift Operator)It moves the value of the right operand to the left by the number of bits specified by the right operand.
>> (Binary Right Shift Operator)It moves the value of the left operands to the right by the number of bits mentioned by the right operand.
Bitwise OperatorDescriptionExample
Let A= 60, i.e 0011 1100
~Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits.
(~A ) = -60, i.e,. 11000011 in 1’s complement form.
————————————–
To get -60, add 1 to above i.e (~A ) = -60, i.e,. 1100 0100 in 2’s complement form.
<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.A << 2 = 240 i.e., 1111 0000
>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.A >> 2 = 15 i.e., 0000 1111
In short: every single left shift doubles the number and every single right shift half the number.
pqp & q (Bitwise AND)p | q (Bitwise OR)p ^ q (Bitwise XOR)
00000
01011
11110
10011
Bitwise or say binary operation is performed on bit-by-bit manner.

Special operators

Besides the operators mentioned above in C, some more operators are used to carry out different tasks. Let’s have a look at them:

OperatorDescriptionExample
sizeof()Returns the size of a variable.sizeof(a), where a is integer, will return 4.
&Returns the address of a variable.&a; returns the actual address of the variable.
*Pointer to a variable.*a;
? :Conditional Expression.If Condition is true ? then value X : otherwise value Y

sizeof Operator

The sizeof is a unary operator in the C language used to calculate the size of operands. Its result is an unsigned internal type represented by size_t.

#include  

int main() 
{ int i=10,a;
 printf("integer: %d\n", sizeof(a)); 
return 0;
}

// Output :
// Integer 4

Comma Operator

The comma operator in C is represented by the token. It is a binary operator with the lowest precedence among all C operators. It works as an operator and separator. It evaluates the first operand, discards the result, evaluates the second operand, and returns its value.

int a = 1, b = 2, c = 3; 
int result = (a++, b++, c++); 

/* The value of result will be 3, 
as the comma operator evaluates a, b, and c 
in sequence and returns the value of c. */

Conditional Operator

It is written in the form of Expression1? Expression2: Expression3. Expression 1 is the condition that needs to be evaluated, and if it is true, then Expression 2 is executed, and we return the result.

However, if Expression 1 is false, we execute and return the result of Expression 3. We can use conditional operators to replace if…else statements.

Image 15
y? x : y); printf("Maximum of the first two numbers= %d\n" ,Max1); Max2 = (x> y ?x : y) > z ? (x>y ? x:y): z; printf("Maximum of the three numbers is= %d\n", Max2); } " style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button">
// Program to find maximum in first two and maximum in all three input values to a,b,c
#include 
void main() { 
 int x, y, z, Max1,Max2,Min; 
 printf("Enter three whole numbers:\n"); 
 scanf( "%d%d%d",&x, &y, &z ) ; 
 Min = x < y ? x : y; 
 printf("Minimum of the first two numbers= %d\n" ,Min ); 
 Max1 = (x > y? x : y); 
 printf("Maximum of the first two numbers= %d\n" ,Max1); 
 Max2 = (x> y ?x : y) > z ? (x>y ? x:y): z; 
 printf("Maximum of the three numbers is= %d\n", Max2); 
} 

dot (.) and arrow (->) Operators

They are member operators used to reference individual members of classes, unions, and structures. The dot (.) operator is applied to real objects, while the arrow operator (->) is used with a pointer.

Cast Operator

It converts one type of data to another type. This special C operator forces one data type to convert into another. The process of conversion may result in loss of data precision or information, so it is important to use type casting carefully and consider the implications of the conversion. As an example, if you are converting a floating-point number to an integer, you may lose the decimal part of the number.

&,* Operator

& returns a variable’s address while * operator is a pointer to a variable. For example, &a; will give the address of the variable, and *var will be a pointer to a variable ‘var’. This will be discussed in detail in the topic of pointers. In C programming, pointers are widely used to work with memory addresses and manipulate data at a lower level. Understanding pointers is crucial for writing efficient and optimized code.

Previous Section

Leave a comment