發表文章

Git標籤

查詢標籤 列出所有標籤 git tag 加標籤 對最後一次commit加註標籤 git tag [tag_name] 對最指定的commit加註標籤 git tag [tag_name] [commit_hash] Push特定標籤至remote端,需將標籤的commit都要先push上去,再執行 git push origin tag [tag_name] Push所有的標籤至remote端,需將所有有標籤的commit都要先push上去,再執行 git push --tags 刪標籤 刪除local端的標籤 git tag --delete tagname 或者縮寫為 git tag -d tagname 刪除remote端的標籤 git push --delete origin [tag_name]

Python邏輯判斷要用「is」還是「==」

標準答案是用「is」 如果是兩個布林數要比較,就用「is」,例如 a is b (i > 0) is True 讀者最關心的應該是這個吧,若是不想探究原因的話,可以把此網頁關掉回去寫程式了。 什麼是「is」 「is」條件成立,代表左右兩端是同一樣東西,而「==」條件成立,左右兩端的值一樣就好。 舉個例子,變數的值一樣,但是代表不同東西: a = [1] b = [1] print(a == b) # True print(a is b) # False b[0] = 2 print(a) # [1] print(b) # [2] 另一個例子,兩個變數代表同一個東西,當然,它們的值也會相同: a = [1] b = a print(a == b) # True print(a is b) # True b[0] = 2 print(a) # [2] print(b) # [2] 當 a is b 條件成立時,b有變動,a會跟著變,反之亦然,因為它們是代表同一個東西。 Python中None、True、False這幾個值都是固定且唯一,「固定」代表值永遠不變,「唯一」代表程式中的None都是同一個東西,True、False也是如此,用is會是最適合的 a is None a is True a is False 用「==」跟用「is」結果一樣嗎 了解「is」與「==」的不同後,是不是覺得用「==」跟用「is」結果可能會不一樣,可是先前的程式通篇用「==」也是跑得很正常? 先來看True和False是什麼 print(int(True)) # 1 print(int(False)) # 0 在「==」的判斷中,True和False的值會是1和0,現在可以展示「==」與「is」會有不一樣的結果: print(1 == True) # True print(0 == False) # True print(1 is True) # False print(0 is False) # False 好的,我明白各位讀者的程式中,不會有整數和布林數比較條件的寫法,所以通篇用「==」也是跑得會跟預期一樣,但...

Python的10進制與16進制轉換

16進制轉10進制 準確地說,是16進制的string轉成10進制的int。 print(int('ff', 16)) # 255 有前綴0x時,base可使用0,讓int()函式自動辨認是16進制 建議 print(int('0xff', 0)) # 255 斟酌 print(int('0xff', 16)) # 255 10進制轉16進制 使用hex()可以回傳有前綴0x的小寫16進制string。 print(hex(255)) # 0xff 也可以用format格式 print('{:x}'.format(255)) # ff print('{:X}'.format(255)) # FF 前面補0 建議 print('{:04x}'.format(255)) # 00ff 不建議 print('{:0>4x}'.format(255)) # 00ff 加上前綴0x 建議 print(hex(255)) # 0xff print('0x{:x}'.format(255)) # 0xff 斟酌 print('{}'.format(hex(255))) # 0xff print('{:#x}'.format(255)) # 0xff 加上前綴0x且補0 建議 print('0x{:04x}'.format(255)) # 0x00ff 不建議 print('{:#06x}'.format(255)) # 0x00ff

Git指令彙整

在本機下載專案 git clone user@website:my/path/projectname.git git clone git@github.com:zlargon/git-test.git 在本機下載專案 git clone -b fix-header git@github.com:zlargon/git-test.git 在本機下載專案 git clone -b fix-header --single-branch git@github.com:zlargon/git-test.git 在本機下載專案 git clone -b fix-header --single-branch git@github.com:zlargon/git-test.git 分支 列出本地(local)分支 git branch 列出遠程(remote)分支 git branch -r git branch --remotes 列出本地、遠程全部分支 git branch -a git branch --all 新增分支 git branch branch_name 新增develop git branch develop 將分支develop合併於master git checkout master git merge develop --no-ff 顯示小耳朵 git log --oneline --graph 刪除本地(local)分支 git branch -d branch-name 刪除遠程(remote)分支 git push origin -d branch-name git reset --hard git clean -f git clean -df git rebase HEAD~1 --exec "git commit --amend --no-edit --date 'now'" git rebase HEAD~2 --exec "git commit --amend --no-edit ...

設定CPU的Cache模式:MTRR

圖片
  MSR(Model-Specific Register)內記錄關於CPU的特性,在MSR有段被稱為MTRR(Memory-Type Range Registers),用來記錄快取記憶體的模式。 快取模式 有5種快取模式。 編碼 簡寫 名稱 00h UC Uncacheable 01h WC Write-Combining 02h Reserved 03h Reserved 04h WT Writethrough 05h WP Write-Protect 06h WB Writeback IA32_MTRRCAP IA32_MTRRCAP的位址在MSR[FEh],定義如下: 位元 標記 描述 63 ~ 11 Reserved 10 WC Write-combining type is available 9 Reserved 8 FIX All fixed-size MTRRs are available 7 ~ 0 VCNT Count of variable-size MTRRs supported IA32_MTRRdefType IA32_MTRRdefType的位址在MSR[2FFh],定義如下: 位...

字串連接:strcat()、strncat()

  比照 strcpy()與strncpy() ,做個筆記吧。順帶一提,一副貓樣的cat是concatenate的縮寫。 strncat() 函式宣告 char *strncat(char *dest, const char *src, size_t n); 測試範例 #include <stdio.h> #include <string.h> void main() { char src1[] = "abcd"; char src2[] = "123"; char arr[10]; int i, n; for (n = 0; n < sizeof(arr) - strlen(src1); n++) { memset(arr, '.', sizeof(arr)); strncpy(arr, src1, sizeof(src1)); printf("N = %d: ", n); strncat(arr, src2, n); for (i = 0; i < sizeof(arr); i++) { if (arr[i] == '\0') printf("%c ", '_'); else printf("%c ", arr[i]); } printf("\n"); } } 輸出如下: 顯示「.」代表未改變,保持原始值。 顯示「_」代表字串結尾字元「\0」。 N = 0: a b c d _ . . . . . N = 1: a b c d 1 _ . . . . N = 2: a b c d 1 2 _ . . . N = 3: a b c d 1 2 3 _ . . N = 4: a b c d 1 2 3 _ . . N = 5: a b ...

字串複製:strcpy()、strncpy()

  每次都為了有沒有包含字串結尾字元「\0」而煩惱,就做個筆記吧。 前言:sizeof()與strlen()   進入正題前,先看看字串長度,順便複習陣列和指標。 #include <stdio.h> #include <string.h> void main() { char arr1[] = {'1', '2', '3', '\0'}; char arr2[] = "123"; char *ptr = "123"; printf("--- arr1 ---\n"); printf("length of string : %lu\n", strlen(arr1)); printf("size of array : %lu\n", sizeof(arr1)); printf("--- arr2 ---\n"); printf("length of string : %lu\n", strlen(arr2)); printf("size of array : %lu\n", sizeof(arr2)); printf("--- ptr_arr1 ---\n"); printf("length of string : %lu\n", strlen(&arr1[0])); printf("size of pointer : %lu\n", sizeof(&arr1[0])); printf("--- ptr ---\n"); printf("length of string : %lu\n", strlen(ptr)); printf("size of pointer : %lu\n", sizeof(ptr)); } 輸出如下: ---...