Moät soá kieán thöùc cô sôû veà laäp trình baèng ngoân ngöõ C

Phaâàn tröôùc | Muïc luïc | Phaâàn sau

VIII. Moät soá toaùn töû ñaëc bieät

Noäi dung:

Vôùi phaàn naøy ta laøm quen vôùi caùc toaùn töû ñaëc bieät sau:

 

Toùan töû sizeof

sizeof (expression)

Keát quaû laø soá byte phaàn boä nhôù chöùa giaù trò cuûa expression.

Ví duï:

1:  /* Using the sizeof operator */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     char   ch = ` `;
7:     int    int_num = 0;
8:     float  flt_num = 0.0f;
9:     double dbl_num = 0.0;
10:
11:    printf("The size of char is: %d-byte\n", sizeof(char));
12:      printf("The size of ch is: %d-byte\n", sizeof ch );
13:        printf("The size of int is: %d-byte\n", sizeof(int));
14:          printf("The size of int_num is: %d-byte\n", sizeof int_num);
15:            printf("The size of float is: %d-byte\n", sizeof(float));

16:          printf("The size of flt_num is: %d-byte\n", sizeof flt_num);
17:        printf("The size of double is: %d-byte\n", sizeof(double));
18:      printf("The size of dbl_num is: %d-byte\n", sizeof dbl_num);
19:    return 0;
20: }
Keát quaû:
 
C:\app> 08L01
The size of char is: 1-byte
The size of ch is: 1-byte
The size of int is: 2-byte
The size of int_num is: 2-byte
The size of float is: 4-byte
The size of flt_num is: 4-byte
The size of double is: 8-byte
The size of dbl_num is: 8-byte
C:\app>

Toaùn töû Logic

&&

Toùan töû logic AND

||

Toùan töû logic OR

!

Toùan töû logic phuû ñònh

 

Ghi chuù

Khi bieåu thöùc traû giaù trò khaùc 0, bieåu thöùc logic traû giaù trò TRUE.

Khi bieåu thöùc traû giaù trò baèng 0, bieåu thöùc logic traû giaù trò FALSE.

 

Exp1

Exp2

Exp1 && Exp2

Exp1 || Exp2

!Exp1

1

1

1

1

0

1

0

0

1

0

0

1

0

1

1

0

0

0

0

1

Ví duï:

1:  /* Using the logical AND operator */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int   num;
7:
8:     num = 0;
9:     printf("The AND operator returns: %d\n",
10:           (num%2 == 0) && (num%3 == 0));
11:    num = 2;
12:    printf("The AND operator returns: %d\n",
13:           (num%2 == 0) && (num%3 == 0));
14:    num = 3;
15:    printf("The AND operator returns: %d\n",
16:           (num%2 == 0) && (num%3 == 0));
17:    num = 6;
18:    printf("The AND operator returns: %d\n",
19:           (num%2 == 0) && (num%3 == 0));
20:
21:    return 0;
22: }
Keát quaû:
 
The AND operator returns: 1
The AND operator returns: 0
The AND operator returns: 0
The AND operator returns: 1

Ví duï:

1:  /* Using the logical OR operator */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int   num;
7:
8:     printf("Enter a single digit that can be divided\nby both 2 and 3:\n");
9:     for (num = 1; (num%2 != 0) || (num%3 != 0); )
10:       num = getchar() - 48;
11:    printf("You got such a number: %d\n", num);
12:    return 0;
13: }
Keát quaû:
 
Enter a single digit that can be divided
by both 2 and 3:
2
3
4
5
6
You got such a number: 6

Ví duï:

1:  /* Using the logical negation operator */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int   num;
7:
8:     num = 7;
9:     printf("Given num = 7\n");
10:    printf("!(num < 7)  returns: %d\n", !(num < 7));
11:    printf("!(num > 7)  returns: %d\n", !(num > 7));
12:    printf("!(num == 7) returns: %d\n", !(num == 7));
13:    return 0;
14: }
Keát quaû:
 
Given num = 7
!(num < 7)  returns: 1
!(num > 7)  returns: 1
!(num == 7) returns: 0

Thao taùc Bits

Toaùn töû

Moâ taû

Toaùn töû

Moâ taû

&

Pheùp hoäi bit AND

~

Pheùp laáy phaàn buø

|

Pheùp tuyeån bit OR

>>

Pheùp dòch traùi 1 bit

^

Pheùp tuyeån loaïi tröø XOR

<<

Pheùp dòch phaûi 1 bit

 

Ví duï veà thao taùc bits:

 

Decimal

 

 

 

Results

Expressions

Hex

Binary

Decimal

Hex

Binary

12 & 10

0x0C & 0x0A

1100 &1010

8

0x08

1000

12 | 10

0x0C | 0x0A

1100 | 1010

14

0x0E

1110

12 ^ 10

0x0C ^ 0x0A

1100 ^ 1010

6

0x06

0110

~12

~0x000C

~0000000000001100

65523

FFF3

1111111111110011

Ví duï:

1:  /* Using bitwise operators */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int   x, y, z;
7:
8:     x = 4321;
9:     y = 5678;
10:    printf("Given x = %u, i.e., 0X%04X\n", x, x);
11:    printf("      y = %u, i.e., 0X%04X\n", y, y);
12:    z = x & y;
13:    printf("x & y  returns: %6u, i.e., 0X%04X\n", z, z);
14:    z = x | y;
15:    printf("x | y  returns: %6u, i.e., 0X%04X\n", z, z);
16:    z = x ^ y;
17:    printf("x ^ y  returns: %6u, i.e., 0X%04X\n", z, z);
18:    printf("  ~x   returns: %6u, i.e., 0X%04X\n", ~x, ~x);
19:    return 0;
20: }
Keát quaû:
 
Given x = 4321, i.e., 0X10E1
      y = 5678, i.e., 0X162E
x & y  returns:  4128, i.e., 0X1020
x | y  returns:  5871, i.e., 0X16EF
     x ^ y  returns:  1743, i.e., 0X06CF
       ~x   returns: 61214, i.e., 0XEF1E

Caûnh baùo

Ñöøng laàm laãn giöõa toaùn töû quan heä && and || vaø toaùn töû logic & and | . Ví duï:

(x=1) & (y=10)
(x=1) && (y=10)

Toaùn töû dòch chuyeån bit

x >> y
x << y

x laø soá thöïc hieän dòch chuyeån. y laø soá laàn phaûi dòch chuyeån, moãi laàn moät bit.

Ví duï:

8 >> 2 -> (1 * 23 + 0 *22 + 0 * 21 + 0 *20) >> 2

seõ laø:

(0 * 23 + 0 * 22 + 1 *21 + 0 * 20) -> 0010 (binary) -> 2 (decimal).

Ví duï:

1:  /* Using shift operators */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int   x, y, z;
7:
8:     x = 255;
9:     y = 5;
10:    printf("Given x = %4d, i.e., 0X%04X\n", x, x);
11:    printf("      y = %4d, i.e., 0X%04X\n", y, y);
12:    z = x >> y;
13:    printf("x >> y  returns: %6d, i.e., 0X%04X\n", z, z);
14:    z = x << y;
15:    printf("x << y  returns: %6d, i.e., 0X%04X\n", z, z);
16:    return 0;
17: }
Keát quaû:
 
Given x =  255, i.e., 0X00FF
      y =    5, i.e., 0X0005
x >> y  returns:      7, i.e., 0X0007
x << y  returns:   8160, i.e., 0X1FE0

Chuù yù:

x >> y     ó     x / 2y
x << y     ó     x * 2y

Toaùn töû ñieàu kieän

x ? y : z

Giaù trò bieåu thöùc seõ baèng y neáu x ñuùng; vaø baèng z neáu x sai.

Ví duï: s = x>y?x:y  seõ cho ta giaù trò lôùn nhaát giöõa x vaø y.

Ví duï:

1:  /* Using the ?: operator */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int   x;
7:
8:     x = sizeof(int);
9:     printf("%s\n",
10:       (x == 2) ? "The int data type has 2 bytes." : "int doesn't have 2
                      Âbytes.");
11:    printf("The maximum value of int is: %d\n",
12:       (x != 2) ? ~(1 << x * 8 - 1) : ~(1 << 15) );
13:    return 0;
14: }
Keát quaû:
 
The int data type has 2 bytes.
The maximum value of int is: 32767

Exercises

  1. Cho x = 0xEFFF vaø y = 0x1000, ~x vaø ~y seõ traû giaù trò gì?
  2. Cho x = 123 vaø y = 4, vieát chöông trình bieåu dieãn keát quaû x << y and x >> y.
  3. Vieát chöông trình tính 0xFFFF^0x8888, 0xABCD & 0x4567, vaø 0xDCBA | 0x1234.
  4. Duøng toaùn töû ?: vaø leänh for vieát chöông trình nhaäp kyù töï vaøo maõi cho ñeán khi nhaäp kyù töï ‘q’ thì döøng. (Gôïi yù: Duøng bieåu thöùc x!='q' ? 1 : 0.)