2011-08-10 4 views
6

J'utilise une technologie appelée DDS et dans l'IDL, elle ne prend pas en charge int. Donc, je me suis dit que je voudrais juste utiliser short. Je n'ai pas besoin de tant de bits. Cependant, quand je fais ceci:Opérations bit à bit sur court

short bit = 0; 
System.out.println(bit); 
bit = bit | 0x00000001; 
System.out.println(bit); 
bit = bit & ~0x00000001; 
bit = bit | 0x00000002; 
System.out.println(bit); 

Il dit "incompatibilité de type: Impossible de convertir de int à court". Quand je change short en long, cela fonctionne très bien.

Est-il possible d'effectuer des opérations au niveau du bit comme ceci sur un short en Java?

Répondre

1

Ma compréhension est que Java ne supporte pas les valeurs littérales courtes. Mais cela a fonctionné pour moi:

short bit = 0; 
short one = 1; 
short two = 2; 
short other = (short)~one; 
System.out.println(bit); 
bit |= one; 
System.out.println(bit); 
bit &= other; 
bit |= two; 
System.out.println(bit);