Two‘s Complement(二进制补码)

十进制转十六进制

8位char和16位short十六进制输出格式分别为%hhx,%hx

32位int和64位longlong十六进制输出格式分别为%x,%llx

8位char型

Input an integer and it's type. such as

127 char

outout the two's complement of them.

7fH

#include <bits/stdc++.h>
using namespace std;
int main() {
    long long  num;
    string st;
    while(scanf("%lld",&num)!=-1){
        cin>>st;
        if(st=="char") {
            printf("%02hhxH", (char) num);
        }
        cout<<endl;
    }
    return 0;
}

16位short型

Input an integer and it's type. such as

127 short

outout the two's complement of them.

007fH

#include <bits/stdc++.h>
using namespace std;
int main() {
    long long int num;
    string st;
    while(scanf("%lld",&num)!=-1){
        cin>>st;
        if (st=="short") {
            printf("%04hxH",(short)num);
        }
        cout<<endl;
    }
    return 0;
}

32位int

Input an integer and it's type. such as

127 int

outout the two's complement of them.

0000007fH

#include <bits/stdc++.h>
using namespace std;
int main() {
    long long int num;
    string st;
    while(scanf("%lld",&num)!=-1){
        cin>>st;
        if(st=="int") {
            printf("%08xH",(int)num);
        }
        cout<<endl;
    }
    return 0;
}

64位longlong

Input an integer and it's type. such as

127 longlong

outout the two's complement of them.

000000000000007fH

#include <bits/stdc++.h>
using namespace std;
int main() {
    long long num;
    string st;
    while(scanf("%lld",&num)!=-1){
        cin>>st;
        if(st=="longlong") {
            printf("%016llxH",num );
        }
        cout<<endl;
    }
    return 0;
}

十六进制转十进制

 8位char和16位short十进制输出格式分别为%hhd,%hd

32位int和64位longlong十进制输出格式分别为%d,%lld

8位char型

Input an integer and it's type. such as

7f char

outout the two's complement of them.

127

#include <bits/stdc++.h>
using namespace std;
int main() {
    long long int num;
    string cos;
    while(scanf("%llx",&num)!=-1){
        cin>>cos;
        if(cos=="char") {
            printf("%hhd", num);
        }
        cout<<endl;
    }
    return 0;
}

16位short型

Input an integer and it's type. such as

007f short

outout the two's complement of them.

127

#include <bits/stdc++.h>
using namespace std;
int main() {
    long long int num;
    string cos;
    while(scanf("%llx",&num)!=-1){
        cin>>cos;
        if (cos=="short") {
            printf("%hd",num);
        }
        cout<<endl;
    }
    return 0;
}

32位int

Input an integer and it's type. such as

0000007f int

outout the two's complement of them.

127

#include <bits/stdc++.h>
using namespace std;
int main() {
    long long int num;
    string cos;
    while(scanf("%llx",&num)!=-1){
        cin>>cos;
        if(cos=="int") {
            printf("%d",num);
        }
        cout<<endl;
    }
    return 0;
}

64位longlong

Input an integer and it's type. such as

000000000000007f longlong

outout the two's complement of them.

127

#include <bits/stdc++.h>
using namespace std;
int main() {
    long long int num;
    string cos;
    while(scanf("%llx",&num)!=-1){
        cin>>cos;
        if(cos=="longlong") {
            printf("%lld",num );
        }
        cout<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个将十进制带符号整数转换为十六进制补码表示字符串的C语言程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> char* decToHexComplement(int dec) { // Allocate a string to hold the hexadecimal complement representation char* hex = (char*) malloc(sizeof(char) * 9); memset(hex, 0, sizeof(char) * 9); // Initialize the string to all zeros // Compute the two's complement of the decimal number int complement = ~dec + 1; // Convert the complement to a hexadecimal string sprintf(hex, "%.8X", complement); return hex; } int main() { int dec; printf("Enter a signed decimal integer: "); scanf("%d", &dec); char* hex = decToHexComplement(dec); printf("Hexadecimal two's complement: %s\n", hex); free(hex); return 0; } ``` 该程序使用`decToHexComplement()`函数将带符号十进制整数转换为其补码表示的十六进制字符串。该函数首先计算输入数字的二进制补码,然后使用sprintf函数将其转换为一个八位十六进制字符串。注意,我们使用sprintf函数而不是printf函数来格式化输出,因为我们需要将字符串返回给主函数。 在主函数中,我们使用`scanf()`函数获取用户输入的带符号十进制整数,并将其传递给`decToHexComplement()`函数。然后,我们将返回的十六进制补码字符串打印到控制台,并使用`free()`函数释放先前分配的字符串内存空间。 请注意,该程序假设整数类型在该平台上是32位带符号整数。如果该平台上的整数类型不是这种类型,则可能需要对程序进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值