十六进制到一个补码的转换给出错误的输出?

时间:2019-12-23 17:24:51

标签: c hex

我需要帮助以找出为什么我的十六进制补码有时会输出正确的答案,而有时却在实际补码前面输出C或ffff或其他随机数。

我的屏蔽操作和移位操作按预期进行;我不明白为什么有时我的补码有正确的输出,而有时却没有。

这是我的完整代码:

#include <stdio.h>

void menu(unsigned i);
void onesCompliment(unsigned i);
void AND (unsigned i);
void OR (unsigned i);                                           
void XOR (unsigned i);
void LEFT(unsigned i);
void RIGHT(unsigned i);

main(){
    unsigned int hex1;                                          

    printf("Please enter a Hexadecimal: ");                     
    scanf("%x", &hex1);                                         

    menu(hex1);                                                 

    return 0;                                                   
}

void menu(unsigned i){                                          

    int input;                                                  

    printf("\nWhat would you like to do next? (type the number)\n"); 
    printf("1) Ones Compliment: \n");
    printf("2) Masking operation: \n");                         
    printf("3) Bit shifting operation: \n");
    printf("4) Exit: \n\n");

    scanf("%d",&input);                                         

    if (input == 1){                                            
        onesCompliment(i);                                      

}
void onesCompliment(unsigned i){                                
    printf("Hexandecimal values: #1= %x Ones Compliment= %x\n", i, ~i);
}

0 个答案:

没有答案