ITPub博客

首页 > 应用开发 > IT综合 > PB中位操作的实现代码

PB中位操作的实现代码

原创 IT综合 作者:hljhrbsjf 时间:2006-11-22 14:25:54 0 删除 编辑
(三)编写公用函数以逐位获取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/,如需转载,请注明出处,否则将追究法律责任。

下一篇: VC快捷键大全
请登录后发表评论 登录
全部评论
  • 博文量
    115
  • 访问量
    527046