1 / 15

第 六 章

第 六 章. 基本位元運算. 大綱. 6-1 整數之位元表示 6- 2 運算符號 6-3 & 運算子 6-4 | 運算子 6-5 ^ 運算子 6-6 ~ 補數運算子 6-7 << 左移及 >> 右移運算子. 位元. 15. 14. 13. 12. 11. 10. 9. 8. 7. 6. 6. 4. 3. 2. 1. 0. 值. a=. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 3. b=. 1. 1. 1. 1. 1. 1.

imani-short
Download Presentation

第 六 章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第六章 基本位元運算

  2. 大綱 • 6-1 整數之位元表示 • 6-2 運算符號 • 6-3 &運算子 • 6-4 | 運算子 • 6-5 ^運算子 • 6-6 ~補數運算子 • 6-7 << 左移及 >> 右移運算子

  3. 位元 15 14 13 12 11 10 9 8 7 6 6 4 3 2 1 0 值 a= 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 3 b= 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 -8 代表值 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1 6-1 整數之位元表示 • C++之整數資料的表示(以16位元為例): int a=3,b=-8 • 第16位元(Bit 15)若為0表該值為正,1表該值為負。 • a=0000 0000 0000 0011,以二進位表示為2+1=3。 • b=1111 1111 1111 1000 , -32768 + 16384 + 8192 + 4096 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 = -8,另一算法為若0位數少的話,只加0的位元之代表值及加1後再加上負號,即-(4+2+1+1)=-8。 • 整數最大值32767(0111 1111 1111 1111) ,最小值-32768(1000 0000 0000 0000) 。

  4. 運算符號 意義 說明 & AND 而且 | OR 或者 ^ XOR 互斥 ~ Complement 補數 << Shift Left 左移 >> Shift Right 右移 6-2 運算符號 • 位元運算如下

  5. a b & | ^ ~a ~b 0 0 0 0 0 1 1 1 0 0 1 1 0 1 0 1 0 1 1 1 0 1 1 1 1 0 0 0 6-3 『&』運算子 • 運算方式與邏輯運算子(&&)相同,真值表如下: • 例如: a=3 → 0000 0000 0000 0011 b=7 → 0000 0000 0000 0111 c=a&b → 0000 0000 0000 0011 → c=3

  6. 例題: 輸入任意兩整數,求其&之值。 #include <iostream> //cout using namespace std; int main( ){ int a,b; cout << "Input two integers a , b = "; cin >> a >> b; cout << "a & b = " << (a & b) << endl; return 0; }

  7. a b | ^ ~a ~b 0 0 0 0 1 1 1 0 1 1 0 1 0 1 1 1 1 0 1 1 1 0 0 0 6-4 『|』運算子 • 『|』運算子與邏輯運算子(||)相同,真值表如下: • 例如: a=3 → 0000 0000 0000 0011 b=7 → 0000 0000 0000 0111 c=a|b → 0000 0000 0000 0111 → c=7

  8. 例題: 輸入任意一整數,求其與000F之「|」之值。 #include <iostream> //cout using namespace std; int main( ){ int a; cout << "Input one integers a = "; cin >> a; cout << "a | 0x000F = " << (a | 0x000F) << endl; return 0; // 0x000F→0000 0000 0000 1111 }

  9. a b ^ ~a ~b 0 0 0 1 1 1 0 1 0 1 0 1 1 1 0 1 1 0 0 0 6-5 『^』運算子 • 『^』運算子互斥(Exclusive or)相同的位元相互排斥,結果為0,不相同位元相吸,結果為1 ,真值表如下: • 例如: a=3 → 0000 0000 0000 0011 b=7 → 0000 0000 0000 0111 c=a^b → 0000 0000 0000 0100 → c=4

  10. 例題: 輸入任意兩整數,求兩數之『^』值。 #include <iostream> //cout using namespace std; int main( ){ int a,b; cout << "Input two integers a , b = "; cin >> a >> b; cout << "a ^ b = " << (a ^ b) << endl; return 0; }

  11. a b ~a ~b 0 0 1 1 1 0 0 1 0 1 1 0 1 1 0 0 6-6 『~』補數運算子 • 『~』補數運算子似邏輯運算子之not,將0變為1,將1變為0 真值表如下: • 例如: a=3 → 0000 0000 0000 0011 c=~a → 1111 1111 1111 1100 → c=-4

  12. 例題: 輸入任意兩整數,分別求兩數之補數值。 #include <iostream> //cout using namespace std; int main( ){ int a,b; cout << "Input two integers a , b = "; cin >> a >> b; cout << “~a = " << (~a) << endl; cout << “~b= " << (~b) << endl; return 0; }

  13. 6-7 『<<』左移及『>>』右移運算子 • 『<<』左移運算子: 將所有位元向左移位,最左位元移出,最右位元補0 。結果相當於乘2。 • 例如: a=3 → 0000 0000 0000 0011 c=a<< 1 → 0000 0000 0000 0110 → c=6 • 『>>』右移運算子: 將所有位元向右移位,最右位元移出,最左位元補0 。結果相當於除2求商。 • 例如: a=7 → 0000 0000 0000 0111 c=a>> 1 → 0000 0000 0000 0011 → c=3

  14. 例題: 分別將17向左移二位,20向右移三位,結果分別以10及16進位輸出。 #include <iostream> //cout using namespace std; int main( ){ int a=17,b=20,c; c=(a << 2); cout << a <<" << 2 = " << c << endl; cout << "Hex = " << hex << c << endl; c=(b >> 3); cout << b << " >> 3 = " << dec << c << endl; cout << "Hex = " << hex << c << endl; return 0; }

  15. 第六章習題 • 輸入任意整數將其轉為2進位,8進位,16進位後輸出。 • 輸入任意二進位(16位元)將其轉為8,10,16進位後輸出。

More Related