这甚至可能吗?
我怎么会截断零?
在withOUT中使用任何掩码技术的整数(不允许:0x15000000和0xff000000之类的.).而且没有任何铸造.
嗯,真的,如果你想截断右侧,天真的解决方案是:
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)