xor

Why are XOR often used in java hashCode() but another bitwise operators are used rarely?

﹥>﹥吖頭↗ 提交于 2019-11-26 15:39:27
问题 I often see code like int hashCode(){ return a^b; } Why XOR? 回答1: Of all bit-operations XOR has the best bit shuffling properties. This truth-table explains why: A B AND 0 0 0 0 1 0 1 0 0 1 1 1 A B OR 0 0 0 0 1 1 1 0 1 1 1 1 A B XOR 0 0 0 0 1 1 1 0 1 1 1 0 As you can see for AND and OR do a poor job at mixing bits. OR will on average produce 3/4 one-bits. AND on the other hand will produce on average 3/4 null-bits. Only XOR has an even one-bit vs. null-bit distribution. That makes it so

T-SQL XOR Operator

ぃ、小莉子 提交于 2019-11-26 15:30:52
问题 Is there an XOR operator or equivalent function in SQL Server (T-SQL)? 回答1: There is a bitwise XOR operator - the caret (^), i.e. for: SELECT 170 ^ 75 The result is 225. For logical XOR, use the ANY keyword and NOT ALL, i.e. WHERE 5 > ANY (SELECT foo) AND NOT (5 > ALL (SELECT foo)) 回答2: Using boolean algebra, it is easy to show that: A xor B = (not A and B) or (A and not B) A B | f = notA and B | g = A and notB | f or g | A xor B ----+----------------+----------------+--------+-------- 0 0 |

What's wrong with XOR encryption?

瘦欲@ 提交于 2019-11-26 15:16:38
问题 I wrote a short C++ program to do XOR encryption on a file, which I may use for some personal files (if it gets cracked it's no big deal - I'm just protecting against casual viewers). Basically, I take an ASCII password and repeatedly XOR the password with the data in the file. Now I'm curious, though: if someone wanted to crack this, how would they go about it? Would it take a long time? Does it depend on the length of the password (i.e., what's the big-O)? 回答1: The problem with XOR

What does bitwise XOR (exclusive OR) mean?

假装没事ソ 提交于 2019-11-26 15:13:23
问题 I'm trying to understand the binary operators in C# or in general, in particular ^ - exclusive or. For example: Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time and constant space. This can be done with ^ as follows: Do bitwise XOR of all the elements. Finally we get the number which has odd occurrences. How does it work? When I do: int res = 2 ^ 3; res = 1; int res = 2 ^ 5; res = 7;

Why does the xor operator on two bytes produce an int?

喜夏-厌秋 提交于 2019-11-26 14:40:30
问题 //key & hash are both byte[] int leftPos = 0, rightPos = 31; while(leftPos < 16) { //possible loss of precision. required: byte, found: int key[leftPos] = hash[leftPos] ^ hash[rightPos]; leftPos++; rightPos--; } Why would a bitwise operation on two bytes in Java return an int? I know I could just cast it back to byte, but it seems silly. 回答1: Because the language spec says so. It gives no reason, but I suspect that these are the most likely intentions: To have a small and simple set of rules

Creating a “logical exclusive or” operator in Java

巧了我就是萌 提交于 2019-11-26 14:05:38
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 operator, used as follows: boolean myVal = x ^^ y; Question: I can't find anything on how to go about

Simple Python Challenge: Fastest Bitwise XOR on Data Buffers

*爱你&永不变心* 提交于 2019-11-26 12:54:55
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 os import urandom from numpy import frombuffer,bitwise_xor,byte def slow_xor(aa,bb): a=frombuffer(aa

Why is there no logical xor in JavaScript?

天涯浪子 提交于 2019-11-26 11:51:49
问题 Why is there no logical xor in JavaScript? 回答1: JavaScript traces its ancestry back to C, and C does not have a logical XOR operator. Mainly because it's not useful. Bitwise XOR is extremely useful, but in all my years of programming I have never needed a logical XOR. If you have two boolean variables you can mimic XOR with: if (a != b) With two arbitrary variables you could use ! to coerce them to boolean values and then use the same trick: if (!a != !b) That's pretty obscure though and

Why is this statement not working in java x ^= y ^= x ^= y;

拥有回忆 提交于 2019-11-26 11:17:51
问题 int x=1; int y=2; x ^= y ^= x ^= y; I am expecting the values to be swapped.But it gives x=0 and y=1. when i tried in C language it gives the correct result. 回答1: Your statement is roughly equivalent to this expanded form: x = x ^ (y = y ^ (x = x ^ y)); Unlike in C, in Java the left operand of a binary operator is guaranteed to be evaluated before the right operand. Evaluation occurs as follows: x = x ^ (y = y ^ (x = x ^ y)) x = 1 ^ (y = 2 ^ (x = 1 ^ 2)) x = 1 ^ (y = 2 ^ (x = 3)) x = 1 ^ (y =

Why is XOR the default way to combine hashes?

感情迁移 提交于 2019-11-26 11:04:01
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 equal, else a 1. In other words, in 50% of the combinations, a 1 will be output. So if the two input bits