xor

XOR-ing strings in C#

橙三吉。 提交于 2019-12-03 13:03:57
I recently started playing around with C#, and I'm trying to understand why the following code doesn't compile. On the line with the error comment, I get: Cannot implicitly convert type 'int' to 'char'. An explicit conversion exits (are you missing a cast?) I'm trying to do a simple XOR operation with two strings. public string calcXor (string a, string b) { char[] charAArray = a.ToCharArray(); char[] charBArray = b.ToCharArray(); char[] result = new char[6]; int len = 0; // Set length to be the length of the shorter string if (a.Length > b.Length) len = b.Length - 1; else len = a.Length - 1;

How do you implement XOR using +-*/?

亡梦爱人 提交于 2019-12-03 11:08:55
How can the XOR operation (on two 32 bit ints) be implemented using only basic arithmetic operations? Do you have to do it bitwise after dividing by each power of 2 in turn, or is there a shortcut? I don't care about execution speed so much as about the simplest, shortest code. Edit: This is not homework, but a riddle posed on a hacker.org . The point is to implement XOR on a stack-based virtual machine with very limited operations (similar to the brainfuck language and yes - no shift or mod). Using that VM is the difficult part, though of course made easier by an algorithm that is short and

django template if or statement

无人久伴 提交于 2019-12-03 09:31:02
Basically to make this quick and simple, I'm looking to run an XOR conditional in django template. Before you ask why don't I just do it in the code, this isn't an option. Basically I need to check if a user is in one of two many-to-many objects. req.accepted.all and req.declined.all Now they can only be in one or the other (hence the XOR conditional). From the looking around on the docs the only thing I can figure out is the following {% if user.username in req.accepted.all or req.declined.all %} The problem I'm having here is that if user.username does indeed appear in req.accepted.all then

Fastest way to check if two integers are on the same side of 0

此生再无相见时 提交于 2019-12-03 06:46:11
问题 I need to check if two integers are on the same side of zero many times. I don't care if it's positive or negative, just that it's the same side... and performance is very important. Currently I'm doing this: if (int1 == 0 || int2 == 0) { // handle zero } else if ((int1 ^ int2) > 0) { // different side } else { // same side } This is a 30% improvement in speed (tested with caliper) over the more obvious: if ((int1 > 0 && int2 > 0) || (int1 < 0 && int2 < 0)) { Can it be done faster? If anyone

XOR using mathematical operators

走远了吗. 提交于 2019-12-03 05:02:34
How can I implement XOR using basic mathematical operators like +,-,*,/ Update: Actually, I need to track change in two matrix having Boolean values. This can be done using XORing each value with corresponding value in other matrix. But, Lp_Solve library doesn't support XOR operation. Also, it accepts only linear equations. (a − b)² This works because: (a − b)² = a * (a − b) + b * (b − a) Since multiplication in ℤ₂ is conjuction ( & ), and 1 - a is negation ( ! ), the above formula is equivalent to XOR for a, b ∈ {0, 1} : (a & !b) | (b & !a) See the comment below by Pascal Cuoq explaining why

Finding the number missing in the sequence [duplicate]

馋奶兔 提交于 2019-12-03 00:22:46
This question already has an answer here: Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missing 44 answers I have been given a list of n integers and these integers are in the range of 1 to n. There are no duplicates in list.But one of the integers is missing in the list.I have to find the missing integer. Example: If n=8 I/P [7,2,6,5,3,1,8] O/P 4 I am using a simple concept to find the missing number which is to get the sum of numbers total = n*(n+1)/2 And then Subtract all the numbers from sum. But the above method will fail if the

What is XOR Encryption?

喜欢而已 提交于 2019-12-02 20:49:43
I have heard about people starting encryption and thought it may be something I would like, so I checked XOR and can't make any sense of it. So can someone explain to me what XOR is ? XOR is a logical operation, pronounced exclusive or . It can be used to cipher messages simply and fast. You can see a truth table for this operation here: http://mathworld.wolfram.com/XOR.html quasi-pseudo code implementation (via http://www.evanfosmark.com/2008/06/xor-encryption-with-python/) : #!/usr/bin/env python from itertools import izip, cycle def xor_crypt_string(data, key): return ''.join(chr(ord(x) ^

Fastest way to check if two integers are on the same side of 0

筅森魡賤 提交于 2019-12-02 20:25:27
I need to check if two integers are on the same side of zero many times. I don't care if it's positive or negative, just that it's the same side... and performance is very important. Currently I'm doing this: if (int1 == 0 || int2 == 0) { // handle zero } else if ((int1 ^ int2) > 0) { // different side } else { // same side } This is a 30% improvement in speed (tested with caliper ) over the more obvious: if ((int1 > 0 && int2 > 0) || (int1 < 0 && int2 < 0)) { Can it be done faster? If anyone wants to see the test framework I'm using for the 30%, it's here. I used caliper 0.5-rc1 NOTE: All of

Bitwise xor 0xFFFFFFFF?

こ雲淡風輕ζ 提交于 2019-12-02 17:19:42
问题 I couldn't wrap my head around this: def expr(a): return ~(a ^ 0xFFFFFFFF), a ^ 0xFFFFFFFF, ~a, a print(expr(0xFFFFFFFF)) print(expr(1)) print(expr(0)) print(expr(-1)) I understand ~a means two's complement of a , but a ^ 0xFFFFFFFF also flips all the bits, but python will interpret it as a large number. I know Python3 is using unbound integer size, how does that work? Can someone ELI5 (Explain Like I'm Five)? Results: ( -1, 0, -4294967296, 4294967295) (-4294967295, 4294967294, -2, 1) (

Determine numbers based on their sum and xor

99封情书 提交于 2019-12-02 13:06:39
If we know the sum and XOR of two numbers, then can we find out what are the two numbers? I am trying to resolve a problem and the above problem is a part of that. Although, I do have another solution for that problem but I still want a solution for this. No. Example: SUM=7, XOR=7 Possible answers: a) 1, 6 b) 2, 5 c) 3, 4 Not enough information to decide which pair (a, b or c) is the original pair of numbers. 来源: https://stackoverflow.com/questions/25565626/determine-numbers-based-on-their-sum-and-xor