0

In the code below, the output to the console is 521.

There are exactly 32 digits in that input.

Can someone please explain or provide links that explain how the number variable has been converted to 521.

int number = 00000000000000000000000000001011;
System.out.println(number);
Stephen
  • 38
  • 1
  • 4
  • 4
    Numbers with a leading `0` are considered octal (i.e. base-8) in Java. Your usage of just 0 and 1 suggests you want to write a binary number, then you should prefix it with `0b` (i.e. `0b1011`). – Joachim Sauer Jan 09 '22 at 23:19
  • 1
    Does this answer your question? [Why are integer literals with leading zeroes interpreted strangely?](https://stackoverflow.com/questions/565634/why-are-integer-literals-with-leading-zeroes-interpreted-strangely) – Waleed Jan 10 '22 at 00:21
  • It is octal number, not binary: `521 = 512 + 8 + 1 = 1 * 8^3 + 0 * 8^2 + 1 * 8^1 + 1 * 8^0`. – Nowhere Man Jan 10 '22 at 02:05

0 Answers0