octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bitwise functions and negative numbers


From: Jordi Gutiérrez Hermoso
Subject: bitwise functions and negative numbers
Date: Thu, 17 May 2012 09:03:40 -0400

On any other language I could think of, negative numbers make sense
with bitwise operations:

C++:

    $ echo '#include <iostream>
    int main(){std::cout << (-25&2) << std::endl;}' | g++ -x c++ - \
                                                     -o foo && ./foo
    2

Perl:

    $ perl -e 'print -25 & 2; print "\n";'2
    2

Python:

    $ python -c 'print -25 & 2'
    2

Ruby:

    $ ruby -e 'print -25 & 2; print "\n";'
    2

Emacs:

    $ emacs -Q --batch --eval '(print (logand -25 2))'

    2

Bash:

    $ echo $(( -25 & 2 ))
    2

Octave:

    $ octave -q --eval 'disp(bitand(int32(-25),2))'
    2

BUT in Matlab:

    >>> bitand(int32(-2),25)
    ??? Error using ==> bitand
    Inputs must be non-negative integers.

Now, I know that technically bitwise operations with negative numbers
are implementation-defined according to the C and C++ standards. So
Matlab here is erring on the side of in my opinion somewhat extreme
caution. I don't even see why, as it's not like people compile Matlab
for obscure architectures (isn't wintel what they care about the
most?).

What should be done about this? I think this is one of those cases
where we don't need to also error out. What architecture nowadays
doesn't use two's complement for negative numbers anyways?

- Jordi G. H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]