問題描述
我的簡(jiǎn)單問題是為什么:
My simple question is why:
System.out.println(010|4);
打印12"?我了解按位或運(yùn)算符,但為什么010"等于 8?肯定不是恭維2的通知,那這個(gè)數(shù)字怎么解碼呢?
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 語言規(guī)范,第 3.10.1 章整數(shù)文字
整數(shù)字面量可以用十進(jìn)制(以 10 為底)、十六進(jìn)制表示(以 16 為底)、八進(jìn)制(以 8 為底) 或二進(jìn)制(以 2 為底).
An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2).
[...]
八進(jìn)制數(shù)字由 ASCII 數(shù)字 0 后跟一個(gè)或多個(gè)ASCII 數(shù)字 0 到 7 之間穿插下劃線,并且可以表示正整數(shù)、零整數(shù)或負(fù)整數(shù).
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.
現(xiàn)在你應(yīng)該明白為什么010
是8
了.
Now you should understand why 010
is 8
.
這篇關(guān)于為什么選擇“010"?等于 8?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!