C99 规范规定:
The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2^E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.
我很想知道,哪些实现/编译器不会将已签名的 E1 >> 31
视为一堆 11111....
?
最佳答案
大多数用于微 Controller 的嵌入式编译器倾向于逻辑移位(零移位)而不是算术移位(符号位移位)。
这可能是因为带符号的数字在嵌入式系统中很少见,因为与使用屏幕的桌面编程相比,此类编程更接近硬件而远离用户。
签名数字毕竟只是用户展示。如果您不需要向用户打印数字,那么您根本不需要经常使用带符号的数字。
当然,一开始就对有符号数使用 shift 并没有任何意义。在我的编程生涯中,我从未遇到过需要这样做的情况。这意味着在大多数情况下,这种转变只是偶然的错误。
关于c - 有符号整数的算术右移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36311912/