当前位置:网站首页>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 .
边栏推荐
- 2022-5-the fourth week daily
- __ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
- Relevant information of National Natural Science Foundation of China
- 博弈论 AcWing 891. Nim游戏
- 2.Oracle-数据文件的添加及管理
- Knapsack problem acwing 9 Group knapsack problem
- 【高德地图POI踩坑】AMap.PlaceSearch无法使用
- Application of recyclerview
- How to correctly ask questions in CSDN Q & A
- P2575 master fight
猜你喜欢
ADG5412FBRUZ-RL7应用 双电源模拟开关和多路复用器IC
Operator priority, one catch, no doubt
区间问题 AcWing 906. 区间分组
Inclusion exclusion principle acwing 890 Divisible number
4. Object mapping Mapster
4. Oracle redo log file management
Game theory acwing 893 Set Nim game
VLAN experiment
Financial risk control practice -- feature derivation based on time series
LeetCode-61
随机推荐
The route of wechat applet jumps again without triggering onload
博弈论 AcWing 893. 集合-Nim游戏
Presentation of attribute value of an item
Interval problem acwing 906 Interval grouping
H5 embedded app adapts to dark mode
P3265 [jloi2015] equipment purchase
Huawei bracelet, how to add medicine reminder?
代码中的英语全部
[QT] QT multithreading development qthread
Game theory acwing 891 Nim games
Chapter 6 relational database theory
2022-5-the fourth week daily
Time is fast, please do more meaningful things
Niu Mei's math problems
LSA Type Explanation - lsa-1 [type 1 LSA - router LSA] detailed explanation
Vscode configures the typera editor for MD
Find the combination number acwing 887 Find combination number III
H5 模块悬浮拖动效果
VLAN experiment
‘mongoexport‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。