PB中位操作的實現程式碼

hljhrbsjf發表於2006-11-22
(三)編寫公用函式以逐位獲取BIT位資料:

public function boolean of_getbit (long al_decimal, unsignedinteger ai_bit);Boolean lb_null

If IsNull(al_decimal) or IsNull(ai_bit) then

SetNull(lb_null)

Return lb_null

End If

If Int(Mod(al_decimal / (2 ^(ai_bit - 1)), 2)) > 0 Then

Return True

End If

Return False

end function



(四)編寫AND與操作函式:

public function long of_bitwiseand (long al_value1, long al_value2);Integer li_Cnt

Long ll_Result

Boolean lb_Value1[32], lb_Value2[32]

If IsNull(al_Value1) Or IsNull(al_Value2) Then

SetNull(ll_Result)

Return ll_Result

End If

For li_Cnt = 1 To 32

lb_Value1[li_Cnt] = of_getbit(al_Value1, li_Cnt)

lb_Value2[li_Cnt] = of_getbit(al_Value2, li_Cnt)

Next

For li_Cnt = 1 To 32

If lb_Value1[li_Cnt] And lb_Value2[li_Cnt] Then

ll_Result = ll_Result + (2^(li_Cnt - 1))

End If

Next

Return ll_Result

end function



(五)編寫OR或操作函式:

public function long of_bitwiseor (long al_value1, long al_value2);Integer li_Cnt

Long ll_Result

Boolean lb_Value1[32], lb_Value2[32]

If IsNull(al_Value1) Or IsNull(al_Value2) Then

SetNull(ll_Result)

Return ll_Result

End If

For li_Cnt = 1 To 32

lb_Value1[li_Cnt] = of_getbit(al_Value1, li_Cnt)

lb_Value2[li_Cnt] = of_getbit(al_Value2, li_Cnt)

Next

For li_Cnt = 1 To 32

If lb_Value1[li_Cnt] Or lb_Value2[li_Cnt] Then

ll_Result = ll_Result + (2^(li_Cnt - 1))

End If

Next

Return ll_Result

end function



(六)編寫XOR異或操作函式:

public function long of_bitwisexor (long al_value1, long al_value2);Integer li_Cnt

Long ll_Result

Boolean lb_Value1[32], lb_Value2[32]

If IsNull(al_Value1) Or IsNull(al_Value2) Then

SetNull(ll_Result)

Return ll_Result

End If

For li_Cnt = 1 To 32

lb_Value1[li_Cnt] = of_getbit(al_Value1, li_Cnt)

lb_Value2[li_Cnt] = of_getbit(al_Value2, li_Cnt)

Next

For li_Cnt = 1 To 32

If (lb_Value1[li_Cnt] And Not lb_Value2[li_Cnt]) Or &

(Not lb_Value1[li_Cnt] And lb_Value2[li_Cnt]) Then

ll_Result = ll_Result + (2^(li_Cnt - 1))

End If

Next

Return ll_Result

end function



(七)編寫NOT否操作函式:

public function long of_bitwisenot (long al_value);Integer li_Cnt, li_Count

Long ll_Result

string ls_Value, ls_Result

If IsNull(al_Value) Then

SetNull(ll_Result)

Return ll_Result

End If

ls_Value = of_binary(al_Value)

li_Cnt = Len(ls_Value)

For li_Count = 1 To li_Cnt

If Mid(ls_Value, li_Count, 1) = '0' Then

ls_Result = ls_Result + '1'

Else

ls_Result = ls_Result + '0'

End If

End For

Return of_decimal(ls_Result)

end function



(八)呼叫上述函式:

//將十進位制數10轉換為二進位制1010

ls_binary = of_binary(10)

//將二進位制1010轉換為十進位制數10

ll_decimal = of_decimal(“1010”)

//執行AND與操作(55 && 44) = 36

ll_ret = of_bitwiseand(55,44) //返回36

//執行NOT否操作(! 55) = 8

ll_ret = of_bitwisenot(55) //返回8

//執行OR或操作(55 ¦ ¦ 44) = 63

ll_ret = of_bitwiseor(55,44) //返回63

//執行XOR異或操作(55 XOR 44) = 27

ll_ret = of_bitwisexor(55,44) //返回27[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/631872/viewspace-879154/,如需轉載,請註明出處,否則將追究法律責任。

相關文章