简体   繁体   中英

C programme bitwise: value of -1 >> 1

For GCC 32 bits, -1 >> 1 returns me FFFFFFFF, but I thought after 2's complement, I will get 0111 1111 ... 1111 which should be 7fff ffff. did i miss something?

Under most implementations, that operator does an arithmetic shift for signed types, so it preserves the sign bit (which is the leftmost bit), in this case 1 .

As @Clifford correctly pointed out, the language standard leaves the implementation of >> up to the implementor.

See the Wikipedia article for details.

For E1 >> E2 , if E1 is negative, then the behavior is implementation-defined, which means different compilers could use different strategies to implement it.

Apparently GCC choose arithmetic shift, as pointed out by @merlin2011

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM