• # C'est décrit dans la spec de Java

    Posté par . En réponse au journal Un décalage de 64 bits, ça vous inspire comment ?. Évalué à 4.

    Je reprend ton exemple :

    public class Shift {
     public static void main(String[] args) {
     int a = 1;
     System.out.println("Resultat : " + (a >> 64));
     }
    }

    La spécification explique :

    If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive.

    Dans notre cas, vu que 64 (0x40) & 0x1f est égal à 0, on fait donc 1 >> 0, et donc cela vaut bien 1. C'est la même chose pour les long avec un masque plus gros d'un bit.

    En gros, Java autorise n'importe quelle distance, mais si c'est plus gros que la taille de l'opérande de gauche, il fait un modulo pour avoir une valeur utilisable.