0

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
  • 274,122
  • 60
  • 696
  • 724

2 Answers2

4

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.

Community
  • 1
  • 1
AntonH
  • 6,359
  • 2
  • 30
  • 40
0

011 is treated as octal not decimal

Girish
  • 1,717
  • 1
  • 18
  • 30