Operator Precedence - java

I tried to compute this as i=i*++i therefore i=56*57 which gives me 3192 but my program says the value is 2162:
class Demo {
public static void main(String args[]) {
short i=056;
i*=++i;
System.out.println(i);
}
}

The problem is before the multiplication - it's here:
short i=056;
That's an octal literal, with decimal value 46. So you're actually getting the results of 46 * 47, which is indeed 2162.
I would strongly advise you not to use code like i *= ++i though. It's simpler for everyone concerned to use i *= i + 1. I'd also advise you not to use octal literals.

Related

Negate int using XOR and addition

I have to write a method which converts an int value to the same negative int value. For example, if the user types in 5 the method should give back -5.
The whole story is:
The method has a transfer parameter which is either a positive or a negative int. Is it a positive int, we should change it to a negative int. If it is a negative int, do nothing.
It is also hard to check if the given int is positive or negative.
The regulations:
I'm only allowed to use the arithmetic operation + and all the boolean operations. But in this case I know that I have to use XOR.
It's not about writing the method and all the JAVA stuff. It's just the logic I struggle with.
How can I change a value to its negative value using only XOR and +?
ATTENTION:
The only operations allowed are:
!
^
&&
||
+
No *, no -, no ~
Edit:
The two solutions from CherryDT and MikeC are working well. Any ideas how to check if its a negative or a positive parameter with the same regulations? I thought this would be the easy part. Then realized it isn't.
(x ^ -1) + 1
Explanation: Basically, it's the same as ~x + 1. -1 has all bits set, and therefore inverts all bits when XORed with, just like NOT. And since, the other way round, inverting will always give you -x - 1, all you need to do is invert and add 1.
Here we go
public class Test {
public static void main(String []args){
int x = 245;
System.out.println(x);
x = (~x^x)*x;
System.out.println(x);
}
}

How can I always print 15 non-zero digits in Java?

Say for example that I have the Double value 1.23456789123456. I want to be able to multiply this number by various powers of 10 and display the output as
123.456789123456
12345.6789123456
0.0123456789123456
0.000123456789123456
etc
Is there any way to do this in Java without having to use if...then statements to handle the changing decimal precision required for different powers of 10?
This could be improved, but it's close:
public class Test {
public static void main(final String[] args) {
System.out.println(format(123.456789123456d));
System.out.println(format(12345.6789123456d));
System.out.println(format(0.0123456789123456d));
System.out.println(format(0.000123456789123456d));
}
public static String format(final double d) {
final int before = 16 - Integer.toString((int) d).length();
final String format = "%" + (16 + before) + "." + before + "f";
return String.format(format, d);
}
Output:
123.4567891234560
12345.67891234560
0.012345678912346
0.000123456789123
If you don't need the number as an actual floating point value, try representing the number as a String without a decimal point (e.g., "123456789123456"). Then using the String.substring() method, you can print the decimal point wherever you want, include leading zeroes, etc. I don't know that you can totally avoid using any if statements, but the logic should be fairly straightforward.
For instance, this prints the decimal after three significant digits:
String n = "123456789123456";
System.out.print(n.substring(0, 3));
System.out.print('.');
System.out.print(n.substring(3));
Check out the DecimalFormat class

What is the modulo operator for longs in Java?

How do I find the modulo (%) of two long values in Java? My code says 'Integer number too large' followed by the number I'm trying to mod. I tried casting it to a long but it didn't work. Do I have to convert it to a BigInteger and use the remainder method? Thanks.
The % operator does work for longs. It sounds like you may have forgotten to stick L at the end of a numeric literal, as in 123456789L. Can we see your code?
You can only have an integer up to 2 147 483 647. If you want to go bigger than that, say 3 billion, you must specify it to be a long
class Descartes {
public static void main(String[] args) {
long orig = Long.MAX_VALUE;
long mod = orig % 3000000000; // ERROR 3000000000 too big
long mod = orig % 3000000000L; // no error, specified as a long with the L
}
}
Keep in mind that you can use capital OR lowercase L, but it's advisable to use capital, since the lowercase looks remarkably similar to the number 1.
You can also try working with the BigInteger class which has a remainder() method that works similarly to %.

Convert very small double values to string (with scientific notation) (Java)

I'm trying to print a small double number like 6.67e-11, but using Double.toString() returns 0. What can I do to make it print 6.67e-11 (or something similar) instead?
Unable to reproduce:
public class Test {
public static void main(String args[])
{
double d = 6.67e-11;
System.out.println(Double.toString(d)); // Prints "6.67E-11"
}
}
IIRC, Double.toString() always returns a string which allows the exact value to be round-tripped using Double.parseDouble().
My guess is that you don't actually have a small value - that you have 0, due to some rounding errors in other operations.

Java int division confusing me

I am doing very simple int division and I am getting odd results.
This code prints 2 as expected:
public static void main(String[] args) {
int i = 200;
int hundNum = i / 100;
System.out.println(hundNum);
}
This code prints 1 as not expected:
public static void main(String[] args) {
int i = 0200;
int hundNum = i / 100;
System.out.println(hundNum);
}
What is going on here?
(Windows XP Pro, Java 1.6 running in Eclipse 3.4.1)
The value 0200 is an octal (base 8) constant. It is equal to 128 (decimal).
From Section 3.10.1 of the Java Language Specification:
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 and can represent a positive, zero, or negative integer.
The value 0200 is an octal, which is 128 in decimal.
For further information, see the literals section of the Primitive Data Types explanation.
Observed an interesting behavior here.
If I do an Integer.parseInt("0200"), I get 200 as o/p.
Howzzat ?!

Categories