Its good you understand these things very well/quickly, unlike me. I do
know what he meant about representations. My question is: without
converting to any other formats (for purposes of visual
representation), can Java take a String, assume the String is binary as
given, and work on it as such?
Is there a prefix for binary numbers that I can append ahead of the
given String so that it now is known as a binary number, much like hex
has 0x?
> My question is: without
> converting to any other formats (for purposes of visual
> representation), can Java take a String, assume the String is binary as
> given, and work on it as such?
It's not the Java language itself which is doing the conversion from
Strings to numbers. Rather, it's the code inside Integer.parseInt(). I
believe if you wanted to, you could implement your own version of
parseInt(). It would probably look something like:
<roughPseudoCode>
for each character in the String {
multiply the character interpreted as a digit by the radix raised to the
power of its index (e.g. is the index is 5, the radix is 2, and the
character is '1', you have 1 * (2 ^ 5) == 32), and add this result to the
current running total.
}
</roughPseudoCode>
> Is there a prefix for binary numbers that I can append ahead of the
> given String so that it now is known as a binary number, much like hex
> has 0x?
Technically, the stuff after the 0x is not considered a String by the
Java compiler. The compiler only recognizes string literals by seeing the "
character. When it sees the sequence 0x, it knows that what's coming up is
an integer written in hexadecimal, and the compiler will treat this as an
integer the whole time, with no conversion to or from String.
You can read more about how the Java compiler treats integer literals at
http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.1
It talks about decimal, hexadecimal, and octal integer literals, but no
binary integer literals, so I guess it's not supported. When I wrote
"b1101011", I was just making up my own notation for that.
- Oliver