xor

How to make bit wise XOR in C

夙愿已清 提交于 2019-11-27 10:24:00
问题 I'm trying to get into C programming, and I'm having trouble writing a bitwise XOR function with only ~ and & operators. Example: bitXor(4, 5) = 1 . How can I achieve this? So far I have this: int bitXor(int x, int y) { return z; } 回答1: Well, let's think about this. What does XOR do? x y XOR ------------ 0 0 0 1 0 1 0 1 1 1 1 0 So how do we turn that into a function? Let's think about AND, and the inverse order of AND (~x&~y) (this happens to be NOR): (~x&~y) x y AND NOR ---------------------

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

主宰稳场 提交于 2019-11-27 09:23:59
//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. 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 to cover arithmetic operations involving all possible combinations of types To allow an efficient

Is it good practice to use the xor operator for boolean checks? [closed]

核能气质少年 提交于 2019-11-27 09:18:20
问题 I personally like the exclusive or , ^ , operator when it makes sense in the context of boolean checks because of its conciseness. I much prefer to write if (boolean1 ^ boolean2) { //do it } than if((boolean1 && !boolean2) || (boolean2 && !boolean1)) { //do it } but I often get confused looks from other experienced Java developers (not just the newbies), and sometimes comments about how it should only be used for bitwise operations. I'm curious as to the best practices regarding the usage of

Why is there no logical xor in JavaScript?

限于喜欢 提交于 2019-11-27 06:08:00
Why is there no logical xor in JavaScript? 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 would certainly deserve a comment. Indeed, you could even use the bitwise XOR operator at this point, though

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

人盡茶涼 提交于 2019-11-27 04:39:04
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. 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 = 2 ^ 3) // x is set to 3 x = 1 ^ (y = 1) x = 1 ^ 1 // y is set to 1 x = 0 // x is set to 0 You could reverse

Pass two integers as one integer

爱⌒轻易说出口 提交于 2019-11-27 03:55:21
问题 I have two integers that I need to pass through one integer and then get the values of two integers back. I am thinking of using Logic Operators (AND, OR, XOR, etc) . 回答1: Using the C programming language, it could be done as follows assuming that the two integers are less than 65535. void take2IntegersAsOne(int x) { // int1 is stored in the bottom half of x, so take just that part. int int1 = x & 0xFFFF; // int2 is stored in the top half of x, so slide that part of the number // into the

XOR operation with two strings in java

て烟熏妆下的殇ゞ 提交于 2019-11-27 03:19:34
How to do bitwise XOR operation to two strings in java. You want something like this: import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.IOException; public class StringXORer { public String encode(String s, String key) { return base64Encode(xorWithKey(s.getBytes(), key.getBytes())); } public String decode(String s, String key) { return new String(xorWithKey(base64Decode(s), key.getBytes())); } private byte[] xorWithKey(byte[] a, byte[] key) { byte[] out = new byte[a.length]; for (int i = 0; i < a.length; i++) { out[i] = (byte) (a[i] ^ key[i%key.length]); } return out

xor operation between java references

≯℡__Kan透↙ 提交于 2019-11-27 02:21:46
问题 I want to write java code for xor-linked list . Can somebody suggest me how to perform xor operation between references? 回答1: To say long things short, you can't . With just a little more words, if Java allows you to pass variables by references, arithmetic on these reference is not permitted by the Java language. Hence, your xor operations won't be possible. Moreover, when reading the Wikipedia entry, I understand it's a memory optimization of classical linked list implementation relying,

Exclusive Or in Regular Expression

◇◆丶佛笑我妖孽 提交于 2019-11-26 20:21:29
问题 Looking for a bit of regex help. I'd like to design an expression that matches a string with " foo " OR " bar ", but not both " foo " AND " bar " If I do something like... /((foo)|(bar))/ It'll match " foobar ". Not what I'm looking for. So, how can I make regex match only when one term or the other is present? Thanks! 回答1: You can do this with a single regex but I suggest for the sake of readability you do something like... (/foo/ and not /bar/) || (/bar/ and not /foo/) 回答2: This is what I

How to decrypt simple XOR encryption

非 Y 不嫁゛ 提交于 2019-11-26 20:02:34
问题 I found the following XOR encryption function on the internet: void xor_encrypt(char *key, char *string) { int i, string_length = strlen(string); for(i=0; i<string_length; i++) { string[i]=string[i]^key[i]; printf("%i", string[i]); } } It works perfect, but I would like to decrypt the string also. For example: void xor_decrypt(char *key, char *encrypted_string) { //decrypt method goes here } So basically after I encrypt the string, I would use the same encryption key to decrypt the previously