Vamos mover algums bits de umas letras, e depois retorná-las ao lugar:
Primeiro vamos fazer com a letra 'C'.Color pixel1 = Color.black;
int r1 = pixel1.getRed() & 0xFC; //Limpa os dois ultimos bit. FC=1111 1100
int g1 = pixel1.getGreen() & 0xFE; //Limpa o ultimo bit. FE=1111 1110
int b1 = pixel1.getBlue() & 0xFE;
Color pixel2 = Color.blue;
int r2 = pixel2.getRed() & 0xFC;
int g2 = pixel2.getGreen() & 0xFE;
int b2 = pixel2.getBlue() & 0xFE;
int bits = 'C';
bits = bits & 3;
int novoRED1 = r1 | bits;
bits = 'C';
bits = bits >>2;
bits = bits & 1;
int novoGREEN1 = g1 | bits;
bits = 'C';
bits = bits >>3;
bits = bits & 1;
int novoBLUE1 = b1 | bits;
/////////////////////
bits = 'C';
bits = bits >>5;
bits = bits & 3;
int novoRED2 = r2|bits;
bits = 'C';
bits = bits >>6;
bits = bits & 1;
int novoGREEN2 = g2|bits;
bits = 'C';
bits = bits >>7;
bits = bits & 1;
int novoBLUE2 = b2 |bits;
//PRESTEM ATENÇÃO AKI! ! ! ! ! !
int byteValueAntes = 'C';
System.out.println("ByTE ANTES: " + byteValueAntes);
int byteValue= ((novoRED1 & 3)) | ((novoGREEN1 & 1) << 2) | ((novoBLUE1 & 1) <<3 | ((novoRED2 & 3) << 5) | ((novoGREEN2 & 1) << 6) | ((novoBLUE2 & 1) << 7));
System.out.println("ByTE DEPOIS: " + byteValue);
Opa! Deu certo..
Dá certo com todas as letras de A até J (a,b,c,d,e,f,g,h,i,j)
Mas de P para frente dá erro..
Substituam todos os lugares que tá 'C' por 'P' e verifiquem a nova saída:
Saída:
ByTE ANTES: 80 (na tabela Ascii, 80 corresponde a letra P)
ByTE DEPOIS: 64 (na tabela Ascii, 64 corresponde ao '@')
Porque isso ocorre???