Re: [問題] 徵求bit compare作法?

看板C_and_CPP作者 (藍影)時間14年前 (2011/08/16 14:28), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《WillyLin (我是我 我不是我)》之銘言: : 我本來也想用memcmp : 但想到用memcmp之前要先把buf1 shift copy到另一個新的buffer : (然後再豪邁的call memcmp一口氣比完...) : 這個shift copy的過程其實就已經和上面compare差不多 : 且長度未必是8的倍數 : 最後留下的幾個bit 還是要自己處理 恕刪。 對微處理機沒研究,不知這方法您試過沒 假設 #bit = 9~35,其中會用到的 Array index 含 Arr[1] : bit2 ~ bit7 (9 ~15) Arr[2] : bit0 ~ bit7 (16~23) Arr[3] : bit0 ~ bit7 (24~31) Arr[4] : bit0 ~ bit3 (32~35) 一開始先做完整之筆對部份 ---- U16 SBit = 9 , EBit = 35; /* Start bit and End Bit */ /* 以下計算請自行確認 */ U16 SIndex = SBit >> 3; /* SIndex = SBit / 8 */ U16 EIndex = EBit >> 3; /* EIndex = SBit / 8 */ U16 CompLen = EIndex - SIndex + 1; /* memcpy compare length */ if ( memcmp( (void*)(Arr1+SIndex), (void*)(Arr2+SIndex), CompLen ) return 0; 我其實比較建議這裡用 memcmp 做,不建議用 for 做 雖都是 O(n),但要寫贏它,真的不容易 ( 微處理機便不確定.. ) ---- 剩下的, Arr[1] bit2~bit7 與 Arr[4] bit0~bit3,以 Arr[1] 而言,我看您大致的作法, 應是將 Arr[1] bit2~bit7 修正成 bit0~bit5,做 mask 後再比對, Arry[4] 應也是類似作法,此處無異。 參考一下。 -- When I saw the turth of love, I feel the pain which the world brings to me. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 180.177.78.41 ※ 編輯: tropical72 來自: 180.177.78.41 (08/16 14:36)
文章代碼(AID): #1EIWtrLD (C_and_CPP)
文章代碼(AID): #1EIWtrLD (C_and_CPP)