且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C中的移位运算符(<<、>>)是算术还是逻辑?

更新时间:2023-11-10 12:35:52

根据 K&R 第 2 版 结果取决于有符号值右移的实现.

According to K&R 2nd edition the results are implementation-dependent for right shifts of signed values.

***说 C/C++通常"对有符号值实现算术移位.

Wikipedia says that C/C++ 'usually' implements an arithmetic shift on signed values.

基本上,您需要测试编译器或不依赖它.我对当前 MS C++ 编译器的 VS2008 帮助说他们的编译器进行了算术移位.

Basically you need to either test your compiler or not rely on it. My VS2008 help for the current MS C++ compiler says that their compiler does an arithmetic shift.