C按位,以十六进制逼近零.0x15000000 - > 0x15 ??? 怎么样?

Rea*_*ude 1 c++

这甚至可能吗?

我怎么会截断零?

在withOUT中使用任何掩码技术的整数(不允许:0x15000000和0xff000000之类的.).而且没有任何铸造.

Joh*_*zen 5

嗯,真的,如果你想截断右侧,天真的解决方案是:

uint input = 0x150000;
if(input)
{
    while(!(input & 0x01))  // Replace with while(!(input % 0x10)) if you are actually against masking.
    {
        input >>= 1;
    }
}

// input, here, will be 0x15.
Run Code Online (Sandbox Code Playgroud)

但是,您可以展开此循环.如:

if(!(input & 0xFFFF)) { input >>= 16; }
if(!(input & 0x00FF)) { input >>= 8;  }
if(!(input & 0x000F)) { input >>= 4;  }  // Comment this line, down, if you want to align on bytes.
if(!(input & 0x0003)) { input >>= 2;  }  // Likewise here, down, to align on nybbles.
if(!(input & 0x0001)) { input >>= 1;  }
Run Code Online (Sandbox Code Playgroud)

  • 我会测试0.否则你可能会进入无限循环. (3认同)
  • 不完全的.这会截断1.while(!(输入&0x01)) (2认同)