⬛ >> versus >>>
Java的位元運算有兩個很類似的右移運算子,目的是將其唯一的運算元進行往右的位元平移。
當運算元為正時,兩者結果相同,分不出來;遇負數時,兩者結果才有差異。
說明如下:
邏輯右移運算子 >>>: number >>> bits; 表示將2進位數字右移bits位元,左邊補0
算術右移運算子 >> : number >> bits; 也表示將2進位數字右移bits位元,左邊保留目前符號位元
例1: 對於負數,兩種位元右移結果的比較: >> versus >>>
int n = -7;
System.out.printf("n = %d (%x)\n",n,n);
System.out.printf("n >> 2 = %d (%x)\n",n >> 2,n >> 2);
System.out.printf("n >>> 2 = %d (%x)\n",n >>> 2, n >>> 2);
則3行輸出如下:
n = -7 (fffffff9) // 二進位表示為 1111 1111 1111 1111 1111 1111 1111 1001
n >> 2 = -2 (fffffffe) // 二進位表示為 1111 1111 1111 1111 1111 1111 1111 1110
n >>> 2 = 1073741822 (3ffffffe) // 二進位表示為 0011 1111 1111 1111 1111 1111 1111 1110
其中,第一行二進位為原始數字;
第二行運算將二進位數字往右平移2位元,左邊補目前符號位元1;
第三行運算將二進位數字往右平移2位元,左邊補0
註: 須要維持正負號的右移運算可用 >>
註: 不補符號位元的右移運算可用 >>>
註: 至於左移運算子,因為右邊永遠補0,沒有其他選擇,所以只有一種 << 運算子
⬛ & versus && as well as | versus ||
Java的且及或運算也各有很接近的運算子,其說明如下:
邏輯且: b = bool_1 && bool_2; // 有短路求值,遇bool_1假,不看bool_2值,一律回傳假
位元且: b = bool_1 & bool_2; // 無短路求值
n = number_1 & number_2;
邏輯或: b = bool_1 || bool_2; // 有短路求值,遇bool_1真,不看bool_2值,一律回傳真
位元或: b = bool_1 | bool_2; // 無短路求值
n = number_1 | number_2;
例2: 對於數字及布林值,兩種【且】運算的比較: & versus &&
System.out.printf("true & false = %b\n", true & false);
System.out.printf("true && false = %b\n", true && false);
System.out.printf("1100 & 0101 = %x\n", 0b1100 & 0b0101);
//System.out.printf("1100 && 0101 = %d\n", 0b1100 && 0b0101);
則3行輸出如下:
true & false = false
true && false = false
1100 & 0101 = 4 // 二進位表示 為0100
其中,第一行兩布林值進行【位元且】運算,只當兩布林值同時為真時才為真,故得到假;
第二行兩布林值進行【邏輯且】運算,只當兩布林值同時為真時才為真,故得到假;
第三行兩數字進行【位元且】運算,只留下兩數字同時為1的位元才為1,故得到0100=4。
另外,第四行語法不允許兩數字進行【邏輯且】運算,故註解起來。
例3: 對於數字及布林值,兩種【或】運算的比較: | versus ||
System.out.printf("true | false = %b\n", true | false);
System.out.printf("true || false = %b\n", true || false);
System.out.printf("1100 | 0101 = %x\n", 0b1100 | 0b0101);
//System.out.printf("1100 || 0101 = %d\n", 0b1100 || 0b0101);
則3行輸出如下:
true | false = true
true || false = true
1100 | 0101 = d // 二進位表示 為1101
其中,第一行兩布林值進行【位元或】運算,只當兩布林值同時為假時才為假,故得到真;
第二行兩布林值進行【邏輯或】運算,只當兩布林值同時為假時才為假,故得到真;
第三行兩數字進行【位元或】運算,只留下兩數字同時為0的位元才為0,故得到1101=13=d。
另外,第四行語法不允許兩數字進行【邏輯或】運算,故註解起來。
例4: 對於布林值,短路求值與否的【且】運算比較: & versus &&
b = b1 = b2 = true;
b = (b1=false) & (b2=false);
System.out.printf("no short circuit evaluation: b:%b, b1:%b, b2:%b\n",b,b1,b2);
b = b1 = b2 = true;
b = (b1=false) && (b2=false);
System.out.printf("use short circuit evaluation:b:%b, b1:%b, b2:%b\n",b,b1,b2);
則2行輸出如下:
no short circuit evaluation: b:false, b1:false, b2:false
use short circuit evaluation:b:false, b1:false, b2:true
其中,第一行輸出不作短路求值,所以b1及b2皆接收到false值;
第二行輸出有作短路求值,所以b1接收回傳false值後,不須進行b2=false運算,就可判定b為假,故b2維持true值。
例5: 對於布林值,短路求值與否的【或】運算比較: | versus ||
b = b1 = b2 = false;
b = (b1=true) | (b2=true);
System.out.printf("no short circuit evaluation: b:%b, b1:%b, b2:%b\n",b,b1,b2);
b = b1 = b2 = false;
b = (b1=true) || (b2=true);
System.out.printf("use short circuit evaluation:b:%b, b1:%b, b2:%b\n",b,b1,b2);
則2行輸出如下:
no short circuit evaluation: b:true, b1:true, b2:true
use short circuit evaluation:b:true, b1:true, b2:false
其中,第一行輸出不作短路求值,所以b1及b2皆接收到true值;
第二行輸出有作短路求值,所以b1接收回傳true值後,不須進行b2=true運算,就可判定b為真,故b2維持false值。
註: 數字的且/或運算只能用 &, |
註: 條件式或布林值的且/或運算,若須要短路求值可用 &&, ||
註: 條件式或布林值的且/或運算,若不須要短路求值可維持 &, |
參考:
1.StackOverflow: Difference between >>> and >>
2.Wiki: Short Circuit Evaluation
java right shift and and/or operators in comparison
how to create an auto-scoring assignment for network planning using cisco packet tracer
思科的封包追蹤模擬軟體Packet Tracer可以自動評分, 適合當成網路規劃課程的練習、作業或小考題目。 其出題過程分成如下7階段: 0.Extensions/Activity Wizard/Password: 設定密碼 1.Answer Network/Show Answer Network: 建立答案版的網路拓樸 2.Instructions: 建立html標籤版的題目說明 3.Answer Network/Assessment Tree/Assessment Items 勾選哪些設備的哪些設定要當評分項,配分多少 宜勾選[X] Show Checked Only,只顯示勾選評分項的答案及配分,方便一目了然 從Answer Network/Settings頁籤,可設定如下一種得分的回饋模式(Feedback Settings): No Dynamic Feedback 不回饋 Show Score 顯示答對的分數 Show Item Count 顯示答對的評分項個數 Show Score Percentage 顯示答對的分數百分比 Show Item Count Percentage 顯示答對的評分項個數百分比 (預設值) 從Answer Network/Settings頁籤,另可如下鎖定用戶基本資料(Name,Email),防止答案檔分享: [X] User Profile Locking 用戶必須綁定某基本資料,作答中不得再行變更,否則答案歸零 [X] No Guest Profile 用戶不允許綁定預設訪客基本資料 4.Initial Network/Copy from Answer Network 先拷貝答案拓樸,再刪除設定值或連線,留下初始網路拓樸 設定喜好 Options/Preferences: Interface/Customize User Experience: ..... Show Device Name Labels ..... Disable Auto Cable Use Metric System (Uncheck to use Imperial) Show Link Lights ..... Show Port Labels When Mouse Over in Logical Workspace ..... Hide/Customize User Experience: Hide Physical Tab ..... Hide Router/Switch CLI Tab Hide Services Tab ..... Hide Attributes Tab Hide Wireless/Cellular Connection Hide Legacy Equipment Show Wireless Grid Fill Wireless Grid With Pattern ..... 設定哪些功能要鎖定 Initial Network/Locking Options/Locking Items Interface Switching to Logical Workspace ..... Preferences (Deprecated) Activity View Assessment Items 註: 預設可查看評分項得分,若勾選表示不允許 ..... Check Results 註: 預設可按鈕查看活動成績(含評分項得分及連線測試),若勾選表示不允許 Reset Activity 註: 預設可重置回初始拓樸,若勾選表示不允許 ..... Topology Global Use Config Tab 註: 預設設備有組態頁籤,若勾選表示不允許 Existing Devices Existing Annotations Clusters Remove Networks Physical Locations Simulation Global Existing Scenarios 5.Variable Manager: 建立IP/數字/字串的亂數範圍(Pool)及變數名稱(Variable),填入(1)~(4)中 Seeds/Number/Strings/IP Addresses -- (1) Answer Network/Show Answer Network的設備名稱可填入[[亂數變數名]] (2) Instructions的題目變動部份可填入[[亂數變數名]] (3) Answer Network/Assessment Tree/Assessment Items的評分項答案可填入[[亂數變數名]] (4) Initial Network/Show Initial Network的設備名稱可填入[[亂數變數名]] 6.複查題目初始為0,最後可達滿分方法: 對一份完成題目,存檔名為my_1.pka,檢查步驟如下: (a) 打開題目,解開密碼,設定 Answer Network/Settings: Show Item Count回饋模式 Initial Network/Locking Items: View Assessment Items及Check Results不鎖定 另存新檔名my_1_check0.pka (b) 利用如下操作,切換到答案拓樸 Initial Network/Copy from Answer Network 另存新檔名my_1_check100.pka (c) 打開my_1_check0.pka,檢查完成度是否為Completion: 0/? 若非0,利用選單Check Activity/Check Results/Assessment Items看哪些評分項得分無法歸零 記錄問題點 (d) 打開my_1_check100.pka,檢查完成度是否為Completion: ?/? 若非全拿,利用選單Check Activity/Check Results/Assessment Items看哪些評分項得分無法拿分 記錄問題點 (e) 將(c),(d)問題點的修正反應在my_1.pka,適當修改如下設定: Initial Network/Initial Network Setup: 可針對各評分項改變初始設定值 Answer Network/Assessment Tree/Assessment Items: 可針對無法歸零或滿分的評分項放棄勾選,不列入評分 另存新檔名my_2,pka,回到(a)進入下一輪修訂,直到題目初始為0,最後可達滿分為止 註: 有時候因為未知原因,在設定5.亂數變數之後,原來無法歸零的評分項得分可以歸零列入評分 註: 無線AP在發放DHCP時,常出現筆電接收不到訊息,造成無法為0或滿分,宜放棄筆電端評分項 註: 由於Packet Tracer本身問題,有可能明明設定正確,卻無法滿分,這時只能割捨該評分項,不列入計分
weka.classifiers.functions.MultilayerPerceptron
weka.classifiers.functions.MultilayerPerceptron 為多層感知機學習器。
使用具備輸入層、隱藏層、及輸出層的類神經結構,以倒傳遞法學習各層之間的連結權重,於輸出層進行類別或數值預測。
若遇名目屬性,將先進行二值化處理再學習及預測。
參數說明:
-L <learning rate> 依據下降梯度的多少百分比更新權重,稱為學習速率,介於[0,1]之間,預設值 0.3。
-M <momentum> 參考上回權重更新量的多少百分比更新本回權重,稱為更新動量,介於[0,1]之間,預設值 0.2。
-N <number of epochs> 訓練迭代次數,預設值 500。
-V <percentage size of validation set> 連續變差終止訓練用的驗證集大小比例,介於[0,100],預設值0。
-S <seed> 亂數產生器種子,值應>=0,預設值為0。
-E <threshold for number of consequetive errors> 網路終止前驗證集允許的連續錯誤門檻,值應>0,預設值20。
-G 開啟圖形介面,預設不開啟圖形介面。
-A 不要自動建立網路連結,只在開啟圖形介面(-G)才有作用。
-B 不要自動使用【名目轉成二元】屬性過濾器,預設使用【名目轉成二元】屬性過濾器。
-H <comma seperated numbers for nodes on each layer> 建立網路的隱藏層。
0表示不要隱藏層,預設值是a。決定隱藏層逐層的節點個數值可以是
i表示輸入層屬性個數, o表示輸出層類別數, t表示i+o總數, a表示i及o平均值。
-C 不要對數值類別輸出作正規化,預設數值類別輸出會作正規化
-I 不要對屬性輸入值作正規化,預設屬性輸入值會作正規化,名目屬性值將介於[-1,1]之間
-R 不允許重設網路
-D 學習速率會衰減,預設不衰減
>java -cp weka.jar;. weka.classifiers.functions.MultilayerPerceptron -t data\weather.numeric.arff
-L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a
Options: -L 0.3 -M 0.2 -N 500 -V 0 -S 0 -E 20 -H a
Sigmoid Node 0
Inputs Weights
Threshold -3.248835441689124
Node 2 5.706344521860183
Node 3 2.443270263208691
Node 4 2.6425576499015655
Node 5 2.5103414057156117
Sigmoid Node 1
Inputs Weights
Threshold 3.247940047055843
Node 2 -5.7047440571074866
Node 3 -2.3959635449403223
Node 4 -2.61941341516743
Node 5 -2.57892674553124
Sigmoid Node 2
Inputs Weights
Threshold -1.4298110453038173
Attrib outlook=sunny 1.2796074137730873
Attrib outlook=overcast 2.5993304643376662
Attrib outlook=rainy -2.482189408449902
Attrib temperature -0.991784436689735
Attrib humidity -4.132575972523981
Attrib windy -0.8030823939514043
Sigmoid Node 3
Inputs Weights
Threshold -0.7740672340804496
Attrib outlook=sunny -1.9100370742566128
Attrib outlook=overcast 2.3822068707682824
Attrib outlook=rainy 0.2349921312574373
Attrib temperature -0.8639638424331715
Attrib humidity -0.8117295111072012
Attrib windy 3.0923597946788437
Sigmoid Node 4
Inputs Weights
Threshold -0.7812523749731839
Attrib outlook=sunny -2.0149350612947305
Attrib outlook=overcast 2.4850160661055654
Attrib outlook=rainy 0.2429746779978898
Attrib temperature -0.9010443938018432
Attrib humidity -0.8326891162034927
Attrib windy 3.255120039808521
Sigmoid Node 5
Inputs Weights
Threshold -0.7574102682219431
Attrib outlook=sunny -1.9605922799976891
Attrib outlook=overcast 2.481930135373603
Attrib outlook=rainy 0.2838381715677166
Attrib temperature -0.8613350411165092
Attrib humidity -0.775628050353589
Attrib windy 3.169910152935346
Class yes
Input
Node 0
Class no
Input
Node 1
註: 學到的神經網路如下圖,其中,黃色兩個節點由上而下分別為Node 0, Node 1。
紅色4個節點由上而下分別為Node 2, Node 3, Node 4, Node 5。
隱藏層會有4個節點的理由為參數-H a,而 a=(i + o)/2=(6 + 2)/2=4。
Time taken to build model: 0.1 seconds
Time taken to test model on training data: 0.01 seconds
=== Error on training data ===
Correctly Classified Instances 14 100 %
Incorrectly Classified Instances 0 0 %
Kappa statistic 1
Mean absolute error 0.036
Root mean squared error 0.0454
Relative absolute error 7.7533 %
Root relative squared error 9.4618 %
Total Number of Instances 14
=== Confusion Matrix ===
a b <-- classified as
9 0 | a = yes
0 5 | b = no
=== Stratified cross-validation ===
Correctly Classified Instances 11 78.5714 %
Incorrectly Classified Instances 3 21.4286 %
Kappa statistic 0.5116
Mean absolute error 0.265
Root mean squared error 0.4627
Relative absolute error 55.6497 %
Root relative squared error 93.7923 %
Total Number of Instances 14
=== Confusion Matrix ===
a b <-- classified as
8 1 | a = yes
2 3 | b = no
如下 weather.numeric.arff 案例集的14個案例利用2個文字屬性及2個數字屬性,預測文字屬性。
| outlook | temperature | humidity | windy | play |
| sunny | 85 | 85 | FALSE | no |
| sunny | 80 | 90 | TRUE | no |
| rainy | 65 | 70 | TRUE | no |
| sunny | 72 | 95 | FALSE | no |
| rainy | 71 | 91 | TRUE | no |
| overcast | 83 | 86 | FALSE | yes |
| rainy | 70 | 96 | FALSE | yes |
| rainy | 68 | 80 | FALSE | yes |
| overcast | 64 | 65 | TRUE | yes |
| sunny | 69 | 70 | FALSE | yes |
| rainy | 75 | 80 | FALSE | yes |
| sunny | 75 | 70 | TRUE | yes |
| overcast | 72 | 90 | TRUE | yes |
| overcast | 81 | 75 | FALSE | yes |
訂閱:
意見 (Atom)
Linked Lists from C to Java
「 C Pointer Concepts in Java 」一文提到 Java 沒有指標型別 (pointer type) ,但有參照型別 (reference type) 的設計。在遇到須要處理鏈結清單 (linked list)、圖形 (graph) 等資料結構時,Java ...