# PB中位操作的实现代码

（三）编写公用函数以逐位获取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@]

• 博文量
115
• 访问量
527046