From 285a5d6d95c7294dd235dd8975bcf79a2b7e44b5 Mon Sep 17 00:00:00 2001 From: lsrong Date: Tue, 31 Oct 2023 23:11:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8D=B0=E4=B8=80=E4=B8=AA=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E7=9A=8432=E4=BD=8D=20=E7=9A=84=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 +++ tyro/src/main/java/Code01_PrintBinary.java | 53 ++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tyro/src/main/java/Code01_PrintBinary.java diff --git a/.gitignore b/.gitignore index 9154f4c..d1d1f59 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,10 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* replay_pid* +**/mvnw +**/mvnw.cmd +**/.gitignore +**/.mvn +**/target +.idea diff --git a/tyro/src/main/java/Code01_PrintBinary.java b/tyro/src/main/java/Code01_PrintBinary.java new file mode 100644 index 0000000..af9925a --- /dev/null +++ b/tyro/src/main/java/Code01_PrintBinary.java @@ -0,0 +1,53 @@ +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); + } +}