逻辑运算后的十六进制到bin

ott*_*tto 2 c hex casting

我想要:

111 || 100  ---> 111,  not 1
100 && 100  ---> 100,  not 1
101 && 010  ---> 000,  not 0
Run Code Online (Sandbox Code Playgroud)

破碎的代码

#include <stdio.h>

main(void){
        string hexa = 0xff;
        strig hexa2 = 0xf1;

        // CONVERT TO INT??? cast
        int hexa3 = hexa || hexa2;
        int hexa4 = hexa && hexa2;

        puts(hexa3);
        puts(hexa4);
}
Run Code Online (Sandbox Code Playgroud)

R S*_*hko 11

您需要按位运算符(|,&)而不是逻辑运算符(||,&&):

110 | 011 --> 111
110 & 101 --> 100
Run Code Online (Sandbox Code Playgroud)

至于破损的代码,你也有不正确的类型hexa,hexb哪些都应该是数字类型:

int hexa = 0xff;
int hexa2 = 0xf1;
Run Code Online (Sandbox Code Playgroud)

最后,要输出一个整数,您可以使用printf它们格式化:

printf("hexa3 = 0x%08x\n", heaxa3);   // display as 8 digit, 0 padded hex
Run Code Online (Sandbox Code Playgroud)

  • || 和&&也是二元运算符.我想你的意思是说"有点". (3认同)