Module X: Additional Materials - Algoritma-dan-Pemrograman-ITS/DasarPemrograman GitHub Wiki
Table of Contents
- Bitwise Operators
- sizeof Operator
- Address-of Operator
- Dereference Operator
- Conditional Operator
- Comma Operator
- Subscript Operator
Bitwise Operators
Bitwise operators, like the name, are used to perform operations on two operands in a binary scale (base 2 numbers). Before learning further about how bitwise operations work, it's better that we understand binary numbers.
There are 6 bitwise operators: AND, OR, XOR, COMPELEMENT, SHIFT LEFT, and SHIFT RIGHT. Let's look at the table below to understand their differences.
Operator | Symbol | Details |
---|---|---|
Bitwise AND | & | Evaluates two operands per bit. Results in 1 if both operand bits are 1, otherwise results in 0. |
Bitwise OR | | | Evaluates two operands per bit. Results in 1 if one or more operand bits are 1, otherwise results in 0. |
Bitwise XOR | ^ | Evaluates two operands per bit. Results in 1 if both operand bits are different, otherwise results in 0. |
Bitwise COMPLEMENT | ~ | Inverses all bit values, 1 to 0 and 0 to 1 (in bit length). |
Bitwise SHIFT LEFT | << | Shift bits to the left for as many as n (second operand). |
Bitwise SHIFT RIGHT | >> | Shift bits to the right for as many as n (second operand). |
Bitwise operators examples:
Suppose we have the numbers 12 and 5. In binary, 12 is represented as (1100) and 5 as (0101). The bitwise operations for these numbers are as follows.
-
Bitwise AND
12 = (1100) 5 = (0101) ------------ & 4 = (0100)
-
Bitwise OR
12 = (1100) 5 = (0101) ------------ | 13 = (1101)
-
Bitwise XOR
12 = (1100) 5 = (0101) ------------ ^ 9 = (1001)
-
Bitwise COMPLEMENT
12 = (1100) ~12 = (0011)
-
Bitwise SHIFT LEFT
Suppose we want to shift the bits of 13 to the left as many as 2, then the expression is 13 << 2.
13 = (001101) 13 << 2 = (110100)
Notice that the rightmost bit, after the left shifts, will be filled with 0's. Therefore the result of 13 << 2 = 52.
-
Bitwise SHIFT RIGHT
Now, suppose we want to shift the bits of 13 to the right as many as 2, then the expression is 13 >> 2.
13 = (001101) 13 >> 2 = (000011)
Notice that the leftmost bit, after the right shifts, will be filled with 0's. Therefore the result of 13 >> 2 = 3.
sizeof()
Operator
Although its form is more similar to a function, in the standardization of the C programming language it is an operator. The use of this operator is to find out the size of memory allocated by an operand (can be a variable or a data type) in bytes.
Example:
sizeof(int);
&
)
Address-of Operator (This operators returns the memory address of an operand (variable).
Example:
int var;
scanf("%d\n", &var);
*
)
Dereference Operator (Contrary to the address-of operator (&
), dereference operator (*
) returns the value of a pointer variable (this will be further explained in a later module on Pointers).
Although it uses the same symbol as a multiplier, dereference operator has a totally different function than the multiplier.
? :
)
Conditional Operator (Conditional operator is the only ternary operator (works on three operands) in C. Its function is similar to if - else
statement (will be further explained in a later module on Branching).
,
)
Comma Operator (The comma operator (,
) in C is used as a binary operator when used in the same sequence as assignment. It will only consider the rightmost operand as the value to be assigned.
int number = (5, 23); // number is equal to 23, not 5
Aside from functioning as an operator, the comma sign (,
) is also used as a separator between statements. For instance when making declarations of more than one variable.
int var1, var2, var3;
// Usage of the comma sign to separate the declaration of each variable
Not all statements can be separated by the comma sign.
[]
)
Subscript Operator (It is most commonly used to access an array element (will be explained in a later module on Arrays).