Выбрать главу

/* Pre-increment operator */

j = 4;

k = ++j; // k = 5, j = 5

/* Post-decrement operator */

j = 12;

k = j--; // k = 12, j = 11

/* Pre-decrement operator */

j = 12;

k = --j; // k = 11, j = 11

Relational Operators

Relational operators are used in comparisons. If the expression evaluates to TRUE, a 1 is returned; otherwise a 0 is returned.

All relational operators associate from left to right. A list of mikroC relational operators is given in Table 3.5.

Table 3.5: mikroC relational operators

Operator Operation
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

The following example illustrates the use of relational operators:

x = 10

x > 8   // returns 1

x == 10 // returns 1

x < 100 // returns 1

x > 20  // returns 0

x != 10 // returns 0

x >= 10 // returns 1

x <= 10 // returns 1

Logical Operators

Logical operators are used in logical and arithmetic comparisons, and they return TRUE (i.e., logical 1) if the expression evaluates to nonzero, and FALSE (i.e., logical 0) if the expression evaluates to zero. If more than one logical operator is used in a statement, and if the first condition evaluates to FALSE, the second expression is not evaluated. The mikroC logical operators are listed in Table 3.6.

Table 3.6: mikroC logical operators

Operator Operation
&& AND
|| OR
! NOT

The following example illustrates the use of logical operators:

/* Logical AND */

x = 7;

x > 0 && x < 10   // returns 1

x > 0 || x < 10   // returns 1

x >= 0 && x <= 10 // returns 1

x >= 0 && x < 5   // returns 0

a = 10; b = 20; c = 30; d = 40;

a > b && c > d // returns 0

b > a && d > c // returns 1

a > b || d > c // returns 1

Bitwise Operators

Bitwise operators are used to modify the bits of a variable. The mikroC bitwise operators are listed in Table 3.7.

Table 3.7: mikroC bitwise operators

Operator Operation
& Bitwise AND
| Bitwise OR
^ Bitwise EXOR
$ Bitwise complement
<< Shift left
>> Shift right

Bitwise AND returns 1 if both bits are 1, otherwise it returns 0.

Bitwise OR returns 0 if both bits are 0, otherwise it returns 1.

Bitwise XOR returns 1 if both bits are complementary, otherwise it returns 0.

Bitwise complement inverts each bit.

Bitwise shift left and shift right move the bits to the left or right respectively.

The following example illustrates the use of bitwise operators:

   i. 0xFA 0xEE returns 0xEA

      0xFA: 1111 1010

      0xEE: 1110 1110

      - - - - - - - -

      0xEA: 1110 1010

  ii. 0x01 | 0xFE returns 0xFF

      0x01: 0000 0001

      0xFE: 1111 1110

      - - - - - - - -

      0xFE: 1111 1111

 iii. 0xAA ^ 0x1F returns

      0xAA: 1010 1010

      0x1F: 0001 1111

      - - - - - - - -

      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

      - - - - - - - -