bitwise-xor

What is C# exclusive or `^` usage? [closed]

那年仲夏 提交于 2019-12-05 06:24:54
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . Can anyone explain this operator with a good example? I know what this operator is. I mean a real-life example. It is an implementation of the the logical operation exclusive disjunction http://en.wikipedia.org/wiki/Exclusive_or

Xor encryption in PHP

六眼飞鱼酱① 提交于 2019-12-05 03:45:15
I'm new to Xor encryption, and I'm having some trouble with the following code: function xor_this($string) { // Let's define our key here $key = ('magic_key'); // Our plaintext/ciphertext $text =$string; // Our output text $outText = ''; // Iterate through each character for($i=0;$i<strlen($text);) { for($j=0;$j<strlen($key);$j++,$i++) { $outText .= $text{$i} ^ $key{$j}; //echo 'i='.$i.', '.'j='.$j.', '.$outText{$i}.'<br />'; //for debugging } } return $outText; } When I run this it works for normal strings, like 'dog' but it only partially works for strings containing numbers, like '12345'.

Find a number with even number of occurrences

会有一股神秘感。 提交于 2019-12-03 02:08:28
Given an array where number of occurrences of each number is odd except one number whose number of occurrences is even. Find the number with even occurrences. e.g. 1, 1, 2, 3, 1, 2, 5, 3, 3 Output should be: 2 The below are the constraints: Numbers are not in range. Do it in-place. Required time complexity is O(N). Array may contain negative numbers. Array is not sorted. With the above constraints, all my thoughts failed: comparison based sorting, counting sort, BST's, hashing, brute-force. I am curious to know: Will XORing work here? If yes, how? This problem has been occupying my subway

Can XOR of two integers go out of bounds?

这一生的挚爱 提交于 2019-11-30 04:10:44
I had been studying the algorithm for finding lonely integers in an array, and here is the implementation: int arr[] = {10, 20, 30, 5, 20, 10, 30}; int LonelyInteger = 0; for(int i=0; i< 7; i++) { LonelyInteger = LonelyInteger ^ arr[i]; } The result is 5 . My question is - supposedly the integers (getting generated by the XOR operation) are too large due to this operation: LonelyInteger ^ arr[i] Which leads to a potentially large integer which cannot be represented by the datatype say int in this case. My questions are: Is it even possible that XOR will generate such a large integer value that

Given XOR & SUM of two numbers. How to find the numbers?

我们两清 提交于 2019-11-29 12:05:13
Given XOR & SUM of two numbers. How to find the numbers? For example, x = a+b, y = a^b; if x,y are given, how to get a, b? And if can't, give the reason. This cannot be done reliably. A single counter-example is enough to destroy any theory and, in your case, that example is 0, 100 and 4, 96 . Both of these sum to 100 and xor to 100 as well: 0 = 0000 0000 4 = 0000 0100 100 = 0110 0100 96 = 0110 0000 ---- ---- ---- ---- xor 0110 0100 = 100 xor 0110 0100 = 100 Hence given a sum of 100 and an xor of 100 , you cannot know which of the possibilities generated that situation. For what it's worth,

Given XOR & SUM of two numbers. How to find the numbers?

牧云@^-^@ 提交于 2019-11-28 05:52:35
问题 Given XOR & SUM of two numbers. How to find the numbers? For example, x = a+b, y = a^b; if x,y are given, how to get a, b? And if can't, give the reason. 回答1: This cannot be done reliably. A single counter-example is enough to destroy any theory and, in your case, that example is 0, 100 and 4, 96 . Both of these sum to 100 and xor to 100 as well: 0 = 0000 0000 4 = 0000 0100 100 = 0110 0100 96 = 0110 0000 ---- ---- ---- ---- xor 0110 0100 = 100 xor 0110 0100 = 100 Hence given a sum of 100 and

How to find all the subarrays with xor 0?

十年热恋 提交于 2019-11-27 22:38:31
The problem is to find all the subarrays of the given array with xor of all its elements equal to zero. For example, if array contains elements [13,8,5,3,3] , the solution should give the indices of all subarrays like 0-2 , 3-4 , 0-4 , etc. The question is similar to the one asked here The only difference is that I want the indices of all the subarrays that satisfies the equation A0 xor A1 xor...xor An = 0 This is a fairly straightforward extension of the linked question. In Python, # Multivalued map from the XOR of array[:i] to i for all i. prefix_xor_to_stops = {0: [0]} prefix_xor = 0 for j,

Computing the Parity

对着背影说爱祢 提交于 2019-11-27 01:29:41
问题 I don't fully understand this algorithm of calculating the parity bit. Can someone please explain in detail? The following code is taken from the 'Hacker's Delight' book: int parity(unsigned x) { unsigned y; y = x ^ (x >> 1); y = y ^ (y >> 2); y = y ^ (y >> 4); y = y ^ (y >> 8); y = y ^ (y >>16); return y & 1; } 回答1: First a bit of theory. The parity of a set of bits is even if the number of 1-bits is even, is odd if the number of 1-bits is odd. We encode the parity information as: 1 -->