Jump to content

C Programming/Operators

From Wikiversity
Completion status: this resource has reached a high level of completion.

Objective

[edit | edit source]
  • Unary Operators
  • Binary Operators: arithmetic, comparison, logical operations, bitwise operations, and assignment operators

Unary operators

[edit | edit source]

Unary operators are the operators that perform operations on a single operand to produce a new value. For unary operators (one operand), the operator is placed before.

Unary operators include:

  • - Minus operator. Negate the value of its operand. For example, if i is 5, then -i is -5.
  • ++ Pre-increment or post-increment operator. Increase a variable's value by 1.
  • -- Pre-decrement or post-decrement operator. Decrease a variable's value by 1.
  • ! NOT operator. Invert the boolean value of its operand.
  • ~ Complement operator
  • & Ampersand operator will get the address of a variable or a function. We will investigate it in the pointer lesson.
  • sizeof()

Minus operator

[edit | edit source]
int a = 10;
int b = -a;  // b = -10

The arithmetic subtraction operator and the minus operator refer to the same symbol - but are used in different contexts so you must be careful using this minus unary operators. It can be helpful to use brackets to make sure you don't accidentally get an undesirable result.

NOT operator !

[edit | edit source]

Change the value which is equal to 0 to 1 and different from 0 to 0:

int a = 100;
int b = 0;
a = !a;
b = !b;
printf("a: %d, b: %d", a, b);//0, 1

Increment operator ++: ++i (prefix) and i++ (postfix)

[edit | edit source]

++i will increment the value of i, and then return the incremented value to j

int i,j;
j = ++i; // j=1, i=1

i++ will return its initial value first to j then increases its value

int i,j;
j = i++; // j=0, i=1

Example:

int x=5, y=7;
int z = x++ + ++y -8;
printf("x %d, y %d, z %d", x, y, z);//6 8 5

The same implementation can be applied to the decrement operator --

Complement operator ~

[edit | edit source]

The complement operator ~ is a bitwise unary operator. It performs a bitwise NOT operation on its operand, which means it inverts all the bits of the operand.

int binVal0 = 0b00;
int binResult = ~binVal0;
printf("%d \n", binVal0);   // 0
printf("%d \n", binResult); // -1

Explain

int binVal0 = 0b00;         // binVal0 = 0b 0000 0000;
int binResult = ~binVal0;   // binResult = 0b 1111 1111;

When converting back to integer, the duplicate 1 bit from the left is omit, and one bit is kept for sign. So, there is 2 bit left

binResult = 0b11 = -1

Example with other decimal value

int binVal0 = 5;
int binResult = ~binVal0;
printf("%d \n", binVal0);   //5
printf("%d \n", binResult); //-6 = 1111111111111010

sizeof()

[edit | edit source]

sizeof() returns the amount of memory allocated to that data type in byte.

int a = 16;
printf("Size of variable a: %d\n", sizeof(a)); //Size of variable a : 4
printf("Size of int data type: %d\n", sizeof(int)); //Size of int data type : 4
printf("Size of char data type: %d\n", sizeof(char)); //Size of char data type : 1
printf("Size of float data type: %d\n", sizeof(float)); //Size of float data type : 4
printf("Size of double data type: %d\n", sizeof(double)); //Size of double data type : 8

Binary operators

[edit | edit source]

In C, binary operators are operators that take two operands. These operators are used for arithmetic, comparison, assignment, logical and bitwise operations.

C uses infix notation, which means that any binary operator is placed between the two operands. Note that the exact way that binary operators look could be said to defy the above rule for operator[] and operator(), but this is moot at this point. Typically, the binary operators have a space between themselves and each operand, whereas unary operators are placed with no space.

Arithmetic operators

[edit | edit source]
  • + – Addition operator. Produces the sum of two variables.
  • - – Subtraction operator. Produces the difference of two variables.
  • * – Multiplication operator. Produces the product of two variables.
  • / – Division operator. Produces the quotient of two variables.
  • % – Modulus operator. Produces the remainder of the division of two variables.
int a = 5, b = 2;
int sum = a + b; // sum is 7
int difference = a - b; // difference is 3
int product = a * b; // product is 10
int quotient = a / b; // quotient is 2
int remainder = a % b; // remainder is 1

Comparison operators

[edit | edit source]
  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to
int a = 5, b = 2;
int result;

result = (a == b); // result is 0 (false)
result = (a != b); // result is 1 (true)
result = (a > b); // result is 1 (true)
result = (a < b); // result is 0 (false)
result = (a >= b); // result is 1 (true)
result = (a <= b); // result is 0 (false)

Bitwise operators

[edit | edit source]

A bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. Bitwise operators might be NOT, AND, OR, XOR,...

  • & : Bitwise AND
  • | : Bitwise OR
  • ^ : Bitwise XOR (exclusive OR)
  • ~ : Bitwise NOT (complement operator)
  • << : Left shift
  • >> : Right shift

AND, OR and XOR example

[edit | edit source]
int a = 5, b = 2; // a = 0101 in binary, b = 0010 in binary
int result;

result = a & b; // result is 0 (0000 in binary)
result = a | b; // result is 7 (0111 in binary)
result = a ^ b; // result is 7 (0111 in binary)

Left shift and right shift example

[edit | edit source]
Caption text
Operator 7 6 5 4 3 2 1 0 Value
A 0 0 0 1 1 0 0 0 24
A >> 1 0 0 0 0 1 1 0 0 12
A >> 2 0 0 0 0 0 1 1 0 6
A << 3 1 1 0 0 0 0 0 0 192
A >> 4 0 0 0 0 0 0 0 1 1 (lost the last bit)

Mask or bitmask

[edit | edit source]

A mask or bitmask is data that is used for bitwise operations. Common bitmask functions are masking bits to 1, masking bits to 0, querying the status of a bit and toggling bit values.

Masking bits to 1

   10010101
OR 11110000
 = 11110101

Masking bits to 0

   10010101
AND 00001111
 = 00000101

Querying the status of a bit

   10011101
AND 00001000
 = 00001000

Assignment operators

[edit | edit source]

Assignment operators perform an operation and then assign the result to the variable.

  • = : Assignment
  • += : Add and assign
  • -= : Subtract and assign
  • *= : Multiply and assign
  • /= : Divide and assign
  • %= : Modulus and assign
  • &= : Bitwise AND and assign
  • |= : Bitwise OR and assign
  • ^= : Bitwise XOR and assign
  • <<= : Left shift and assign
  • >>= : Right shift and assign

lvalue, rvalue in = operator

[edit | edit source]

Take this assignment as an example

int a = 6;
  • a is the left value, which is abbreviated as lvalue in all C compiler, is the value on the left is modificable. lvalue is usually a variable.
  • 1 is the right value, which is abbreviated as rvalue in all C compiler. Right value concept pulls or fetches the value of the expression or operand on the right side of the assignment operator.

= Operator order with = operator

[edit | edit source]

As what we can learn from variable assigment, C has the right-to-left priority.

int a = 1;
int b = 2 + 3;  // b = -5

From that example, we can see:

  • 1 (rvalue) is assigned to a.
  • (2 + 3) is executed in the right side and is then assigned to b

To change the order of operations, brackets override any other ordering, like they would inside a maths equation:

float a = 9/2+1;// 5.000000
float b = 9/(2+1);  // 3.000000

Explantion

  • For a, 9/2 = 4.5 and is then casting to int which results in 4.0. Adding it to 1 results in 5
  • For b, 2+1 is executed first as they're inside the bracket. 9/3 results in 3

Example

[edit | edit source]
int a = 5;

a += 2; // a is now 7
a -= 2; // a is now 5
a *= 2; // a is now 10
a /= 2; // a is now 5
a %= 2; // a is now 1
a &= 2; // a is now 0 (0001 & 0010)
a |= 2; // a is now 2 (0000 | 0010)
a ^= 2; // a is now 0 (0010 ^ 0010)
a <<= 1; // a is now 0 (still 0 shifted left)
a >>= 1; // a is still 0 (still 0 shifted right)

Assignments

[edit | edit source]

Analyse the result between 2 binary numbers with bitwise OR | and arithmetic operators addition +

int binVal0 = 0b110;
int binVal1 = 0b111;
int binResult = binVal0 | binVal1;
printf("%d", binResult); //7 (0b111)
int binVal0 = 0b11;
int binVal1 = 0b11;
int binResult = binVal0|binVal1;
printf("%d", binResult); //3
int binVal0 = 0b11;
int binVal1 = 0b11;
int binResult = binVal0+binVal1;
printf("%d", binResult); //6