powerbuilder 的邏輯運算函數

powerbuilder 的位元運算能力幾乎同等『沒有』。

但是使用 windows API 又常常遇到許多變數需要邏輯運算過,但是看看目前的 PFC 的邏輯運算效能也不好。

因此突發奇想,把原本 PFC 的邏輯運算改一改,發現也蠻好用的,效能上也有改善。

Function名稱:f_bitoperator
回傳值:unsignedlong
傳遞參數:aul_s1 [unsignedlong][value]、aul_s2 [unsignedlong][value]、as_operator [string][value]

/* PFC簡易版兩組數字做邏輯運算 輸入來源:aul_s1 [uLong],aul_s2 [uLong],as_operator [String] as_operator為邏輯運算描述如:"and" , "or" , "xor" , "not"只需要第一組(aul_s1)輸入, aul_s2放0就可以了 返回:運算結果[Long],當參數錯誤返回NULL */ uLong ll_ret boolean lb_s1[32] , lb_s2[32] , lb_u integer li_bit as_operator = lower(trim(as_operator)) //參數檢查 if isnull(aul_s1) or isnull(aul_s2) or isnull(as_operator) then setnull(ll_ret) Return ll_ret end if if as_operator = "" then setnull(ll_ret) Return ll_ret end if //位元轉換 for li_bit = 32 to 1 step -1 if mod(aul_s1 , 2) = 1 then lb_s1[li_bit] = true else lb_s1[li_bit] = false end if aul_s1 = truncate(aul_s1 / 2 , 0) next if as_operator <> "not" then for li_bit = 32 to 1 step -1 if mod(aul_s2 , 2) = 1 then lb_s2[li_bit] = true else lb_s2[li_bit] = false end if aul_s2 = truncate(aul_s2 / 2 , 0) next end if //邏輯運算 ll_ret = 0 for li_bit = 32 to 1 step -1 choose case as_operator case "and" lb_u = lb_s1[li_bit] and lb_s2[li_bit] case "or" lb_u = lb_s1[li_bit] or lb_s2[li_bit] case "xor" lb_u = not (lb_s1[li_bit] or lb_s2[li_bit]) case "not" lb_u = not lb_s1[li_bit] end choose if lb_u then ll_ret += 2^(32 - li_bit) next Return ll_ret

雖然說可以用 VC++ 寫一個支援的 DLL 來作邏輯運算,但是殺雞用牛刀好像小題大作了。

其次,在某些特別環境下用 VC++ 寫的 DLL 要重新編譯過,例如:Pocket Powerbuilder 在做手持終端設備開發就不太方便了。

這次改的部份大致有兩點:一、邏輯儲存使用 Boolean 陣列,節省轉換與記憶體運算空間,二、這個 Function 就可以做到 AND、OR、XOR、NOT 四種運算了,使用上很方便。

另外,就是 in/out 使用 UnsignedLong 長達 32 bit 的運算,即使你是傳遞 integer、long 都沒問題,
負數也是可以邏輯運算的。 ^_^

留言

這個網誌中的熱門文章

【研究】列印的條碼為什麼很難刷(掃描)

C# 使用 Process.Start 執行外部程式

統一發票列印小程式