xor

Simple Python Challenge: Fastest Bitwise XOR on Data Buffers

大城市里の小女人 提交于 2019-11-26 05:53:48
问题 Challenge: Perform a bitwise XOR on two equal sized buffers. The buffers will be required to be the python str type since this is traditionally the type for data buffers in python. Return the resultant value as a str . Do this as fast as possible. The inputs are two 1 megabyte (2**20 byte) strings. The challenge is to substantially beat my inefficient algorithm using python or existing third party python modules (relaxed rules: or create your own module.) Marginal increases are useless. from

Creating a “logical exclusive or” operator in Java

本小妞迷上赌 提交于 2019-11-26 03:48:10
问题 Observations: Java has a logical AND operator. Java has a logical OR operator. Java has a logical NOT operator. Problem: Java has no logical XOR operator, according to sun. I would like to define one. Method Definition: As a method it is simply defined as follows: public static boolean logicalXOR(boolean x, boolean y) { return ( ( x || y ) && ! ( x && y ) ); } Method Call: This method is called in the following way: boolean myVal = logicalXOR(x, y); Operator Usage: I would much rather have an

Why is XOR the default way to combine hashes?

主宰稳场 提交于 2019-11-26 03:29:40
问题 Say you have two hashes H(A) and H(B) and you want to combine them. I\'ve read that a good way to combine two hashes is to XOR them, e.g. XOR( H(A), H(B) ) . The best explanation I\'ve found is touched briefly here on these hash function guidelines: XORing two numbers with roughly random distribution results in another number still with roughly random distribution*, but which now depends on the two values. ... * At each bit of the two numbers to combine, a 0 is output if the two bits are

How does XOR variable swapping work?

穿精又带淫゛_ 提交于 2019-11-26 00:51:39
问题 Can someone explain to me how XOR swapping of two variables with no temp variable works? void xorSwap (int *x, int *y) { if (x != y) { *x ^= *y; *y ^= *x; *x ^= *y; } } I understand WHAT it does, but can someone walk me through the logic of how it works? 回答1: You can see how it works by doing the substitution: x1 = x0 xor y0 y2 = x1 xor y0 x2 = x1 xor y2 Substituting, x1 = x0 xor y0 y2 = (x0 xor y0) xor y0 x2 = (x0 xor y0) xor ((x0 xor y0) xor y0) Because xor is fully associative and

How does XOR variable swapping work?

本小妞迷上赌 提交于 2019-11-25 20:40:25
Can someone explain to me how XOR swapping of two variables with no temp variable works? void xorSwap (int *x, int *y) { if (x != y) { *x ^= *y; *y ^= *x; *x ^= *y; } } I understand WHAT it does, but can someone walk me through the logic of how it works? You can see how it works by doing the substitution: x1 = x0 xor y0 y2 = x1 xor y0 x2 = x1 xor y2 Substituting, x1 = x0 xor y0 y2 = (x0 xor y0) xor y0 x2 = (x0 xor y0) xor ((x0 xor y0) xor y0) Because xor is fully associative and commutative: y2 = x0 xor (y0 xor y0) x2 = (x0 xor x0) xor (y0 xor y0) xor y0 Since x xor x == 0 for any x, y2 = x0