当前位置:网站首页>Integer to 8-bit binary explanation (including positive and negative numbers) scope of application -127~+127
Integer to 8-bit binary explanation (including positive and negative numbers) scope of application -127~+127
2022-07-05 06:35:00 【Said it was all】
Look directly at the code , There are notes , What's wrong, please correct , The first bit of binary is sign bit :
public class TheTwoTogether {
public static void main(String[] args) {
// Convert string to binary number
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
if (a < 0) {
String s1[] =eightbinary(a);
s1[0]="1";
for (String s:s1){
System.out.print(s);
}
}
else {
String s1[] =eightbinary(a);
for (String s:s1){
System.out.print(s);
}
}
}
private static String[] eightbinary(int a){
int b = Math.abs(a);// Take the absolute value
String s = Integer.toBinaryString(b);
// The next step is for : Will be for Integer Class binary conversion to 8 Bit Integer Class binary . because %08d Medium d It is not required to be an integer type
Integer s2=Integer.valueOf(s);
// Will be for Integer Class binary conversion to 8 Bit Integer Class binary ( Probably not 8 position , such as 3---->11, Namely 2 position )
String sb1 = String.format("%08d", s2);
int arr[] = new int[8];
// Separate eight binary bits , One by one , Deposit to string An array of types sarray in
String[] sarray = sb1.split("");
return sarray;
}
}
The scope of application of this code -127~+127, The reason is that the limitation of my transformation is 8 Bit binary , And the first bit is the sign bit , If you want to expand the scope , Change it to hexadecimal or 32nd , The way of thinking remains unchanged .
边栏推荐
- Chapter 6 relational database theory
- 博弈论 AcWing 893. 集合-Nim游戏
- Adg5412fbruz-rl7 applies dual power analog switch and multiplexer IC
- ‘mongoexport‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
- 博弈论 AcWing 894. 拆分-Nim游戏
- MPLS experiment
- Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
- Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
- 容斥原理 AcWing 890. 能被整除的数
- Huawei bracelet, how to add medicine reminder?
猜你喜欢
'mongoexport 'is not an internal or external command, nor is it a runnable program or batch file.
Vant Weapp SwipeCell設置多個按鈕
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
What is socket? Basic introduction to socket
P2575 master fight
2021apmcm post game Summary - edge detection
容斥原理 AcWing 890. 能被整除的数
5. Oracle tablespace
论文阅读报告
20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
随机推荐
MPLS experiment
高斯消元 AcWing 884. 高斯消元解异或线性方程组
ollvm编译出现的问题纪录
Configuration method and configuration file of SolidWorks GB profile library
什么是套接字?Socket基本介绍
Application of recyclerview
Single chip computer engineering experience - layered idea
NVM Downloading npm version 6.7.0... Error
How to answer when you encounter a jet on CSDN?
数据库Mysql全部
7.Oracle-表结构
Knapsack problem acwing 9 Group knapsack problem
LSA Type Explanation - lsa-5 (type 5 LSA - autonomous system external LSA) and lsa-4 (type 4 LSA - ASBR summary LSA) explanation
代码中的英语全部
5. Oracle tablespace
vsCode创建自己的代码模板
Game theory acwing 893 Set Nim game
[QT] QT multithreading development qthread
阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
3. Oracle control file management