1
您好我已經編寫了一個程序來切換char數組中的位。我發現當我切換第7位時,我得到錯誤的答案。請幫幫我。C中的位切換混亂
int main() {
int n,c;
char dummy;
scanf("%i", &n);
char a[13];
memset(a,0x00,13);
for(int a_i = 0; a_i < n; a_i++){
scanf("%d",&c);
dummy = c%8;
a[c/8] ^= (1<<dummy);
}
printf("\n");
for (int _i=0;_i<13;_i++)printf("%x ",a[_i]);
// int result = lonelyinteger(n, a);
// printf("%d\n", result);
return 0;
}
Input:
9
4 9 95 93 57 4 57 93 9
Output:
0 0 0 0 0 0 0 0 0 0 0 **ffffff80** 0
我需要輸出爲80而不是ffffff80。請幫我解決一下這個。
'的printf( 「%X」,一個[_i]);' - >'的printf( 「%X」,(無符號字符)一個[_i]);' – BLUEPIXY
它被符號擴展的字節到一個32位的數字,因此(無符號整數)(a [-i])可能/應該解決這個問題。或者dummy = a [_i];然後printf虛擬變量 –
@old_timer投射到'unsigned int'無法按預期工作。 – BLUEPIXY