- - - - - - - -
0xB5: 1011 0101
iv. ~0xAA returns 0x55
0xAA: 1010 1010
~ : 0101 0101
- - - - - - - -
0x55: 0101 0101
v. 0x14 >> 1 returns 0x0A (shift 0x14 right by 1 digit)
0x14: 0001 0100
>> 1: 0000 1010
- - - - - - - -
0x0A: 0000 1010
vi. 0x14 >> 2 returns 0x05 (shift 0x14 right by 2 digits)
0x14: 0001 0100
>> 2: 0000 0101
- - - - - - - -
0x05: 0000 0101
vii. 0x235A << 1 returns 0x46B4 (shift left 0x235A left by 1 digit)
0x235A: 0010 0011 0101 1010
<< 1 : 0100 0110 1011 0100
- - - - - - - - - - - - - -
0x46B4: 0100 0110 1011 0100
viii. 0x1A << 3 returns 0xD0 (shift left 0x1A by 3 digits)
0x1A: 0001 1010
<< 3: 1101 0000
- - - - - - - -
0xD0: 1101 0000
Assignment Operators
In C language there are two types of assignments: simple and compound. In simple assignments an expression is simply assigned to another expression, or an operation is performed using an expression and the result is assigned to another expression:
Expression1 = Expression2
or
Result = Expression1 operation Expression2
Examples of simple assignments are:
Temp = 10;
Cnt = Cnt + Temp;
Compound assignments have the general format:
Result operation = Expression1
Here the specified operation is performed on Expression1 and the result is stored in Result . For example:
j += k;
is same as:
j = j + k;
also
p *= m;
is same as
p = p * m;
The following compound operators can be used in mikroC programs:
+= -= *= /= %=
&= |= ^= >>= <<=
Conditional Operators
The syntax of a conditional operator is:
Result = Expression1 ? Expression2 : Expression3
Expression1 is evaluated first, and if its value is true, Expression2 is assigned to Result , otherwise Expression3 is assigned to Result . In the following example, the maximum of x and y is found where x is compared with y and if x y then max = x, otherwise max = y:
max = (x > y) ? x : y;
In the following example, lowercase characters are converted to uppercase. If the character is lowercase (between a and z), then by subtracting 32 from the character we obtain the equivalent uppercase character:
c = (c >= a && c <= z) ? (c - 32) : c;
Preprocessor Operators
The preprocessor allows a programmer to:
• Compile a program conditionally, such that parts of the code are not compiled
• Replace symbols with other symbols or values
• Insert text files into a program
The preprocessor operator is the (“#”) character, and any line of code leading with a (“#”) is assumed to be a preprocessor command. The semicolon character (“;”) is not needed to terminate a preprocessor command.
mikroC compiler supports the following preprocessor commands:
#define #undef
#if #elif #endif
#ifdef #ifndef
#error
#line
#define, #undef, #ifdef, #ifndef The #define preprocessor command provides macro expansion where every occurrence of an identifier in the program is replaced with the value of that identifier. For example, to replace every occurrence of MAX with value 100 we can write:
#define MAX 100
An identifier that has already been defined cannot be defined again unless both definitions have the same value. One way to get around this problem is to remove the macro definition:
#undef MAX
Alternatively, the existence of a macro definition can be checked. In the following example, if MAX has not already been defined, it is given value 100, otherwise the #define line is skipped:
#ifndef MAX
#define MAX 100
#endif
Note that the #define preprocessor command does not occupy any space in memory. We can pass parameters to a macro definition by specifying the parameters in a parenthesis after the macro name. For example, consider the macro definition:
#define ADD(a, b) (a + b)
When this macro is used in a program, ADD(a, b) will be replaced with (a + b) as shown:
p = ADD(x, y)
will be transformed into
p = (x + y)
Similarly, we can define a macro to calculate the square of two numbers:
#define SQUARE(a) (a * a)
We can now use this macro in a program:
p = SQUARE(x)
will be transformed into
p = (x * x)
#include The preprocessor directive #include is used to include a source file in our program. Usually header files with extension “.h” are used with #include. There are two formats for using #include:
#include
and
#include "file"
In first option the file is searched in the mikroC installation directory first and then in user search paths. In second option the specified file is searched in the mikroC project folder, then in the mikroC installation folder, and then in user search paths. It is also possible to specify a complete directory path as:
#include "C:\temp\last.h"
The file is then searched only in the specified directory path.
#if, #elif, #else, #endif The preprocessor commands #if, #elif, #else , and #endif are used for conditional compilations, where parts of the source code can be compiled only if certain conditions are met. In the following example, the code section where variables A and B are cleared to zero is compiled if M has a nonzero value, otherwise the code section where A and B are both set to 1 is compiled. Notice that the #if must be terminated with #endif:
#if M
A = 0;
B = 0;
#else
A = 1;
B = 1;
#endif
We can also use the #elif condition, which tests for a new condition if the previous condition was false:
#if M
A = 0;
B = 0;
#elif N
A = 1;
B = 1;
#else
A = 2;
B = 2;
#endif
In the above example, if M has a nonzero value code section, A = 0; B = 0; are compiled. Otherwise, if N has a nonzero value, then code section A = 1; B = 1; is compiled. Finally, if both M and N are zero, then code section A = 2; B = 2; is compiled. Notice that only one code section is compiled between #if and #endif and that a code section can contain any number of statements.
3.1.19 Modifying the Flow of Control
Statements are normally executed sequentially from the beginning to the end of a program. We can use control statements to modify this normal sequential flow in a C program. The following control statements are available in mikroC programs:
Читать дальше