“Java中的整型”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“=== 怎样输出整型的二进制形式 === <syntaxhighlight lang="java"> public class Test { public static void main(String[] args) { System.out.p…”的新页面) |
Jihongchang(讨论 | 贡献) |
||
第14行: | 第14行: | ||
</syntaxhighlight><syntaxhighlight lang="console"> | </syntaxhighlight><syntaxhighlight lang="console"> | ||
binary string of 7: 111 | binary string of 7: 111 | ||
+ | binary string of -7: 11111111111111111111111111111001 | ||
+ | </syntaxhighlight>如果想要正数显示全 ,可以:<syntaxhighlight lang="java"> | ||
+ | public class Test { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | System.out.printf("binary string of %d: %s\n", 7, String.format("%32s", Integer.toBinaryString(7)).replace(' ', '0')); | ||
+ | |||
+ | System.out.printf("binary string of %d: %s\n", -7, Integer.toBinaryString(-7)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | binary string of 7: 00000000000000000000000000000111 | ||
binary string of -7: 11111111111111111111111111111001 | binary string of -7: 11111111111111111111111111111001 | ||
</syntaxhighlight> | </syntaxhighlight> |
2022年11月11日 (五) 17:29的版本
怎样输出整型的二进制形式
public class Test {
public static void main(String[] args) {
System.out.printf("binary string of %d: %s\n", 7, Integer.toBinaryString(7));
System.out.printf("binary string of %d: %s\n", -7, Integer.toBinaryString(-7));
}
}
binary string of 7: 111
binary string of -7: 11111111111111111111111111111001
如果想要正数显示全 ,可以:
public class Test {
public static void main(String[] args) {
System.out.printf("binary string of %d: %s\n", 7, String.format("%32s", Integer.toBinaryString(7)).replace(' ', '0'));
System.out.printf("binary string of %d: %s\n", -7, Integer.toBinaryString(-7));
}
}
binary string of 7: 00000000000000000000000000000111
binary string of -7: 11111111111111111111111111111001