#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
void bcode(int num, char strb[33]) {
unsigned int data = 1 << 31;
for (int i = 1;i <= 32;i++) {
if (num & data) {
strb[i - 1] = '1';
}
else {
strb[i - 1] = '0';
}
num <<= 1;
}
}
void fcode(int num,char strf[33]) {
unsigned int data = 1 << 31;
if (num < 0)
{
num = num - 1;
}
for (int i = 1;i <= 32;i++) {
if (num & data) {
strf[i - 1] = '1';
}
else {
strf[i - 1] = '0';
}
num <<= 1;
}
}
void ycode(int num, char stry[33]) {
unsigned int data = 1 << 31;
if (num < 0)
{
num = ~num + 1;
num = num | data;
}
for (int i = 1;i <= 32;i++) {
if (num & data) {
stry[i - 1] = '1';
}
else {
stry[i - 1] = '0';
}
num <<= 1;
}
}
void main()
{
int num;
//多后面一个结束符 \0
char stry[33] = { 0 };
char strf[33] = { 0 };
char strb[33] = { 0 };
scanf("%d", &num);
bcode(num, strb);
printf("%s", strb);
fcode(num, strf);
printf("\n%s", strf);
ycode(num, stry);
printf("\n%s", stry);
system("pause");
}
转载地址:https://edu.51cto.com/center/course/lesson/index?id=628373