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