当前位置:网站首页>Original code, inverse code, complement calculation function applet; C code implementation;
Original code, inverse code, complement calculation function applet; C code implementation;
2022-06-21 09:39:00 【ah_ yl】
We just need to keep one thing in mind ; All data is stored in the form of complement in the computer .
The following for c Language code implementation ;
It can be compiled directly , Use directly as a small application ;
#include <stdio.h>
int main()
{
char a=0;
printf("=============================================\n");
printf(" Please input data ( Range -128~127):\n");
printf(" ");
while(~scanf("%d",&a))
{
unsigned char value=0;
unsigned char bitOri[8]={
0};
unsigned char bitInv[8]={
0};
unsigned char bitPack[8]={
0};
unsigned char bitDiv[8]={
128,64,32,16,8,4,2,1};
if(a>0)
{
// A positive number , The original code is equal to the inverse code and the complement ;
value = a;
int i;
for(i=0;i<8;++i)
{
bitOri[i]=value/bitDiv[i];
value=value%bitDiv[i];
}
memcpy(bitInv,bitOri,8);
memcpy(bitPack,bitOri,8);
}
else
{
// When negative , Original code , The inverse code is the inverse of the original code divided by the sign bit , Complement is inverse +1
value=-a;
bitOri[0]=1;
bitInv[0]=1;
int i;
for(i=1;i<8;++i)
{
bitOri[i]=value/bitDiv[i];
bitInv[i]=!bitOri[i];
value=value%bitDiv[i];
}
value=a;
for(i=0;i<8;++i)
{
bitPack[i]=value/bitDiv[i];
value=value%bitDiv[i];
}
}
int i;
if(a>-128)
{
printf(" The original code of the data is : \n");
printf("\n");
printf(" ");
for(i=0;i<8;++i)
{
printf("%d ",bitOri[i]);
if(i==3)printf(" ");
}
printf("\n");
printf("\n");
printf(" The inverse code of the data is : \n");
printf("\n");
printf(" ");
for(i=0;i<8;++i)
{
printf("%d ",bitInv[i]);
if(i==3)printf(" ");
}
printf("\n");
printf("\n");
}
else
{
printf(" The original code of the data is : INVALID\n");
printf(" The inverse code of the data is : INVALID\n");
}
printf(" The complement of the data is : \n");
printf("\n");
printf(" ");
for(i=0;i<8;++i)
{
printf("%d ",bitPack[i]);
if(i==3)printf(" ");
}
printf("\n");
printf("\n");
printf("=============================================\n");
printf(" Please input data ( Range -128~127):\n");
printf(" ");
}
return 0;
}
边栏推荐
- 【实战】STM32MP157开发教程之FreeRTOS系统篇6:FreeRTOS 列表和列表项
- [practice] stm32mp157 development tutorial FreeRTOS system 6: FreeRTOS list and list items
- optional类,便利函数,创建Optional,Optional对象操作以及Optional流
- Job hopping is better than promotion
- 118. summary of basic knowledge of typescript (data type, interface, abstract class, inheritance, attribute encapsulation, modifier)
- Zhihu wanzan: what kind of programmers are still wanted by the company after the age of 35? Breaking the "middle age crisis" of programmers
- Concurrency - condition variable
- 并发-条件变量
- Uni app advanced creation component / native rendering [Day9]
- Observation on the salary data of the post-90s: poor, counselled and serious
猜你喜欢

stm32mp1 Cortex M4开发篇10:扩展板数码管控制

Verification code ----- SVG captcha

Stm32mp1 cortex M4 development part 13: external interrupt of expansion board key

Float floating layout clear floating

Stm32mp1 cortex M4 Development Part 9: expansion board air temperature and humidity sensor control

Request and response must know

安全百强 中坚力量!美创科技入选《2022年中国数字安全百强报告》

应用配置管理,基础原理分析

Style penetration of vant UI components -- that is, some styles in vant UI components cannot be modified

Underlying principle of Concurrency: thread, resource sharing, volatile keyword
随机推荐
Quick sort_ sort
QRcode dependency
R language uses strptime function, format to format output parameters, and R environment options parameters to convert strings into time objects (the output time information includes time, minutes, se
The spring recruitment is also terrible. Ali asked at the beginning of the interview: how to design a high concurrency system? I just split
Style penetration of vant UI components -- that is, some styles in vant UI components cannot be modified
Mid 2022 Summary - step by step, step by step
Ali has been working for 8 years. This learning note is left when he reaches P8. He has helped his friends get 10 offers
并发底层原理:线程、资源共享、volatile 关键字
Mobile applications introduce static Cordova according to different platforms
Introduction and template of segment tree Foundation (I)
嵌入式软件项目流程、项目启动说明书(示例)
Stm32mp1 cortex M4 development part 13: external interrupt of expansion board key
【云原生 | Kubernetes篇】Kubernetes 配置(十五)
注解的定义以及注解编译器
Equals and hashcode
领导:谁再用redis过期监听实现关闭订单,立马滚蛋!
嵌入式远程岗位、兼职、接单、众包平台
The R language uses the sink function to export the dataframe data and save it as a CSV file in the specified directory. In practice, if no directory is specified, it will be output to the current wor
2022年中总结-一步一个脚印,踩出柳暗花明
character string