【发布时间】:2012-07-03 15:17:35
【问题描述】:
好的,我以一种很好的方式完成了代码,并使用了递增 ++ 和递减 -- 运算符。
unsigned int atob(const char* input)
{
int i = 0;
while (input[i] == '0' || input[i] == '1') i++;
unsigned result = 0;
unsigned currentBit = --i;
while ((*input == '0') || (*input == '1')) {
char isCurrentBitSet = *input == '1';
unsigned setValue = (isCurrentBitSet << currentBit--);
result |= setValue;
input++;
}
return result;
}
现在,我需要删除所有 dec(--)/inc(++),除了 while 语句底部的 input++。我对如何执行此实现感到困惑。
【问题讨论】:
-
你需要摆脱增量和减量的原因是什么?
-
@EvilTeach:我只是在猜测,但我想说是他的老师(教授,随便)间接地试图推动他考虑不同的解决方案。
-
有点接近。不过我只是为了学习!
标签: c bit-manipulation