I have an array storing a series of numbers as follows:
static final int CodeArray[] ={
11,
011,
0011,
1011,
00011,
10011,
01011,
000011,
100011,
010011,
...
}
However when I access the values(using a for loop), it returns the following:
11
9
9
1011
9
10011
521
9
100011
4105
Why is there a difference in the values being printed from the ones stored?
Sotirios Delimanolis
281k62 gold badges718 silver badges744 bronze badges
2 Answers 2
Because putting a 0
before a number turns it into an octal representation, not binary.
So, for example, 011
is octal for decimal 9
, which is what's printed.
See this SO question to see how to work with binary numbers in Java.
answered Feb 1, 2014 at 23:32
Comments
011 is treated as octal not decimal
answered Feb 1, 2014 at 23:41
Comments
lang-java
0b
if you want binary (in Java 7).