This question already has answers here:
Please explain why 17 % 40 = 17
(5 answers)
Closed 8 years ago.
What is 3 % 5?
Plus how do you figure this out?
I really have no idea how to calculate this. I have done some research but everything is blocked.
It is called Modulo operator: which gives the remainder during a division.
example
46%9=1 as 46-(5*9)=1
38%6=2 as 38-(6*6)=2
3%5= 3. As 3-(5*0)=3
It's called the Modulo operator, it essentially calculates the remainder.
http://en.wikipedia.org/wiki/Modulo_operation
The modulo operator is the remainder operation.
To test you could try:
public class Test {
public static void main(String[]args) {
System.out.println(3%5);
}
}
Related
This question already has answers here:
What are bitwise shift (bit-shift) operators and how do they work?
(10 answers)
Closed 2 years ago.
I want to get answer 4, but my code prints 2. I don't know why?
public class Test {
public static void main(String[] args) {
System.out.println(9 >> 2);
}
}
Yes it will give you 2
because
9 binary representation is 1001, and when you applied the right shift by 2 it will give you 0010
thats why
9 >> 2 = 2
9=0x1001
does that make the bit shift clear?
This question already has answers here:
Java: What does ~ mean
(5 answers)
Closed 5 years ago.
int x=10;
System.out.println(~x);
//this will print -11
//how to do the calculation manually using complement arithmetic
This is a Negation operator it will consider ~x= -(10+1), so you will get -11 as the output. Refer some C books you can get more explanation on this
Perhaps this helps you: You can print out the bits of the integer as following. There you can see that a int is represented as a 32-bit value. Explanations about bitwise not operator can be found on the web i guess;
int x = 10;
System.out.println(Integer.toBinaryString(x)); //00000000000000000000000000001010
System.out.println(Integer.toBinaryString(~x)); //11111111111111111111111111110101
System.out.println(~x); //-11
This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
Could anyone explain to me why
System.out.println(100*(1-10^(-10/10)));
results in the number "800" being printed out? The correct answer is 90 when you use a calculator. How would I go about doing this calculation in Java?
Thanks!
The ^ operator does not do what you think it does. It is bitwise-xor
You need to look into the Math.pow() method.
This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
I am trying to calculate the power as below but it is giving me 'bad operands type for binary operator '^'. I am guessing that it is a precedence issue but it still doesn't fix with inserting additional brackets
double pw = ((N - (df + 1))^2);
You should use java.lang.Math.pow(x,y)
Example: java.lang.Math.pow(2,3) returns 8
See this
http://www.tutorialspoint.com/java/lang/math_pow.htm
java.lang.Math.pow(double a, double b)
You can use static import for this.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the ^ operator do in Java?
The power ^ in Java?
I am sorry if this is a duplicate, but i didnĀ“t found anything in SO.
So can someone explaint me why
System.out.println((2^0));
this does return 2?
i was expecting a 1.
Because the ^ operator does not mean "raise 2 to the 0th power". It's a bitwise exclusive OR operator.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
In order to do that, your code should look like this:
double one = Math.pow(2.0, 0.0); // Silly, but you can do it.
Don't be surprised if the answer comes out to something that's not exactly 1.0. You'll need to know about how floating point numbers work.
The ^ sign means XOR and not pow. Try Math.Pow(2.0, 0.0) instead.
^ in Java is Bitwise exclusive-OR.
so 2(1 0) ^(XOR) 0(0 0) =1 0 ie 2 !!!
Got it?