Professional Documents
Culture Documents
Bit 3
Bit 3
คอมพิวเตอร์
ตัวกระทำกำรแบบบิต
2
ตัวกระทำกำรแบบบิต
(Bitwise Operator)
ตัวกระทำกำรแบบบิต
(Bitwise Operator)
|(OR) เช่ น
○ 1 |1 :1 100 101
○ 1 |0 :1
100 110
○ 0 |1 :1
100 111
○ 0 |0 :0
ข้อสังเกต ถ้าบิตเป็ น 0 ทัง้ คู่ จะได้ 0 ที่เหลือได้ 1
4
ตัวกระทำกำรแบบบิต
(Bitwise Operator)
^(XOR) เช่ น
1 ^1 :0
1^0 :1 100 101
0^1 :1
0^0 :0 100 110
000 011
ข้อสังเกต บิตเหมือนกันเป็ น 0 ต่างกันได้ 1
5
Exercise 1
จงหาผลลัพธ์ของ จงหาผลลัพธ์ของ
1111 0000 & 1111 0000 &
1010 1010 0011 1100
Exercise 2
จงหาผลลัพธ์ของ จงหาผลลัพธ์ของ
1111 0000 | 1111 0000 |
1010 1010 0011 1100
1111 1010
Exercise 3
จงหาผลลัพธ์ของ จงหาผลลัพธ์ของ
1111 0000 ^ 1111 0000 ^
1010 1010 0011 1100
0101 1010
ตัวกระทำกำรเลือนบิ ่ ต
(Bitwise shift operator)
การเลือ
่ นบิตขวา Right-shift operator (>>)
่ 10 >> 1
เชน
แปลง 10 เป็ นฐาน 2 จะได ้
10=10102
10102 >> 1 จะได ้ 1012
ตัวกระทำกำรเลือนบิ ่ ต
(Bitwise shift operator)
การเลือ ้ Left-shift operator (<<)
่ นบิตซาย
่ 10 << 1
เชน
➢ แปลง 10 เป็ นฐาน 2 จะได ้
10=10102
10102 << 1 จะได ้ 101002
Exercise 4
จงหำผลลัพธ ์ของ
1. 11>>1
2. 6<<2
3. 4>>1
4. 14<<2
5. 17>>1
6. 22<<2
7. 15>>1
8. 30<<2
11
ลำดับในกำรทำงำนของ Operator
Operator Description
() Parentheses
** Exponentiation
+x -x ~x Unary plus, unary minus, and bitwise NOT
* / // % Multiplication, division, floor division, and modulus
+ - Addition and subtraction
<< >> Bitwise left and right shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
== != > >= < <= is is not in not in Comparisons, identity, and membership operators
not Logical NOT
and AND
or OR
12
Exercise 5
จงหำผลลัพธ ์ของ
SUM = 3+7*2-6*(5-2)//2+4**2+(5-6)
SUM = (3+7*2-6)*(5-2)//(2+4**2+5-6)
SUM = ((3+7*2-6*5-2)//(2+4**2)+(5-6))