为什么BitXOR函数运算结果不正常?[已解决]
本帖最后由 superqvb 于 2012-8-25 16:24 编辑用计算器上可以算出:0xF1F0F3F2FD和0xF5F5F5F5F5的按位与或运算的10进制结果是17264150280(10位16进制0x0405060708),而用BitXOR函数得出的结果却是84281096(10位16进制0005060708),请高手指点。
MsgBox(0,"",Hex(BitXOR(0xF1F0F3F2FD,0xF5F5F5F5F5),10)) 转成二进制看,十进制看不出来 1111000111110000111100111111001011111101
1111010111110101111101011111010111110101
----------------------------------------------------------------bitxor
0000010000000101000001100000011100001000
(17264150280, 十进制)
是不是位数限制 回复 3# netegg
谢谢,运行那一行程序,结果却不是这样,不明白。 看帮助要仔细,帮助里写着:BitOR 位操作使用 32-位整数。貌似你这都40位了,超出其运算范围的运算,一般都会出错。另外,40位的参数我这里直接运行出错,根本不会得到错误的结果。 MsgBox(0,0,_BitXOR(0xF1F0F3F2FD,0xF5F5F5F5F5))
Func _BitXOR($s_value1, $s_value2 = 0)
Local $s_array = StringSplit($s_value1 & "|" & $s_value2, "|"), $l[$s_array + 1], $ret, $res, $max
For $i = 1 To $s_array
$s_array[$i] = _Binary($s_array[$i])
$l[$i] = StringLen($s_array[$i])
If $l[$i] > $max Then $max = $l[$i]
Next
For $j = 0 To $max - 1
$res = 0
For $i = 1 To $s_array
$res += StringMid($s_array[$i], $l[$i] - $j, 1)
Next
$ret = Mod($res, 2) & $ret
Next
Return _BinToInt($ret)
EndFunc ;==>_BitXOR
Func _Binary($s_number)
Local $b, $t, $max = Int(_LogX($s_number, 2)) + 1
For $i = $max To 0 Step - 1
$t = 2 ^ $i
If $s_number < $t Then
$b &= "0"
ContinueLoop
EndIf
$b &= "1"
$s_number -= $t
Next
If $b = "" Then $b = "0"
Return $b
EndFunc ;==>_Binary
Func _LogX($s_Result, $s_base)
Return Log($s_Result) / Log($s_base)
EndFunc ;==>_LogX
Func _BinToInt($s_string)
Local $res, $t, $j = StringLen($s_string)
For $i = 1 To $j
$t = StringMid($s_string, $i, 1)
If $t <> "0"And $t <> "1"Then Return SetError(-1, 0, -1)
If $t = "1"Then $res += 2 ^ ($j - $i); - ($j-$i>48)
; If $t = "1"Then $res += Int(2 ^ ($j - $i)); - ($j-$i>48)
Next
If $res = "" Then $res = 0
Return $res
EndFunc ;==>_BinToInt
好复杂头晕了 谢谢netegg !谢谢ajian55!
页:
[1]