按位补码运算符 (~) 在 C 中的某个点不起作用
Bitwise complement operator (~) not working at a point in C
此代码采用 2 字节 int 并交换字节。
为什么我评论的这段代码中的行似乎不起作用?
INPUT/OUTPUT
*当我输入 4 时,预期输出为 1024 而不是第 9 位到第 16 位在通过该行后全部设置为“1”。
*然后我尝试输入 65280,其预期输出为 255 而不是输出 65535(将所有 16 位设置为“1”
#include<stdio.h>
int main(void)
{
short unsigned int num;
printf("Enter the number: ");
fscanf(stdin,"%hu",&num);
printf("\nNumber with no swap between bytes---> %hu\n",num);
unsigned char swapa,swapb;
swapa=~num;
num>>=8;
swapb=~num;
num=~swapa;
num<<=8;
num=~swapb; //this line is not working why
printf("Swaped bytes value----> %hu\n",num);
}
Integral promotions num
的当前值也被注释行破坏了,可能需要 |=
、+=
或 ^=
。
此代码采用 2 字节 int 并交换字节。
为什么我评论的这段代码中的行似乎不起作用?
INPUT/OUTPUT
*当我输入 4 时,预期输出为 1024 而不是第 9 位到第 16 位在通过该行后全部设置为“1”。
*然后我尝试输入 65280,其预期输出为 255 而不是输出 65535(将所有 16 位设置为“1”
#include<stdio.h>
int main(void)
{
short unsigned int num;
printf("Enter the number: ");
fscanf(stdin,"%hu",&num);
printf("\nNumber with no swap between bytes---> %hu\n",num);
unsigned char swapa,swapb;
swapa=~num;
num>>=8;
swapb=~num;
num=~swapa;
num<<=8;
num=~swapb; //this line is not working why
printf("Swaped bytes value----> %hu\n",num);
}
Integral promotions num
的当前值也被注释行破坏了,可能需要 |=
、+=
或 ^=
。