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
281k62 gold badges718 silver badges744 bronze badges
asked Feb 1, 2014 at 23:32
2
  • The ones stored are the same as the ones printed. Prefix your number with 0b if you want binary (in Java 7). Commented Feb 1, 2014 at 23:33
  • Take a look at the comment by assylias and this wiki article en.wikipedia.org/wiki/Octal Commented Feb 1, 2014 at 23:54

2 Answers 2

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.

answered Feb 1, 2014 at 23:32

Comments

0

011 is treated as octal not decimal

answered Feb 1, 2014 at 23:41

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.