C Programming/Operators

= Objective =
 * Unary Operators
 * Binary Operators: arithmetic, comparison, logical operations, bitwise operations, and assignment operators

= Unary operators = 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.

Minus operator
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 !
Change the whole value from 0 to 1 and different from 0 to 0:

Increment operator ++: ++i (prefix) and i++ (postfix)
++i will increment the value of i, and then return the incremented value to j

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

Example:

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

Complement operator ~
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.

Explain

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

Example with other decimal value

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

= Binary operators = 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

 * – 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.

Comparison operators

 * == : Equal to
 * != : Not equal to
 * > : Greater than
 * < : Less than
 * >= : Greater than or equal to
 * <= : Less than or equal to

Bitwise operators
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)
 * << : Left shift
 * >> : Right shift

Mask or bitmask
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
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
Take this assignment as an example


 * 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 =

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

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:

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
= Assignments =

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