当前位置:网站首页>Decimal to binary advanced version (can convert negative numbers and boundary values)
Decimal to binary advanced version (can convert negative numbers and boundary values)
2022-07-28 18:50:00 【BSP primary school monk】
Decimal to binary
Ten Base number Negative to binary
Let's say I have a int Number of types , The value is 5, that , We know that it's represented in a computer as : ( because java in int yes 4 Bytes , So the high position needs to be filled 0, Occupy enough 32 position )
00000000 00000000 00000000 00000101
Now I want to know ,-5 How to express in a computer ?
In the computer , Negative numbers begin with Original code In the form of complement .
What do you mean Complement code Well ? This has to come from the original , The irony is that .
Original code : A positive number , Converted to... According to the absolute value Binary system Count ; A negative number , Binary number converted by absolute value size , And then the highest bit makes up for 1, It's called the original code .
such as :
5 The original code of : 00000000 00000000 00000000 00000101
-5 The original code of : 10000000 00000000 00000000 00000101
Inverse code : The inverse of a positive number is the same as the original , The inverse of a negative number is the inverse of the original code of the number except for the sign bit .
Reverse operation means : originally 1, have to 0; originally 0, have to 1.(1 change 0; 0 change 1)
such as :
Positive numbers : 00000000 00000000 00000000 00000101 It's the inverse of 00000000 00000000 00000000 00000101 ;
negative : 10000000 00000000 00000000 00000101 Reverse each bit ( Except the sign bit ), have to 11111111 11111111 11111111 11111010.
call :10000000 00000000 00000000 00000101 and 11111111 11111111 11111111 11111010 It's the opposite of each other .
Complement code : The complement of a positive number is the same as the original code , The complement of a negative number is to invert the original code of the number except the sign bit , Then add... To the last 1.
such as :
-5 The original code of :10000000 00000000 00000000 00000101
-5 The inverse of :11111111 11111111 11111111 11111010
-5 Complement :11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011
therefore ,-5 It's expressed in a computer as :11111111 11111111 11111111 11111011. Convert to hex :0xFFFFFFFB.
Take another example , Let's look at integers -1 How to express in a computer :
Suppose this is also a int type , that :
1、 First -1 The original code of :10000000 00000000 00000000 00000001
2、 We have to counter : 11111111 11111111 11111111 11111110( The division sign bit is reversed bit by bit )
3、 Get complement : 11111111 11111111 11111111 11111111
#include <stdio.h>
int main()
{
while(1)
{
int i,count=0,j=0;
long long n=0;
int a[64]={0};
printf("\n");
printf(" Please enter a decimal integer :\n");
scanf("%lld",&n);
if(n>=0)
{
while(n!=0)
{
a[count]=n%2;
n=n/2;
count++;
}
printf(" The value is converted to binary number :");
for(i=63;i>=0;i--)
{
if((i+1) % 4 == 0 && i != 63)
printf(" ");
printf("%d",a[i]);
}
}
else
{
n++;
n=-n;// Take the absolute value first
while(n!=0)
{
a[count]=n%2;
n=n/2;
count++;
}
for(i=0;i<64;i++)// Take the inverse ,0 change 1,1 change 0
{
if(a[i]==0)
a[i]=1;
else
a[i]=0;
}
printf(" The value is converted to binary number :");
for(i=63;i>=0;i--)
{
if((i+1) % 4 == 0 && i != 63)
printf(" ");
printf("%d",a[i]);
}
}
}
return 0;
}Running results :

Employee ranking :
#include <stdio.h>
int count(int arr_08[7],int x);
int main()
{
int i,j,sum=0,temp=0,arr_02[2][8],arr[8][7]={
{2,4,3,4,5,8,8},
{7,3,4,3,3,4,4},
{3,3,4,3,3,2,2},
{9,3,4,7,3,4,1},
{3,5,4,3,6,3,8},
{3,4,4,6,3,4,4},
{3,7,4,8,3,8,4},
{6,3,5,9,2,7,9}};
printf(" Statistics of working hours of each employee :\n");
for(i=0;i<8;i++)// Print work duration
{
for(j=0;j<7;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
printf(" The total working hours of each employee are :\n");
for(i=0;i<8;i++)
{
sum=0;
for(j=0;j<7;j++)
sum += arr[i][j];
printf("%d\t",sum);
arr_02[0][i]=sum;
arr_02[1][i]=i;
}
printf("\n");
for(i=0;i<2;i++)
{
for(j=0;j<8;j++)
printf("%d\t",arr_02[i][j]);
printf("\n");
}
for(i=0;i<8;i++)
{
for(j=0;j<8-i-1;j++)
{
if(arr_02[0][j]<arr_02[0][j+1])
{
temp=arr_02[0][j];
arr_02[0][j]=arr_02[0][j+1];
arr_02[0][j+1]=temp;
temp=arr_02[1][j];
arr_02[1][j]=arr_02[1][j+1];
arr_02[1][j+1]=temp;
}
}
}
printf(" The total working hours of each employee are in descending order :\n");
for(j=0;j<8;j++)
printf("%d\t",arr_02[0][j]);
printf("\n");
printf(" The total duration of each employee's employee number is in descending order :\n");
for(j=0;j<8;j++)
printf("%d\t",arr_02[1][j]);
return 0;
}
Running results :

边栏推荐
- 注意力机制及代码实现
- Gaode map realizes customized small blue dots, customized point markers, drawing polygon / circular areas, and displaying or hiding customized point markers according to the movement of the map
- Golang 并发之锁
- 2022.7.26 构造函数,面试:new的作用、深拷贝和浅拷贝
- C# 之 观察者模式实例 -- 订牛奶
- MySQL advanced mvcc (ultra detailed collation)
- LeetCode_1137_第N个泰波那契数
- UE5 GAS 学习笔记 1.7 任务Ability Tasks
- Bubble sorting and Related videos
- MYSQL入门与进阶(三)
猜你喜欢

十进制转二进制进阶版(可转化负数以及边界值)

MySQL index usage and optimization

广告推荐CTR点击率预测实践项目!
![[actual combat] realize page distortion correction with OpenCV](/img/7b/7e25bde34a9d5463af3dd40599c80e.png)
[actual combat] realize page distortion correction with OpenCV

1.1、稀疏数组

Use the self-developed proxy server to solve the cross domain access errors encountered when uploading files by SAP ui5 fileuploader trial version

C# 之 观察者模式实例 -- 订牛奶
![[GXYCTF2019]StrongestMind](/img/f4/61932548e0c7dd60d790d31fb5b96b.png)
[GXYCTF2019]StrongestMind

专题讲座6 树形dp 学习心得(长期更新)

Unity 之 切换语言导致报错:System.FormatException:String was not recognized as a valid DateTime.
随机推荐
MYSQL入门与进阶(一)
Meta Q2财报:营收首次下滑,Metaverse将与苹果竞争
EasyCVR接入设备后播放视频出现卡顿现象的原因分析及解决
Record your interview experience in Xiamen for two years -- Conclusion
Ue5 gas learning notes 8.0 references
Tencent Tang Daosheng: open source is a new mode of production and collaboration in the era of industrial Internet
UE5 GAS 学习笔记 1.5 Gameplay Effects游戏效果
MongoDB数据库复制表
MySQL advanced mvcc (ultra detailed collation)
历史上的今天:微软收购 QDOS;模型检测先驱出生;第一张激光照排的中文报纸...
kotlin:out in
Gateway入门
MYSQL入门与进阶(五)
Interviewer: what are the usage scenarios of ThreadLocal? How to avoid memory leakage?
UE5 GAS 学习笔记 1.3属性Attribute
jvm四种引用类型
先验、后验、似然
408复习策略(强化阶段)
Docker builds MySQL master-slave replication
十进制转二进制进阶版(可转化负数以及边界值)