You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

public class Code01_PrintBinary {
// << 左移运算-无论什么数字都是一样的操作逻辑二进制整体左移最左边用0来填充
/**
* 打印一个数字的32位 的状态
* @param num
*/
public static void printBinary(int num){
for(int i = 31; i >= 0; i--){
// System.out.print((num & (1 << i)) == 0 ?"0":"1");
int a = num & (1 << i); // 1 左移 i 位 10**0 然后在和num 与 位运算
System.out.print(a == 0 ? "0" : "1");
}
System.out.println();
}
public static void main(String[] args){
int num = 321312;
//printBinary(num);
// 有符号 无符号
num = Integer.MAX_VALUE;
// printBinary(num);
// printBinary(-1);
int a = Integer.MIN_VALUE; // 负数: 第一位为符号位后面取反加1
// printBinary(a);
// 取反
// int b = 12345;
// int c = ~b;
// printBinary(b);
// printBinary(c);
// printBinary(-5);
// 负数最小值的数值并正数的大1 -2^32 ~ 2^32-1(忽略符号位)
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
int c = -5;
int d =-c;
d = (~c + 1);
System.out.println(c);
System.out.println(d);
int e = Integer.MIN_VALUE; // 负数最小为 本身, 取反加1 二进制就是本身
printBinary(e);
printBinary(-e);
}
}