当前位置:网站首页>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 .
边栏推荐
- SolidWorks template and design library are convenient for designers to call
- 5.Oracle-錶空間
- [Chongqing Guangdong education] 1185t administrative leadership reference test of National Open University in autumn 2018
- International Open Source firmware Foundation (osff) organization
- LeetCode-54
- [learning] database: several cases of index failure
- Using handler in a new thread
- 【LeetCode】Easy | 20. Valid parentheses
- 5. Oracle tablespace
- The “mode“ argument must be integer. Received an instance of Object
猜你喜欢

Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software

【LeetCode】Easy | 20. Valid parentheses

Vant weave swipecell sets multiple buttons

阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长

Game theory acwing 893 Set Nim game

Client use of Argo CD installation

Single chip computer engineering experience - layered idea

Stack acwing 3302 Expression evaluation

7.Oracle-表结构

Redis-01. First meet redis
随机推荐
MQClientException: No route info of this topic: type_ topic
Applicable to Net free barcode API [off] - free barcode API for NET [closed]
1. Create Oracle database manually
Find the combination number acwing 887 Find combination number III
Inclusion exclusion principle acwing 890 Divisible number
Adg5412fbruz-rl7 applies dual power analog switch and multiplexer IC
Vscode configures the typera editor for MD
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
Winter vacation water test 1 Summary
时间很快,请多做有意义的事情
TCP's understanding of three handshakes and four waves
Redis-01. First meet redis
11-gorm-v2-03-basic query
How to correctly ask questions in CSDN Q & A
H5 embedded app adapts to dark mode
Record the process of configuring nccl and horovod in these two days (original)
Record of problems in ollvm compilation
Single chip computer engineering experience - layered idea
求组合数 AcWing 887. 求组合数 III