@nrailgun
        
        2015-09-22T09:30:03.000000Z
        字数 746
        阅读 1703
    程序设计
C 程序员经常面对底层问题,很多经常使用位偏移操作。在使用中发现一个陷阱:
C 的
<<与>>运算符优先级低于+和-
我写出了这样的代码(内核查找多处理器浮点结构):
else {/* Last KB of system base memory */p = (bda[0x14] << 8) | bda[0x13];p = p << 10 - 1024;mp = mp_lookup_fp_struct_at(p, 1024);if (mp)return mp;}
向左偏移 << 10,机智),然后减去 
gcc -I. -I../include -m32 -static -nostdinc -fno-builtin -ggdb -MD -O -fno-strict-aliasing -fno-pic -fno-omit-frame-pointer -fno-stack-protector -Werror -c mp.cmp.c: In function ‘mp_lookup_fp_struct’:mp.c:42:3: error: left shift count is negative [-Werror]p = p << 10 - 1024;^cc1: all warnings being treated as errors
我忘记了运算符的优先结合顺序,+ 大于 <<。运气实在是太好了,如果不是 10 - 1024 正好小于 
Generally: unary 
