Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ประเภทของตัวดำเนินการ (Operators) ใน C++

ใน C++ มีตัวดำเนินการหลายประเภท สามารถจัดกลุ่มได้กว้างๆ ดังนี้: arithmetic, relational, logical, bitwise, assignment และ operators อื่นๆ

ตัวดำเนินการเลขคณิต (Arithmetic Operators)

สมมติตัวแปร A มีค่า 10 และ B มีค่า 20 ดังนี้:

ตัวดำเนินการคำอธิบาย
+บวกสอง operands: A + B จะได้ 30
-ลบ operand ตัวที่สองจากตัวแรก: A - B จะได้ -10
*คูณทั้งสอง operands: A * B จะได้ 200
/หารตัวเศษด้วยตัวส่วน: B / A จะได้ 2
%Modulus Operator และเศษหลังการหารจำนวนเต็ม: B % A จะได้ 0
++Increment operator เพิ่มค่า integer ขึ้น 1: A++ จะได้ 11
--Decrement operator ลดค่า integer ลง 1: A-- จะได้ 9

ตัวดำเนินการเชิงสัมพันธ์ (Relational Operators)

สมมติตัวแปร A มีค่า 10 และ B มีค่า 20 ดังนี้:

ตัวดำเนินการคำอธิบาย
==ตรวจสอบว่าค่าของสอง operands เท่ากันหรือไม่ ถ้าใช่ condition จะเป็น true (A == B) เป็น false
!=ตรวจสอบว่าค่าของสอง operands ไม่เท่ากัน ถ้าไม่เท่ากัน condition จะเป็น true (A != B) เป็น true
>ตรวจสอบว่า operand ซ้ายมากกว่า operand ขวาหรือไม่ ถ้าใช่ condition จะเป็น true (A > B) เป็น false
<ตรวจสอบว่า operand ซ้ายน้อยกว่า operand ขวาหรือไม่ ถ้าใช่ condition จะเป็น true (A < B) เป็น true
>=ตรวจสอบว่า operand ซ้ายมากกว่าหรือเท่ากับ operand ขวา ถ้าใช่ condition จะเป็น true (A >= B) เป็น false
<=ตรวจสอบว่า operand ซ้ายน้อยกว่าหรือเท่ากับ operand ขวา ถ้าใช่ condition จะเป็น true (A <= B) เป็น true

ตัวดำเนินการตรรกะ (Logical Operators)

สมมติตัวแปร A มีค่า 1 และ B มีค่า 0 ดังนี้:

ตัวดำเนินการคำอธิบาย
&&Logical AND operator หากทั้งสอง operands ไม่เป็นศูนย์ condition จะเป็น true (A && B) เป็น false
||Logical OR Operator หาก operands ใด operands หนึ่งไม่เป็นศูนย์ condition จะเป็น true (A || B) เป็น true
!Logical NOT Operator ใช้กลับสถานะตรรกะของ operand หาก condition เป็น true จะกลายเป็น false !(A && B) เป็น true

ตัวดำเนินการบิต (Bitwise Operators)

Bitwise operator ทำงานบนบิตและดำเนินการทีละบิต ตารางความจริงสำหรับ &, |, และ ^ มีดังนี้:

p q p&q p|q p^q
0 0 0   0   0
0 1 0   1   1
1 0 0   1   1
1 1 1   1   0

สมมติ A = 60; และ B = 13; ในรูปแบบ binary จะเป็น:

A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A  = 1100 0011
ตัวดำเนินการคำอธิบาย
&Binary AND Operator คัดลอกบิตไปยังผลลัพธ์หากมีอยู่ในทั้งสอง operands (A & B) จะได้ 12 คือ 0000 1100
|Binary OR Operator คัดลอกบิตหากมีอยู่ใน operand ใด operand หนึ่ง (A | B) จะได้ 61 คือ 0011 1101
^Binary XOR Operator คัดลอกบิตหากตั้งค่าใน operand หนึ่งแต่ไม่ใช่ทั้งสอง (A ^ B) จะได้ 49 คือ 0011 0001
~Binary Ones Complement Operator เป็น unary มีผล 'กลับ' บิต (~A) จะได้ -61 คือ 1100 0011 ในรูป 2's complement เนื่องจาก signed binary number
<<Binary Left Shift Operator ค่าของ operand ซ้ายเลื่อนซ้ายตามจำนวนบิตที่ operand ขวาระบุ A << 2 จะได้ 240 คือ 1111 0000
>>Binary Right Shift Operator ค่าของ operand ซ้ายเลื่อนขวาตามจำนวนบิตที่ operand ขวาระบุ A >> 2 จะได้ 15 คือ 0000 1111

ตัวดำเนินการกำหนดค่า (Assignment Operators)

ตัวดำเนินการคำอธิบาย
=Simple assignment operator กำหนดค่าจาก operand ขวาไป operand ซ้าย C = A + B จะกำหนดค่า A + B ให้ C
+=Add AND assignment operator บวก operand ขวาเข้ากับ operand ซ้ายและกำหนดผลลัพธ์ให้ operand ซ้าย C += A เทียบเท่า C = C + A
-=Subtract AND assignment operator ลบ operand ขวาออกจาก operand ซ้ายและกำหนดผลลัพธ์ให้ operand ซ้าย C -= A เทียบเท่า C = C - A
*=Multiply AND assignment operator คูณ operand ขวากับ operand ซ้ายและกำหนดผลลัพธ์ให้ operand ซ้าย C *= A เทียบเท่า C = C * A
/=Divide AND assignment operator หาร operand ซ้ายด้วย operand ขวาและกำหนดผลลัพธ์ให้ operand ซ้าย C /= A เทียบเท่า C = C / A
%=Modulus AND assignment operator หา modulus ด้วยสอง operands และกำหนดผลลัพธ์ให้ operand ซ้าย C %= A เทียบเท่า C = C % A
<<=Left shift AND assignment operator C <<= 2 เหมือน C = C << 2
>>=Right shift AND assignment operator C >>= 2 เหมือน C = C >> 2
&=Bitwise AND assignment operator C &= 2 เหมือน C = C & 2
^=Bitwise exclusive OR and assignment operator C ^= 2 เหมือน C = C ^ 2
|=Bitwise inclusive OR and assignment operator C |= 2 เหมือน C = C | 2

ตัวดำเนินการทั่วไป (Misc Operators)

ลำดับตัวดำเนินการ
1sizeof operator คืนค่าขนาดของตัวแปร เช่น sizeof(a) โดย a เป็น integer จะคืนค่า 4
2Conditional operator (?:) หาก Condition เป็น true คืนค่า X มิฉะนั้นคืนค่า Y
3Comma operator ทำให้ลำดับการดำเนินการเกิดขึ้น ค่าของ comma expression ทั้งหมดคือค่าของ expression ตัวสุดท้ายในรายการที่คั่นด้วย comma
4(dot) และ -> (arrow) Member operators ใช้สำหรับอ้างอิงสมาชิกแต่ละตัวของ classes, structures, และ unions
5() Casting operators แปลงชนิดข้อมูลหนึ่งเป็นอีกชนิดหนึ่ง เช่น int(2.2000) จะคืนค่า 2
6Pointer operator & คืนค่าที่อยู่ของตัวแปร เช่น &a จะให้ที่อยู่จริงของตัวแปร
7Pointer operator * คือ pointer ไปยังตัวแปร เช่น *var จะเป็น pointer ไปยังตัวแปร var