为什么选择“010"?等于 8? [英] Why "010" equals 8?

查看:38
本文介绍了为什么选择“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.

现在你应该明白为什么0108了.

Now you should understand why 010 is 8.

这篇关于为什么选择“010"?等于 8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆