C Programming/Operators
Objective[edit | edit source]
|
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[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]
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]
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,...
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]
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.
lvalue, rvalue in = operator[edit | edit source]Take this assignment as an example int a = 6;
= 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:
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
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
|