【发布时间】:2017-09-12 20:38:59
【问题描述】:
我正在尝试以字节为单位声明一个整数。 我的意思是:我试图将 int a = 4 声明为 int a = 0100(为简单起见,我已将其缩短为 4 位。) 以下是我使用过的代码,给了我意想不到的输出。
public class class4A_d {
public static void main(String[] args) {
System.out.println("Hello world,this is the main function ");
int q1= 00000100; //8 bits
int q2 = 00000000000000000000000000000100; //32 bits
System.out.println("q1 and q2 are respectively "+ q1 + ":" + q2); //q1= 64,q2 =64
}
}
我知道 java 将整数存储为 2 的 32 位数字,在后端进行补码,每个位的权重按以下方式进行: 2(^0),2(^1),2(^2)……等等。 但是在这里,权重似乎采用以下方式: 8(^0),8(^1),8(^2)......等等。 谁能解释一下?
【问题讨论】:
-
@Marged ..我以前看过你提到的链接,它没有回答我的问题。
-
你的位常数是错误的
标签: java integer byte type-conversion