好吧,我有兩個小區位0111010和0101011.我想異或兩個一起,使生成的細胞會0010001.EXCEL異或多個位
我知道你可以使用這個布爾值的字符串
=OR(AND(A1,NOT(A2)),AND(A2,NOT(A1)))
但它不適用於一串位。
好吧,我有兩個小區位0111010和0101011.我想異或兩個一起,使生成的細胞會0010001.EXCEL異或多個位
我知道你可以使用這個布爾值的字符串
=OR(AND(A1,NOT(A2)),AND(A2,NOT(A1)))
但它不適用於一串位。
您需要使用VBA來做到這一點。如果您打開VBA,創建一個新的模塊並輸入功能
Public Function BITXOR(x As Long, y As Long)
BITXOR = x Xor y
End Function
然後可以使用DEC2BIN和BIN2DEC從二進制轉換爲十進制運行此功能。例如:
單元格A1 = 0111010
單元格A2 = 0101011
=DEC2BIN(BITXOR(BIN2DEC(A1),BIN2DEC(A2)))
= 1-(A1 <> 0)+(A2 <> 0)。
可以使用其分割成與上面式各個列這樣: = MID(A1 | 7 | 1) = MID(A1 | 6 | 1) = MID(A1 | 5 | 1) = MID(A1 | 4 | 1) = MID(A1 | 3 | 1) = MID(A1 | 2 | 1) = MID(A1 | 1 | 1) ...
你可以用VBA做到這一點:
Public Function XOR_binary(b1, b2) As String
Dim len_b1
Dim len_b2
Dim len_diff
Dim i
Dim bit1
Dim bit2
' see if the two string are the same length. If not, add 0's to
' the beginning of the shorter string
len_b1 = Len(b1)
len_b2 = Len(b2)
len_diff = len_b1 - len_b2
Select Case len_diff
Case Is < 0
' b2 is longer
b1 = String(Abs(len_diff), "0") & b1
Case Is = 0
' they're the same length
Case Is > 0
' b1 is longer
b2 = String(len_diff, "0") & b2
End Select
XOR_binary = ""
For i = Len(b2) To 1 Step -1
bit1 = CInt(Mid(b1, i, 1))
bit2 = CInt(Mid(b2, i, 1))
XOR_binary = CInt(bit1 Xor bit2) & XOR_binary
Next i
End Function
可能不是最好的實現,但它的工作原理。
使用你的榜樣,A3
包含:
=XOR_Binary(A1,A2)
結果字符串將具有相同的位數,你在傳遞中最長的字符串的
'這個VBA返回必須是一個雙。在工作表上格式化。
Option Explicit
Public Function MYXOR(r1 As Range, r2 As Range) As Double
'r1 and r2 are expected as HEX; for example,
'DEC2HEX(CODE("B")) returns ASCII of "B" as HEX
On Error GoTo ErrHandler
MYXOR = "&H" & r1.Value Xor "&H" & r2.Value
GoTo CleanUp
ErrHandler:
MYXOR = Err.Number
Resume CleanUp
CleanUp:
' format the double being returned in MYXOR with TEXT(DEC2HEX(MYXOR(C9,F9)),"00000")
' number of leading zeroes according to the size of the HEX in r1 and r2
End Function
這裏是一個溶液而不使用VBA:
=TEXT(SUMPRODUCT(MOD(INT(MID(A1,{1,2,3,4,5,6,7},1))+INT(MID(A2,{1,2,3,4,5,6,7},1)),2),{1000000,100000,10000,1000,100,10,1}),"0000000")
該計算使用SUMPRODUCT
和TEXT
把它變成比特串按位XOR
。
注:這個公式既需要輸入值具有長度爲7(按照自己的例子)和輸出也將具有長度7.爲了允許不同的輸入的長度,簡單地實施必要的截斷和/或填充。
您可以選擇使用一些速記定義:
BitPositions
爲={1,2,3,4,5,6,7}
(7位),BitStrings
爲={1000000,100000,10000,1000,100,10,1}
(7位),BitFormat
作爲="0000000"
(7位),那麼你的公式可以變得更清晰一點/短/清潔器:
=TEXT(SUMPRODUCT(MOD(INT(MID(A1,BitPositions,1))+INT(MID(A2,BitPositions,1)),2),BitStrings),BitFormat)
這也使得更容易與較大的比特串,例如工作:
BitPositions
如=ROW(INDIRECT("1:32"))
( 32位),BitStrings
如=10^(32-ROW(INDIRECT("1:32")))
(32位),BitFormat
如=REPT("0",32)
(32位)如果您希望執行NOT
/OR
/AND
/etc。那麼你可以從這些formulas for the decimal counterparts得到你的靈感;這裏是some more in-depth explanations爲XOR
與SUMPRODUCT
雖然它也使用小數輸入。
雖然你如何做每個位部分?對於每一位, – 2009-02-09 18:22:59
= 1-(A1≠0)+(A2≠0)。 我不得不修改這一點讓它現在工作 = INT(ISODD((A1≠0)+(A2≠0))) 它的作品爲1或0的單元格,所以你必須分開你正在工作的任何東西,比如= mid(A1,8,1)......就像他說的 – daniel 2014-06-08 00:10:17