2011-08-10 24 views
6

Estoy utilizando una tecnología llamada DDS y en el IDL, no es compatible con int. Entonces, pensé que usaría short. No necesito tantos bits. Sin embargo, cuando hago esto:Operaciones bit a bit en el corto

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

Dice "No coincide el tipo: No se puede convertir de int a corto". Cuando cambio short a long, funciona bien.

¿Es posible realizar operaciones bit a bit como esta en un short en Java?

Respuesta

1

mi entendimiento es que Java no admite valores literales cortos. Pero esto funcionó para mí:

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); 
Cuestiones relacionadas