我被卡住了,我一直在查看我的代码几个小时,我似乎无法弄清楚出了什么问题。我的函数所做的是它将接受一个负数或正数并将其转换为它的 32 位 2 的补码十六进制表示。我的函数接受一个字符数组(以 32 位有符号幅度表示)并取数组的 2 的补码。我将其转换为 2 的补码的方法是在看到某个字符时将一个字符分配到数组中。如果在数组中看到的字符是第一个字符,则会出现特殊情况。其余的通常被否定。我目前被卡住了。当我输入值 -23(有符号幅度为 80000017)时,我得到值 K00000KK。-23452(有符号的震级 80005B9C)-> K000KKKK。我将值设置为“K” 要找出代码卡在哪里,原始值是“7”。在代码中,我放置了显示消息以查看编译器的去向
for -23 (80000017) input I get the messages
first 1 is seen, leave first 1 the same
first 1 is seen, leave first 1 the same
no one's showed up yet --> 0
no one's showed up yet --> 0
no one's showed up yet --> 0
no one's showed up yet --> 0
no one's showed up yet --> 0
first 1 is seen, leave first 1 the same
几乎显示 K 的地方是我得到第一个 1 消息的地方,显示 0 的地方我得到 --> 0 消息
谢谢您的帮助!非常感激!
这是我的代码的 pastebin 链接,因为代码的格式让我很恼火......
char* two_complement(int number, char* sign_mag_array){
int first_one_flag = 0;
if(number > 0){
cout << "positive number" << endl;
return sign_mag_array;
}
if(number < 0){
for(int q = 7; q >= 0; q--){
if(first_one_flag == 1){ // negate the rest regularly
cout << "negate normally " << endl;
if(sign_mag_array[q] == '0'){
sign_mag_array[q] = 'F';
}
if(sign_mag_array[q] == '1'){
sign_mag_array[q] = 'E';
}
if(sign_mag_array[q] == '2'){
sign_mag_array[q] = 'D';
}
if(sign_mag_array[q] == '3'){
sign_mag_array[q] = 'C';
}
if(sign_mag_array[q] == '4'){
sign_mag_array[q] = 'D';
}
if(sign_mag_array[q] == '5'){
sign_mag_array[q] = 'A';
}
if(sign_mag_array[q] = '6'){
sign_mag_array[q] = '9';
}
if(sign_mag_array[q] == '7'){
sign_mag_array[q] = '8';
}
if(sign_mag_array[q] == '8'){
sign_mag_array[q] = '7';
}
if(sign_mag_array[q] = '9'){
sign_mag_array[q] = '6';
}
if(sign_mag_array[q] == 'A'){
sign_mag_array[q] = '5';
}
if(sign_mag_array[q] == 'B'){
sign_mag_array[q] = '4';
}
if(sign_mag_array[q] = 'C'){
sign_mag_array[q] = '3';
}
if(sign_mag_array[q] == 'D'){
sign_mag_array[q] = '2';
}
if(sign_mag_array[q] == 'E'){
sign_mag_array[q] = '1';
}
if(sign_mag_array[q] == 'F'){
sign_mag_array[q] = '0';
}
}
if(sign_mag_array[q] == '0' && first_one_flag == 0){
cout << "no one's showed up yet --> 0 " << endl;
}
else{ // first '1' in binary seen. special negating
//first_one_flag = 1;
cout << "first 1 is seen, leave first 1 the same " << endl;
if(sign_mag_array[q] == '1'){
sign_mag_array[q] = 'F';
}
if(sign_mag_array[q] == '2'){
sign_mag_array[q] = 'E';
}
if(sign_mag_array[q] == '3'){
sign_mag_array[q] = 'D';
}
if(sign_mag_array[q] == '4'){
sign_mag_array[q] = 'C';
}
if(sign_mag_array[q] == '5'){
sign_mag_array[q] = 'B';
}
if(sign_mag_array[q] == '6'){
sign_mag_array[q] = 'A';
}
if(sign_mag_array[q] = '7'){
sign_mag_array[q] = '9';
}
if(sign_mag_array[q] == '8'){
sign_mag_array[q] = '8';
}
if(sign_mag_array[q] == '9'){
sign_mag_array[q] = 'K';
}
if(sign_mag_array[q] == 'A'){
sign_mag_array[q] = '6';
}
if(sign_mag_array[q] == 'B'){
sign_mag_array[q] = '5';
}
if(sign_mag_array[q] == 'C'){
sign_mag_array[q] = '4';
}
if(sign_mag_array[q] == 'D'){
sign_mag_array[q] = '3';
}
if(sign_mag_array[q] == 'E'){
sign_mag_array[q] = '2';
}
if(sign_mag_array[q] == 'F'){
sign_mag_array[q] = '1';
}
}
}
}
return sign_mag_array;
}