我陷入困境,我一直在看我的代碼幾個小時,我似乎無法弄清楚什麼是錯的。我的函數做的是,它將採用負數或正數,並將其轉換爲32位2的補碼十六進制表示形式。我的函數需要一個字符數組(以32位有符號的幅度表示形式)並採用數組的2的補碼。我將它轉換爲二進制補碼的方法是在看到某個字符時將一個字符分配到數組中。如果數組中看到的字符是第一個字符,則會發生特殊情況。其餘的通常是否定的。我目前被卡住了。當我輸入值-23(有符號數是80000017)時,我得到值K00000KK。 -23452(簽字量80005B9C) - > K000KKKK。我將該值設置爲'K'以找出代碼被卡住的位置,原始值爲'7'。在代碼中,我把顯示消息,以查看其中編譯器會C++中的2的補碼轉換器
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被示出爲其中i得到的第1個消息,並且其中0被示我得到的 - > 0消息
謝謝你的幫助!非常感激!
這裏是一個引擎收錄鏈接到我的代碼,因爲代碼的格式讓我生氣......
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;
}
啓用編譯器警告。 – Lundin