Jump to content

XOR for single bytes


 Share

Recommended Posts

ok, the XOR in autoit is for 32bit integers only. is there any way to get an xor for single bytes?

Just mask it:
$bin1 = Binary("0xFEDCBA98"); 32-bit binary
ConsoleWrite("$bin1 = " & $bin1 & @LF)
$bin2 = Binary("0xCC"); 8-bit alternating ones pattern
ConsoleWrite("$bin2 = " & $bin2 & @LF)
$bin3 = BitAND(BitXOR($bin1, $bin2), 0xFF); mask to lower 8-bits
ConsoleWrite("$bin3 = " & $bin3 & @LF); 0b10011000 XOR 0b10101010 = 0b00110010 (50 Decimal)

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Just mask it:

$bin1 = Binary("0xFEDCBA98"); 32-bit binary
ConsoleWrite("$bin1 = " & $bin1 & @LF)
$bin2 = Binary("0xCC"); 8-bit alternating ones pattern
ConsoleWrite("$bin2 = " & $bin2 & @LF)
$bin3 = BitAND(BitXOR($bin1, $bin2), 0xFF); mask to lower 8-bits
ConsoleWrite("$bin3 = " & $bin3 & @LF); 0b10011000 XOR 0b10101010 = 0b00110010 (50 Decimal)

;)

so this a xor for 0xCC and 0xFF ?
Link to comment
Share on other sites

so this a xor for 0xCC and 0xFF ?

No. It does an XOR of $bin1 and $bin2 (all 32-bits), then masks off the lower 8-bits because that's all you wanted from it.

;)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

No. It does an XOR of $bin1 and $bin2 (all 32-bits), then masks off the lower 8-bits because that's all you wanted from it.

;)

what do you think of this?

func ByteXOR($byte1, $byte2)
    $byte1 = "0x000000" & Hex($byte1,2)
    $byte2 = "0x000000" & Hex($byte2,2)
    Return "0x" & Hex(BitXOR($byte1, $byte2), 2)
EndFunc
Edited by pixartist
Link to comment
Share on other sites

Just mask it:
$bin1 = Binary("0xFEDCBA98"); 32-bit binary
ConsoleWrite("$bin1 = " & $bin1 & @LF)
$bin2 = Binary("0xCC"); 8-bit alternating ones pattern
ConsoleWrite("$bin2 = " & $bin2 & @LF)
$bin3 = BitAND(BitXOR($bin1, $bin2), 0xFF); mask to lower 8-bits
ConsoleWrite("$bin3 = " & $bin3 & @LF); 0b10011000 XOR 0b10101010 = 0b00110010 (50 Decimal)

:)

PsaltyDS, it appears bitwise operations do not work correctly using binary, Binary( ).

$bin1 = '0xFEDCBA98' ; 32-bit binary
$bin2 = '0x000000CC' ; 8-bit alternating ones pattern
$bin3 = BitAND(BitXOR($bin1, $bin2), 0xFF); mask to lower 8-bits

ConsoleWrite("$bin1 = " & $bin1 & @LF)
ConsoleWrite("$bin2 = " & $bin2 & @LF)
ConsoleWrite("$bin3 = " & $bin3 & @LF); 0b10011000 XOR 0b10101010 = 0b00110010 (50 Decimal)

; BitXOR(0xFEDCBA98, 0x000000CC)
; 11111110110111001011101010011000   0xFEDCBA98
; XOR
; 00000000000000000000000011001100   0x000000CC
; =
; 11111110110111001011101001010100   0xFEDCBA54

; BitAND(BitXOR(0xFEDCBA98, 0x000000CC), 0xFF)
; 11111110110111001011101001010100   0xFEDCBA54
; AND
; 00000000000000000000000011111111   0x000000FF
; =
; 00000000000000000000000001010100   0x00000054 

ConsoleWrite("dec('54') = " & dec('54')& @LF)oÝ÷ Ú«¨µéÚ
Link to comment
Share on other sites

PsaltyDS, it appears bitwise operations do not work correctly using binary, Binary( ).

$bin1 = '0xFEDCBA98' ; 32-bit binary
$bin2 = '0x000000CC' ; 8-bit alternating ones pattern
$bin3 = BitAND(BitXOR($bin1, $bin2), 0xFF); mask to lower 8-bits

ConsoleWrite("$bin1 = " & $bin1 & @LF)
ConsoleWrite("$bin2 = " & $bin2 & @LF)
ConsoleWrite("$bin3 = " & $bin3 & @LF); 0b10011000 XOR 0b10101010 = 0b00110010 (50 Decimal)

; BitXOR(0xFEDCBA98, 0x000000CC)
; 11111110110111001011101010011000   0xFEDCBA98
; XOR
; 00000000000000000000000011001100   0x000000CC
; =
; 11111110110111001011101001010100   0xFEDCBA54

; BitAND(BitXOR(0xFEDCBA98, 0x000000CC), 0xFF)
; 11111110110111001011101001010100   0xFEDCBA54
; AND
; 00000000000000000000000011111111   0x000000FF
; =
; 00000000000000000000000001010100   0x00000054 

ConsoleWrite("dec('54') = " & dec('54')& @LF)ƒoÝŠ÷ Ú«¨µéÚ™

pixartist, your function appears to give the correct answer. It works.

I learnt a lot about bitwise operations from here:-

http://www.autoitscript.com/forum/index.ph...st&p=466668

yea, the problem with my function is, that it's extremly slow :)

any idea how to do this faster?

Link to comment
Share on other sites

yea, the problem with my function is, that it's extremly slow :)

any idea how to do this faster?

A one liner. It must make for faster execution.

$byte1 = 0xcc 
$byte2 = 0xFF
$byte3 = "0x" & Hex(BitXOR("0x000000" & Hex($byte1,2),  "0x000000" & Hex($byte2,2)), 2)  

MsgBox(0,"","$byte3 = " & $byte3)

; Bitxor(0xFF,0xCC)
; 00000000000000000000000011111111   0x000000FF
; XOR
; 00000000000000000000000011001100   0x000000CC
; =
; 00000000000000000000000000110011   0x00000033
Link to comment
Share on other sites

Binary data types, and the Bitwise functions, seem to interact on a 1st byte basis: the first byte of the binary is usually what's used to perform the operation. What you want to do is convert the 2 Binary values you want to numbers (Look around the forums, Larry/Resnullius just did work on Binary to Integer). In the case of BitAND, BitOR, BitNOT, and BitXOR, endianess doesn't really matter, as long as both sides are the same endianess.

PsaltyDS is right about masking, just remember to convert your binary to a integer number first, and then mask/xor away. Malkey's last example is close, but the stuff in the middle is a little off:

$byte1 = 0xCC
$byte2 = 0xFF
$byte3 = BitXOR($byte1, $byte2)

MsgBox(0, "", "$byte3 = " & $byte3 & @CRLF & "in hex: " & Hex($byte3, 2))

Remember: just because a hex string has 0x in front of it doesn't automatically mean it's a Binary, Hexadecimal representations for plain old numbers has been in AutoIt for a long time. Also, if you use all 32-Bits in a calculation, you may get a negative number, because all the BitFunctions return Signed 32-bit numbers. Just something to keep in mind while your going around.

Link to comment
Share on other sites

PsaltyDS, it appears bitwise operations do not work correctly using binary, Binary( ).

That's because I'm stupid sometimes... :idiot:

Note in my comments where it says "8-bit alternating ones pattern" next to 0xCC instead of 0xAA. Doh! >_<

By coincidence, the bad pattern (0b11001100 vice 0b10101010) gave me exactly the answer I thought I should get from the correct pattern "0b10011000 XOR 0b10101010 = 0b00110010 (50 Decimal)", so I didn't catch my mistake when I tested it.

I'm learning, slowly.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...