xor

Python equivalent of sum() using xor()

一笑奈何 提交于 2019-11-30 03:28:13
问题 I like the Python sum function : >>> z = [1] * 11 >>> zsum = sum(z) >>> zsum == 11 True I want the same functionality with using xor (^) not add (+). I want to use map. But I can not work out how to do this. Any hints? I am not satisfied with this : def xor(l): r = 0 for v in l: r ^= v return v I want a 1 liner using map. Hints? 回答1: zxor = reduce(lambda a, b: a ^ b, z, 0) import operator zxor = reduce(operator.xor, z, 0) 回答2: Note that starting Python 3.8 , and the introduction of assignment

How can I bitwise XOR two C char arrays?

冷暖自知 提交于 2019-11-30 02:45:42
问题 I feel silly for not being able to figure this out, but I am lost. I am trying to XOR two C strings. #include <stdio.h> #include <memory.h> #include <stdlib.h> int main() { char plainone[16]; char plaintwo[16]; char xor[17]; strcpy(plainone, "PlainOne"); strcpy(plaintwo, "PlainTwo"); int i=0; for(i=0; i<strlen(plainone);i++) xor[i] ^= (char)(plainone[i] ^ plaintwo[i]); printf("PlainText One: %s\nPlainText Two: %s\n\none^two: %s\n", plainone, plaintwo, xor); return 0; } My output is: $ ./a.out

Two elements in array whose xor is maximum

断了今生、忘了曾经 提交于 2019-11-29 18:48:45
Given an array of integers ,You have to find two elements whose XOR is maximum. There is naive approach --just by picking each element and xoring with other elements and then comparing the results to find the pair. Other than this ,Is there any efficient algorithm? templatetypedef I think I have an O(n lg U) algorithm for this, where U is the largest number. The idea is similar to user949300's, but with a bit more detail. The intuition is as follows. When you're XORing two numbers together, to get the maximum value, you want to have a 1 at the highest possible position, and then of the

Why swap doesn't use Xor operation in C++

☆樱花仙子☆ 提交于 2019-11-29 18:01:04
问题 I've learned that Xor operation can be used to implement effective swap function. like this: template<class T> void swap(T& a, T& b) { a = a^b; b = a^b; a = a^b; } But the implementation of swap all i can found on the internet is essentially like this: template<class T> void swap(T& a, T& b) { T temp(a); a = b; b = temp; } It seems that the compiler didn't generate the same code for the two form above because I tested it on VC++ 2010 and the first one is done the job more quickly than std:

Simple XOR encryption routine in C/C++

穿精又带淫゛_ 提交于 2019-11-29 17:40:58
I'm trying to encrypt/decrypt a file using XOR. I have the following encryption/decryption routine where every byte is xor'd and the result is being subtracted by the value of the byte that is located at the previous location. The ASM representation is as follows crypt: mov dl, [eax+ecx] ; read byte xor dl, 0C5h ; xor it with oxC5 sub dl, [eax+ecx-1] ; sub the previous byte mov [eax+ecx], dl ; save the new byte dec eax ; decrement pointer test eax, eax jg short crypt ; That is what my encryption routine should look like, I'm trying to port this this C/C++. My code is as follows #include <stdio

Cannot implicity convert type 'int' to 'string'

我只是一个虾纸丫 提交于 2019-11-29 16:26:49
Random randgen = new Random(); int dkey; public object SetScore(int val) { dkey=randgen.Next(int.MaxValue/2); return val ^ dkey; //^ means XOR. } public string GetScore(int val) { return val ^ dkey; GC.Collect(); } From public string GetScore(int val) { return val ^ dkey; GC.Collect(); } the return val ^ dkey shows the error Cannot implicitly convert type 'int' to 'string' Your GetScore method returns a string, but val ^ dkey is an integer. Either convert the result to string with: return (val ^ dkey).ToString(); or return an int: public int GetScore(int val) ... Oh, and please remove the call

XOR register,register (assembler)

那年仲夏 提交于 2019-11-29 13:43:45
From time to time we have to analyze pieces of assembler code (IA32), and more than often i come across an instruction that looks like this: xor ax, ax or with other registers aswell: xor dx, dx , xor al, al , ... What exactly does this do ? (ax xor ax always gives 0 ?) It's a common assembler idiom to set a register to 0. xor ax, ax corresponds to ax = ax ^ ax which, as you already notices, is effectively ax = 0 . If I recall correctly the main advantage is that its code-size is smaller than mov ax, 0 That is exactly what it does -- zero the contents of a register xor %ax, %ax, as stated in

how to xor binary with python

余生长醉 提交于 2019-11-29 13:14:15
I'm trying to xor 2 binaries using python like this but my output is not in binary any help? a = "11011111101100110110011001011101000" b = "11001011101100111000011100001100001" y = int(a) ^ int(b) print y a = "11011111101100110110011001011101000" b = "11001011101100111000011100001100001" y = int(a,2) ^ int(b,2) print '{0:b}'.format(y) BigH To get the Xor'd binary to the same length, as per the OP's request, do the following: a = "11011111101100110110011001011101000" b = "11001011101100111000011100001100001" y = int(a, 2)^int(b,2) print bin(y)[2:].zfill(len(a)) [output:

Functional variant of 'oneof' function in Racket

让人想犯罪 __ 提交于 2019-11-29 12:11:40
I have written following function to find if one and only one of 5 variables is true: (define (oneof v w x y z) (or (and v (not w) (not x) (not y) (not z)) (and w (not v) (not x) (not y) (not z)) (and x (not v) (not w) (not y) (not z)) (and y (not v) (not w) (not x) (not z)) (and z (not v) (not w) (not x) (not y)) )) (xor takes only 2 arguments) However, it is very imperative and not functional. Moreover, I want to write a function (oneof N) which will be generic rather than specific for 5 variables. How can this be done? Thanks. Edit: As pointed out in the comments, the code is 'repetitive'

What is the point of the logical operators in C?

对着背影说爱祢 提交于 2019-11-29 07:35:31
I was just wondering if there is an XOR logical operator in C (something like && for AND but for XOR). I know I can split an XOR into ANDs, NOTs and ORs but a simple XOR would be much better. Then it occurred to me that if I use the normal XOR bitwise operator between two conditions, it might just work. And for my tests it did. Consider: int i = 3; int j = 7; int k = 8; Just for the sake of this rather stupid example, if I need k to be either greater than i or greater than j but not both, XOR would be quite handy. if ((k > i) XOR (k > j)) printf("Valid"); else printf("Invalid"); or printf("%s"