A thread in the PIC/SX mailing list caused some discomfort about the workings of the shift right operation in different languages. Until now, I was convinced that the correct way to interpret that operation was in the 'logical' context, i.e., a clean bit operation, ignoring any significance of the MSb (=sign).
It seems it isn't that simple. It isn't even simple: some languages, like C, don't even specify how it will work: The C99 standard says it is 'implementation-dependent'. So C-compilers can differ in the way they do it.
Below are some texts I ran, using different languages/implementations.
Language | Compiler/Version | shift result | divide/2 | Comments |
C | gcc 4.2.3 | -8 | -8 | Test with 'int' type |
|
|
2147483640 | 2147483640 | Test with 'unsigned' type |
Pascal | fpc 2.2.0 | 2147483640 | -8 | Test with 'integer' type |
|
|
2147483640 | 2147483640 | Test with 'longword' type |
Tcl | tclsh 8.5 | -8 | -8 |
|
Perl | perl 5.8.8 | 2147483640 | -8 |
|
Python | python 2.5.2 | -8 | -8 |
|
MySQL | mysql 5.0.20 | 9223372036854775800 | -8 | The large number is because of the default type |
Javascript | Firefox 3.0.7 | -8 | -8 |
|
PHP | php 5.2.9-dev | -8 | -8 |
|
(c) John Coppens ON6JC/LW3HAZ | correo |