什么是"?1:0"的意思

pon*_*dev 1 c bitwise-operators

我不明白第三条线要完成什么.我刚刚学会了逐位运算符.如果有人能通过最后两行走我的话会很棒.我理解移位运算符但是移位运算符我不完全确定它的含义.

    void create(uint8_t bInt[], int64_t num){
      for (int pos = 0; pos < 32; pos++){ 
        bInt[pos] = (num & mask) ? 1 : 0;
        mask = mask << 1;
         }
       }
Run Code Online (Sandbox Code Playgroud)

对于此赋值,我们使用uint8_t值的32元素数组来表示32位整数.例如,二进制的整数84193是0 .... 0001 0100 1000 1110 0001.在bInt []中,它将被存储为1000 0111 0001 0010 1000 0000 .... 0.感谢您的时间

Rya*_*yan 6

?:是一个三元运算符.(num & mask) ? 1 : 0;

可以这样想:

if( (num & mask) ) {
    bInt[pos] = 1
} else {
    bInt[pos] = 0
}
Run Code Online (Sandbox Code Playgroud)