为什么选择“010"?等于 8?
我的简单问题是为什么:
My simple question is why:
System.out.println(010|4);
打印12"?我了解按位或运算符,但为什么010"等于 8?肯定不是恭维2的通知,那这个数字怎么解码呢?
prints "12"? I understand bitwise OR operator but why "010" equals 8? It's definitely not compliment 2's notification, so how to decode this number?
推荐答案
看看Java 语言规范,第 3.10.1 章整数文字
整数字面量可以用十进制(以 10 为底)、十六进制表示(以 16 为底)、八进制(以 8 为底) 或二进制(以 2 为底).
An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).
[...]
八进制数字由 ASCII 数字 0 后跟一个或多个ASCII 数字 0 到 7 之间穿插下划线,并且可以表示正整数、零整数或负整数.
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.
现在你应该明白为什么010
是8
了.
Now you should understand why 010
is 8
.
相关文章