I understand this:
35%10 returns 5
but, why does 000000035%10 return 9?
Ruby does the same thing. I checked with irb.
Should I strip the 0s that are padding the number? What is the java function to do that?
When you put the leading zeros followed by octal digits (from 0 to 7), you're creating an octal literal. And since 000000035 in octal is 29 in decimal, its remainder with 10 is 9.
UPDATE
If you have your "octal literal" in a string, simply use Integer.parseInt and you'll get it parsed as a decimal.
assert Integer.parseInt("035") == 35;
Leading zero in number literal means octal number.
000000035 = 035 = 3*8 + 5 = 29
29 % 10 = 9
See this page for other available formats.
Related
This question already has answers here:
Closed 11 years ago.
They think it is:
Possible Duplicate:
Integer with leading zeroes
But if you check Integer with leading zeroes then you will find that the question is asked if before the launch of jdk7 and therefore it has lower researching efforts. But in jdk7 there is some change and addition to the integers. Here are the answers which are up to date covering jdk7.
I've a code:
class Test{
public static void main(String[] args){
int x=09;
System.out.println(x);
}
}
On compilation it gives an error: integer number too large : 09
Why it do so?
Again, if I change the code to:
class Test{
public static void main(String[] args){
int x=012;
System.out.println(x);
}
}
Now the output is 10
Why it give the output 10 instead of 12?
Numbers beginning with 0 are considered octal – and 9 is not an octal digit (but (conventionally) 0-7 are).
Hexadecimal literals begin with 0x, e.g. 0xA.
Up until Java 6, there was no literal notation for binary and
you'll had to use something like
int a = Integer.parseInt("1011011", 2);
where the second argument specifies the desired base.
Java 7 now has binary literals.
In Java SE 7, the integral types (byte, short, int, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number.
Integer literals beginning with a "0" are treated as octal. The permissible digits are 0 through 7.
Integers beginning with the digit 0 are octal (base 8) numbers. The largest octal digit is 7; after 07 comes 010 (which is equal to decimal 8!)
012 (octal twelve) is 010 (octal ten, which is decimal 8) plus 2, or decimal 10.
09 is an octal numeric literal, an invalid one though.
Hexadecimal numbers start with 0x, like 0xFFFF.
There used to be no binary literal in Java. Java 7 supports them, starting with 0b, like 0b00100001.
Numbers beginning with 0 are octal number.
http://en.wikipedia.org/wiki/Octal
This question already has answers here:
Why are integer literals with leading zeroes interpreted strangely?
(8 answers)
Closed 6 years ago.
See the following example:
int a = 0111;
System.out.println(a);
Output: 73
Why is this happening and how can i get the exact value without conversion?
According to the Java Language Specification 3.10.1. Integer Literals, there are 4 ways to write an integer literal:
DecimalNumeral: Digit 1-9 followed by zero or more digits 0-9, or the number 0.
HexNumeral: Leading 0x followed by one or more hexadecimal digits (0-9, a-f). Not case-sensitive.
OctalNumeral: Digit 0 followed by one or more digits 0-7.
BinaryNumeral: Leading 0b followed by one or more digits 0 or 1. Not case-sensitive.
(All 4 allow underscores for clarity)
As you can see, the number 111 depends on the prefix:
0111 (OctalNumeral) is 1*8*8 + 1*8 + 1 = 64+8+1 = 73.
111 (DecimalNumeral) is 1*10*10 + 1*10 + 1 = 100+10+1 = 111.
0b111 (BinaryNumeral) is 1*2*2 + 1*2 + 1 = 4+2+1 = 7.
0x111 (HexNumeral) is 1*16*16 + 1*16 + 1 = 256+16+1 = 273.
Which one is right depends on what you wanted.
Well you're getting the precise value. The problem is the format.
0111
gets interpreted as octal value in java. So your value actually is 73 as a quick calculation would show.
A octal value in java is defined this way:
OctalNumeral:
0 OctalDigits
0 Underscores OctalDigits
jls 3.10.1
So 0_111 would be interpreted in the same way.
The format to use in java code would be:
0b111
Which actually gets interpreted as 7 - in binary format - , as expected.
I'll add the links later on, I'm in a bit of a hurry.
class test{
public static void main(String args[]){
int a = 011;
System.out.println(a);
}
}
Why I am getting 9 as output instead of 011?
How can I get 011 as output?
The JLS 3.10.1 describes 4 ways to define integers.
An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).
An octal numeral consists of a digit 0 followed by one or more of the digits 0 through 7 ...
A decimal numeral is either the single digit 0, representing the integer zero, or consists of an digit from 1 to 9 optionally followed by one or more digits from 0 to 9 ...
In summary if your integer literal (i.e. 011) starts with a 0, then java will assume it's an octal notation.
Solutions:
If you want your integer to hold the value 11, then don't be fancy, just assign 11. After all, the notation doesn't change anything to the value. I mean, from a mathematical point of view 11 = 011 = 11,0.
int a = 11;
The formatting only matters when you print it (or when you convert your int to a String).
String with3digits = String.format("%03d", a);
System.out.println(with3digits);
The formatter "%03d" is used to add leading zeroes.
Alternatively, you could do it in 1 line, using the printf method.
System.out.printf("%03d", a);
A numeric literal that starts with 0 is parsed as an octal number (i.e. radix 8). 011 is 9 in octal.
The 011 is being interpreted as an octal number. This means it's in base 8. See also this SO post. From the accepted answer by Stuart Cook:
In Java and several other languages, an integer literal beginning with 0 is interpreted as an octal (base 8) quantity.
Digits in Base 10:
100s, 10s, 1s
Digits in Base 8:
64s, 8s, 1s
So the 011 is interpreted as 8+1=9
My simple question is why:
System.out.println(010|4);
prints "12"? I understand bitwise OR operator but why "010" equals 8? It's definitely not compliment 2's notification, so how to decode this number?
A leading 0 denotes an octal numeric value so the value 010 can be decoded thus: 010 = 1 * 81 + 0 * 80 = 8
Have a look at the Java Language Specification, Chapter 3.10.1 Integer Literals
An integer literal may be expressed in decimal (base 10), hexadecimal
(base 16), octal (base 8), or binary (base 2).
[...]
An octal numeral consists of an ASCII digit 0 followed by one or more
of the ASCII digits 0 through 7 interspersed with underscores, and can
represent a positive, zero, or negative integer.
Now you should understand why 010 is 8.
That is because java takes it as an octal literal and hence produces 12. Try System.out.println(10|4) and the result is 14. Because this time it is taken as decimal literal.
As everybody mentioned here that 010 is an Octal Integer literal . The leading 0 specifies that it is an octal representation . Actual value will be :
1*8^1 + 0*8^0 = 8(decimal) = 1000(binary-only last 4 digits)
Now coming back to the SOP :
System.out.println(010|4);
Applying Bitwise OR on 010 and 4(considering only the last 4 digits) =>
1000|0100
= 1100
= 1*2^3 + 1*2^2 + 0*2^1 + 0*2^0
= 8 + 4 + 0 + 0
= 12(decimal)
Any number in Java which fulfill the below Conditions -
A. number Should have three or More Digital
B.Number should Start with 0.
If above Condition are true then number treated as Octal_Base (8) Number.
Therefore,
010=(8^2)*0+(8^1)*1+(8^0)*0=64*0+8*1+1*0=8
So,
010=8
One point you should consider that the number will be in octal if "0XX" i.e. both X are in range [0,7], otherwise it will result in "Integer number too large".
This question already has answers here:
Closed 11 years ago.
They think it is:
Possible Duplicate:
Integer with leading zeroes
But if you check Integer with leading zeroes then you will find that the question is asked if before the launch of jdk7 and therefore it has lower researching efforts. But in jdk7 there is some change and addition to the integers. Here are the answers which are up to date covering jdk7.
I've a code:
class Test{
public static void main(String[] args){
int x=09;
System.out.println(x);
}
}
On compilation it gives an error: integer number too large : 09
Why it do so?
Again, if I change the code to:
class Test{
public static void main(String[] args){
int x=012;
System.out.println(x);
}
}
Now the output is 10
Why it give the output 10 instead of 12?
Numbers beginning with 0 are considered octal – and 9 is not an octal digit (but (conventionally) 0-7 are).
Hexadecimal literals begin with 0x, e.g. 0xA.
Up until Java 6, there was no literal notation for binary and
you'll had to use something like
int a = Integer.parseInt("1011011", 2);
where the second argument specifies the desired base.
Java 7 now has binary literals.
In Java SE 7, the integral types (byte, short, int, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix 0b or 0B to the number.
Integer literals beginning with a "0" are treated as octal. The permissible digits are 0 through 7.
Integers beginning with the digit 0 are octal (base 8) numbers. The largest octal digit is 7; after 07 comes 010 (which is equal to decimal 8!)
012 (octal twelve) is 010 (octal ten, which is decimal 8) plus 2, or decimal 10.
09 is an octal numeric literal, an invalid one though.
Hexadecimal numbers start with 0x, like 0xFFFF.
There used to be no binary literal in Java. Java 7 supports them, starting with 0b, like 0b00100001.
Numbers beginning with 0 are octal number.
http://en.wikipedia.org/wiki/Octal