[問題] 暨南資工一題C++

作者: TonyJih (蠕動王)   2011-07-05 09:51:48
#include <iostream>
int main()
{
unsigned s=555;
int i=(s>>4) & ~(~0<<3);
std::cout<<i;
system("PAUSE");
return 0;
}
我的算法是這樣:
unsigned s=555
也就是說二進位是0000001000101011
右移4bit後=0000000000100010
not 0=1
1左移3bit後=00000000000001000
not運算後 =11111111111110111
再 and 00000000000100010
作者: TonyJih (蠕動王)   2011-07-05 10:10:00
找到問題了,問題在於我天真的認為not 0=1,實際上是1111111111111111
作者: robert6408 (Man)   2011-07-05 15:13:00
這題型 最近幾年蠻常考的加油 我是去年上暨大資工的 希望有緣見面!
作者: TonyJih (蠕動王)   2011-07-05 19:22:00
希望有機會當樓上的學弟!今天這題果然出現了,幸好有特別去解= =

Links booklink

Contact Us: admin [ a t ] ucptt.com