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

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

Related

Simple Modulo expression [duplicate]

This question already has answers here:
Modulus division when first number is smaller than second number
(6 answers)
Closed 7 years ago.
I understand that a mod operator finds the remainder of two numbers. However, I am having trouble understanding the concept when the numbers are reversed. Meaning, a smaller number comes first in the operation.
int x = 4 % 3 ; // prints out 1
However, can someone explain this to me:
int y = 1 % 4 ; // prints out 1
int z = 2 % 3 ; // prints out 2
Thanks in advance!
Whether the left-hand side of the operator is larger than the right is irrelevant. There is always a remainder for any division operation, it's just that sometimes it's 0.
So 5 % 2 returns 1, just like 4 % 3 returns 1.
The value of any modulo operation of the form x % n will be 0 to n - 1 inclusive, for positive x. It will be -1(n-1) to 0 inclusive for negative x.
Are you sure about the int y that prints out 2?
The int z though seems normal :
2= 0*3 + 2
int y = 1 % 4 should print 1 because:
1=0*4 + 1
It works the same as when a bigger number comes first, you just take the remainder of the division of the first one by the second one.

Implementation logic behind adding two numbers without using +?

I found this code online. But, I am unable to get the logic behind the following code:
public static int add(int a, int b) {
if (b == 0) return a;
int sum = a ^ b; // add without carrying
System.out.println("sum is : "+sum);
int carry = (a & b) << 1; // carry, but don’t add
return add(sum, carry); // recurse
}
Let's look at an example (using 8 bits for simplicity)
a = 10010110
b = 00111101
a^b is the xor, which gives 1 for places where there is a 1 in one number and 0 in the other. In our example:
a^b = 10101011
Since 0 + 0 = 0, 0 + 1 = 1 and 1 + 0 = 1, the only columns left to deal with are the ones that have a 1 in both of the numbers. In our example, a^b is short by whatever the answer to
00010100
+ 00010100
is. In binary, 1 + 1 = 10, so the answer to the above sum is
00101000
or (a & b) << 1. Therefore the sum of a^b and (a & b) << 1 is the same as a + b.
So, assuming the process is guaranteed to terminate, the answer will be correct. But the process will terminate because each time we call sum recursively the second parameter has at least one more 0 at the end, due to the bit shift <<. Therefore, we are guaranteed to eventually end up with the second argument consisting entirely of 0s, so that the line if (b == 0) return a; can end the process and give us an answer.
Consider, as an example, 5+7:
5 = 101 (Base 2)
7 = 111 (Base 2)
Now consider adding the two (base 2) digits:
0+0 = 0 = 0 carry 0
1+0 = 1 = 1 carry 0
0+1 = 1 = 1 carry 0
1+1 = 10 = 0 carry 1
The sum (without carrying) of A+B is A^B and the carry is A&B; and when you carry a number it is shifted one digit to the left (hence (A&B)<<1).
So:
5 = 101 (Base 2)
7 = 111 (Base 2)
5^7 = 010 (sum without carrying)
5&7 = 101 (the carry shifted left)
Then we can recurse to add the carry:
A = 010
B = 1010
A^B = 1000 (sum without carrying)
A&B = 0010 (the carry shifted left)
Then we can recurse again as we still have more to carry:
A' = 1000
B' = 100 (without the leading zeros)
A'^B' = 1100 (sum without carrying)
A'&B' = 0000 (the carry shifted left)
Now there is nothing to carry - so we can stop and the answer is 1100 (base 2) = 12 (base 10).
The algorithm is just implementing decimal addition as (longhand) binary addition using the ors to add and the bitshifted ands to find the carry and will recurse until there is nothing more to carry (which will always occur as the bitshift appends another zero to the carry each time so with each recursion at least one more bit will not generate a carry value each time).
We are adding converting the integers to bits and using bitwise operators .
EXOR i.e ^ : 0 ^0 and 1 ^1 =0 , other cases give 1.
AND i.e & 1^1 =1 , ..other cases give 0.
<< or left shift . i.e shift left and append a 0 bit : 0010 becomes 0100
eg.
add(2,3)
2= 0010
3=0011
exor both : to get initial sum : 0001
carry : a &b = 0010
Left shift by 1 bit : 0100 i.e 4
add(1,4)
exor both : 0001 0100 and u get 0101 i.e 5
carry = 0000 <<1 i.e 0000 ..
since carry is 0 , it stops addition and returns previous sum
This is the table for addition:
+ 0 1
-- --
0 | 0 1
1 | 1 10
▲
If you ignore the carry bit ▲ you'll see that it's the same as the XOR table:
^ 0 1
-- --
0 | 0 1
1 | 1 0
So if you combine two numbers with bitwise XOR you get bit-by-bit addition without carry.
Now, what is the carry? It's a bit that's only there when both inputs are 1.
You can get that with AND:
& 0 1
-- --
0 | 0 0
1 | 0 1
But it needs to be added to the sum after being shifted one position to the left, because it's "carried" over, hence the (a & b) << 1
So you can compute the addition without carry and the carry itself. How do you add them together without using addition? Simple! By recursing on this very definition of addition!
See #pbabcdefp's answer on why the recursion always terminates.

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

Modulus division when first number is smaller than second number

I apologize if this is a simple question but I'm having trouble grasping the concept of modulus division when the first number is smaller than the second number. For example when 1 % 4 my book says the remainder is 1. I don't understand how 1 is the remainder of 1 % 4. 1 / 4 is 0.25. Am I thinking about modulus division incorrectly?
First, in Java, % is the remainder (not modulo) operator, which has slightly different semantics.
That said, you need to think in terms of integer-only division, as if there were no fractional values. Think of it as storing items that cannot be divided: you can store zero items of size 4 in a storage of overall capacity one. Your remaining capacity after storing the maximum number of items is one. Similarly, 13%5 is 3, as you can fit 2 complete items of size 5 in a storage of size 13, and the remaining capacity is 13 - 2*5 = 3.
If you divide 1 by 4, you get 0 with a remainder of 1. That's all the modulus is, the remainder after division.
I am going to add a more practical example to what "Jean-Bernard Pellerin" already said.
It is correct that if you divide 1 by 4 you get 0 but, Why when you do 1 % 4 you have 1 as result?
Basically it is because this:
n = a / b (integer), and
m = a % b = a - ( b * n )
So,
a b n = a/b b * n m = a%b
1 4 0 0 1
2 4 0 0 2
3 4 0 0 3
4 4 1 0 0
5 4 1 4 1
Conclusion: While a < b, the result of a % b will be "a"
Another way to think of it as a representation of your number in multiples of another number. I.e, a = n*b + r, where b>r>=0. In this sense your case gives 1 = 0*4 + 1. (edit: talking about positive numbers only)
I think you are confused between %(Remainder) and /(Division) operators.
When you say %, you need to keep dividing the dividend until you get the remainder 0 or possible end. And what you get in the end is called Remainder.
When you say /, you divide the dividend until the divisor becomes 1. And the end product you get is called Quotient
Another nice method to clear things up,
In modulus, if the first number is > the second number, subtract the second number from the first until the first number is less than the second.
17 % 5 = ?
17 - 5 = 12
12 % 5 = ?
12 - 5 = 7
7 % 5 = ?
7 - 5 = 2
2 % 5 = 2
Therefore 17 % 5, 12 % 5, 7 % 5 all give the answer of 2.
This is because 2 / 5 = 0 (when working with integers) with 2 as a remainder.

What is inverse function to XOR?

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.

Categories