我试图调试一些位移操作,并且我需要在位移操作之前和之后将它们可视化。如何正确循环并打印Int,Long,Float或BigInteger的位?
I read from this answer我可能需要处理移位回填,但我不确定这意味着什么。
我认为通过询问这个问题(我如何在int中打印这些数据)我可以找出回填是什么以及可能存在的其他一些问题。
这是我的示例代码。
static string GetBits(int num)
{
StringBuilder sb = new StringBuilder();
uint bits = (uint)num;
while (bits!=0)
{
bits >>= 1;
isBitSet = // somehow do an | operation on the first bit.
// I'm unsure if it's possible to handle different data types here
// or if unsafe code and a PTR is needed
if (isBitSet)
sb.Append("1");
else
sb.Append("0");
}
}
回填这里指的是符号位(最左边位)转移逐步权利,但也维持在最左边的位置。所以最终你会得到1比特。 – siride 2013-03-09 20:40:25