bitwise-xor inconsistency with other schemes

Issue #226 resolved
Former user created an issue

Something changes in the operation of bitwise-xor from (rnrs arithmetic bitwise) as numbers get larger:

[using Sagittarius scheme shell, version 0.8.3 (x86_64-pc-linux)]

sash[r7rs]> (import (rnrs arithmetic bitwise))
#<unspecified>
sash[r7rs]> (bitwise-xor (expt -2 90) (- (expt -2 90) 1))    ; this looks fine
2475880078570760549798248447
sash[r7rs]> (bitwise-xor (expt -2 91) (- (expt -2 91) 1))    ; returns a negative number
-340282366915986703306233086332171714561

In other (R7 and R6) implementations I have tried, the return value is always a positive number:

> (bitwise-xor (expt -2 90) (- (expt -2 90) 1))
2475880078570760549798248447
> (bitwise-xor (expt -2 91) (- (expt -2 91) 1))     ; returns a positive number
4951760157141521099596496895

The R6RS definition of bitwise-xor states "exclusive or of the two's complement representations of their arguments." I think the return value for two negative numbers should be positive.

Comments (4)

  1. Takashi Kato repo owner

    Thank you for the report!

    It seems this happens when (expt -2 n) where n is odd and big enough to make the result bignum (e.g. 29 on 32 bit environment, 61 for 64 bit environment).

    Of course n has to be odd, otherwise it'd be positive. Idiot...

    So, the following is rather invalid example... e.g. `

    (bitwise-xor (expt -2 29) (- (expt -2 29) 1)) ; => negative number
    (bitwise-xor (expt -2 30) (- (expt -2 30) 1)) ; => correct value
    

    It has to be something stupid mistake on exclusive or implementation.

  2. Log in to comment