What is inverse function to XOR? - java

There is XOR function in Java - a^b
For exemple: 5^3 = 6
Can you tell me inverse function? If I have 6 and 3 can i get range of numbers which include number 5?

The inverse is XOR!
If you have:
c = a^b;
You can get a or b back if you have the other value available:
a = c^b; // or b^c (order is not important)
b = c^a; // or a^c
For example if a = 5, b = 3 (and thus c = 6 as you mentioned) you get:
b=0011 (3) a=0101 (5)
c=0110 (6) XOR or c=0110 (6) XOR
---------- ----------
a=0101 (5) b=0011 (3)

The inverse of XOR is XOR itself.
I came across a similar problem on leetcode. Leetcode-1720

The inverse of XOR is XOR itself. For example if you take this operation :
3 ^ 4 = 7
The following statements are true :
4 ^ 7 = 3
3 ^ 7 = 4
Hopefully, this helps.

Inverse of a XOR is XOR itself.
For better reference and understanding u can refer to LeetCode 2433.

Related

BigInteger exponentiation with BigInteger number: ArithmeticException, would overflow supported range

I'm building the DSA algorithm. But I had a problem when ranking BigInteger numbers with other BigInteger numbers. This is the formula I want to use:
v = ((g^u1 * y^u2) mod p) mod q
This is the code I made:
BigInteger v = g.pow(u1.intValue()).multiply(y.pow(u2.intValue())).mod(p).mod(q);
When running the script, the error is:
Exception in thread "main" java.lang.ArithmeticException: BigInteger would overflow supported range
at java.math.BigInteger.reportOverflow(Unknown Source)
at java.math.BigInteger.pow(Unknown Source)
at DSAVerifying.main(DSAVerifying.java:38)
To expand on my comment and because I could not find a duplicate: use modPow!
The problem here is that g^u1 (and y^u2) is REALLY large. But very often when dealing with powers in maths you have a mod statement following it and that simplifies stuff a lot: generally a ^ b mod c can be expressed as ((((a * a) mod c) * a) mod c) * a) mod c ..... (b times). And that is basically what modPow does, it applies the mod during the exponation. This will return the same number but will not overflow. They are mathematically identical, but one can be calculated by a computer with reasonable effort while the other cannot. It is up to you as the developer to be smart and simplify or rephrase the expression you want to solve in a way that a computer can properly handle.
BigInteger v = g.modPow(u1, p).multiply(y.modPow(u2, p)).mod(p).mod(q);
Basically to compute (6 ^ 10 mod 7) you do not ever want to first calculate 6 ^ 10 and then apply the mod 7 but instead do 6 * 6 mod 7 = 36 mod 7 = 1 => 1 * 6 mod 7 = 6 => 6 * 6 mod 7 = 36 mod 7 = 1 => ... and you can see that the only values you deal with are 1 and 6 instead of 60466176 (which is 6^10).

Difference between 2^0*2 and (2^0)*2?

I have expected both expression's will give same answer:
System.out.println(2^0*2);
System.out.println((2^0)*2);
Output:
2
4
Is there a specific reason why 2^0*2 = 2 and (2^0)*2 = 4?
You have wrongly assumed that ^ operator behaves the same like the exponentiation in math.
At the first sight you can see that ^ is understood as + operator. Actually it means bitwise XOR operator.
System.out.println(2^0*2); // 2 XOR 0 * 2 = 2
System.out.println((2^0)*2); // (2 XOR 0) * 2 = 4
System.out.println(2^4); // 2 XOR 4 = 6
The XOR is exclusive disjunction that outputs true only when inputs differ. Here is the whole trick:
2^0 = 2 XOR 0 = (0010) XOR (0000) = (0010) = 2
2^4 = 2 XOR 4 = (0010) XOR (0100) = (0110) = 6
check this link
http://bmanolov.free.fr/javaoperators.php
2^0*2=2
has higher priority thatn ^ so first you will evaluate 0*2 which is 0 and then xor it with 2 which will resutl 2
(2^0)*2
() has higher priority so you will first evaluate 2^0 then which is 2 then multiply it with 2
Operator Precedence
In your first example you calculate: 0*2 = 2 ^ 0 = 2
In your second example you calculate: 2 ^ 0 = 2 * 2 = 4

What does step ^= 1 mean in Java?

I know that ^ is the xor operator in Java. But I couldn't understand it in the following context.
int step = 0;
...
step ^=1;
Source: Google Code Jam 2014 (Participant's answer)
File Link : here
it goes under assignment operator category like
+= -= *= /= %= &= ^= |= <<= >>= >>>=
means
^= bitwise exclusive OR and assignment operator
step ^=1; as same as step = step ^ 1;
^ stands for XOR operator.
a ^= b is equivalent to a = a ^ b
step ^=1 means step = step xor 1. Similar to step += 1 which gets evaluated to step = step + 1
So ^= is short hand xor operator.
So xor table says:
operand1 operand2 output
0 0 0
0 1 1
1 0 1
1 1 0
so if my step is 1, then 1 xor 1 would be 0.
From Java Tutorials,
^
Assume integer variable A holds 60 and variable B holds 13 then:
Binary XOR Operator copies the bit if it is set in one operand but not both.(A ^ B) will give 49 which is 0011 0001
In your case it is,
step = step^1
and in result you get step=1
http://www.tutorialspoint.com/java/java_basic_operators.htm
As others have pointed out, step ^=1 flips the least significant bit of step. This makes even numbers get 1 bigger, and odd numbers get 1 smaller.
Examples:
0 --> 1
1 --> 0
7 --> 6
6 --> 7
-3 --> -4

What is the fastest method for specific bit operations between two bytes?

I have two java byte variables, lets say
a = 00010011
b = 01101101 (in binary form)
Suppose that I have a third byte
c = 11001000
where its bits will work as an indicator to select between two operations (XOR/XNOR).
e.g. if c[i] = 1 then I select to XOR a[i]^b[i] and if c[i] = 0 I select to XNOR these values.
In this example the resulted byte will be
d = 01001001
What is the fastest method in Java to achieve such a result?
How about
d = a ^ b ^ ~c;
or
d = ~(a ^ b ^ c);
or
d = ~a ^ b ^ c;
The ^ has the property of flipping bits set to 1 and leaving bits set to 0. If you use ~ to flip that value you get flip for 0 and unchanged for 1.
Don't know whether it is the fastest, which I assume is a silly question, as it's a bitwise operation only, but this will work:
(a XOR b) XNOR c
which is same as:
~(a ^ b ^ c)

When are hash functions orthogonal to each other?

When are hash functions orthogonal to each other?
And can you provide an example in Java of two hash functions that are orthogonal to each other?
From (a Google search result paper)
(Orthogonal Hash Functions) Two hash functions h1 and h2 are orthogonal,
if for all states s, s' ∈ S with h1 (s) = h1 (s') and h2 (s) = h2 (s') we have
s = s'.
S. Edelkamp, Perfect Hashing for State Space Exploration on the GPU.
In English, if any two given values passed to two different orthogonal hash functions result in the same outputs, those inputs must have been the same value.
Example:
Let h and g be hash functions.
Let b be a currently unknown value.
h(0) = h(b) = 5
g(0) = g(b) = 4
if h and g are orthogonal, b MUST equal 0.
Thus for any values given to h that result in a unique result,
If those same values are given to g, they must also result in a unique result,
IF they are orthogonal hash functions.
Pseudocode:
// Assume no wraparound will ever occur due to overflow.
HashFunc h = x -> x + 1;
HashFunc g = y -> y + 2;
h(0) = 1 // No other input value results in --> 1
g(0) = 2 // No other input value results in --> 2
// These must have been orthogonal hash functions.
// Now for some non-orthogonal hash functions:
// Let the domain be integers only.
HashFunc j = x -> ceil(abs(x / 2));
HashFunc k = x -> ceil(sqrt(x));
j(0) = 0 // Unique result
k(0) = 0 // Unique result
j(1) = j(2) = 1
k(1) = 1 != k(2) = 2
// k(1) results in a unique value, but it isn't unique for j.
// These cannot be orthogonal hash functions.
from http://www.aaai.org/ocs/index.php/ICAPS/ICAPS10/paper/download/1439/1529
Obtained with Google: define "orthogonal hash" (second hit).
Translating:
If you have a "perfect hashing function", then h(s) == h(s') iff s == s'.
If you have "any two hashing functions" for which there are values s, s' that have both
h1(s) == h1(s') and h2(s) == h2(s')
then these functions are called orthogonal if the above is true for s == s'
It's actually quite a tricky concept. If h1 and h2 were both perfect hashing functions, then they would automatically have to be orthogonal according to the above definition (if I understand correctly). But you can have imperfect functions that fit the above definition.
Example: in the state space [0, 9], two functions
h1(int x) return x % 5;
h2(int x) return x % 7;
Would be orthogonal:
x h1 h2
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
5 0 5
6 1 6
7 2 0
8 3 1
9 4 2
In the above, h1(s) = h1(s') for pairs of values s that are either 0 apart or 5 apart.
For h1, the distance is either 0 or 7.
The only pairs for which both conditions are true are those where the distance is 0 - so only when s1 == s2. Thus these are orthogonal (although imperfect) hashing functions.
And that, I think, answers both parts of your question.

Categories