Deslocamento de Bits [ resorvido ;) ]

Vamos mover algums bits de umas letras, e depois retorná-las ao lugar:

Primeiro vamos fazer com a letra ‘C’.

[code] 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);
[/code]
[color=red]Saída:[/color]
ByTE ANTES: 67
ByTE DEPOIS: 67
(na tabela Ascii, 67 corresponde a letra C - [color=blue]int bits = ‘C’[/color])

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 ‘@’)

Tabela Ascii

Porque isso ocorre???

Não rodei seu exemplo, mas se trocar “>>” por “>>>” o que ocorre?

Não é isso que eu qro fazer…
Eu apenas estou deslocando bits para a direita para poder fazer o “ou”, e depois eu faço o inverso…
Só que para as letras a partir de P não dá certo…

Alguém ae sabe me explicar??

Para a direita há perda de dados. Exemplo:
Seja uma variável de 8 bits e com o valor 00010000.
Se o valor 00010000 for deslocado 3 bits à direita ele torna-se 10000000. Com 4 torna-se 00000000, pois o tipo não aceita o valor 100000000.

Att.

Entendi…
Então o problema está na hora de voltar?

int byteValue= ((novoRED1 & 3)) | ((novoGREEN1 & 1) << 2) | ((novoBLUE1 & 1) <<3 | ((novoRED2 & 3) << 5) | ((novoGREEN2 & 1) << 6) | ((novoBLUE2 & 1) << 7));  

Como poderia fazer então?

[quote=pedroroxd]

int byteValue= ((novoRED1 & 3)) | ((novoGREEN1 & 1) << 2) | ((novoBLUE1 & 1) <<3 | ((novoRED2 & 3) << 5) | ((novoGREEN2 & 1) << 6) | ((novoBLUE2 & 1) << 7));  

Como poderia fazer então?[/quote]
Não sei se atende ao seu objetivo na linha, mas talvez mudar a lógica dos operadores funcione:

int byteValue= ((novoRED1 & 3)) | (novoGREEN1 & (1 << 2)) | (novoBLUE1 & (1 <<3)) | (novoRED2 & (3 << 5)) | (novoGREEN2 & (1 << 6)) | (novoBLUE2 & (1 << 7));  

Att.

Isso não resolve…

Bom, o que está acontecendo…
Olha as entradas e as saídas:

Entrada:
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Saída:
ABCDEFGHIJKLMNO@ABCDEFGHIJ

O “Q” é igual o “A”, o R é igual o B, e assim por diante…

Ele está igualando o
Q no A: “01010001” no “01000001”
R no B: “01010010” no “01000010”
S no C: “01010011” no “01000011”
Está ocorrendo a perda desses 2 bits do meio…
Estou fazendo alguma coisa errada, e nao sei onde =[

Acho que o problema é aquele 3, porque em alguns casos pode se que novoRED1 e novoGREEN1 sejam alterados nos mesmos bits. Tenta ver se a forma a seguir funciona:

int byteValue= ((novoRED1 & 1)) | (novoGREEN1 & (1 << 2)) | (novoBLUE1 & (1 <<3)) | (novoRED2 & (1 << 5)) | (novoGREEN2 & (1 << 6)) | (novoBLUE2 & (1 << 7));

Ou da outra que já estava sendo usando, mas com o 1 no lugar do 3:

int byteValue= ((novoRED1 & 1)) | ((novoGREEN1 & 1) << 2) | ((novoBLUE1 & 1) <<3 | ((novoRED2 & 1) << 5) | ((novoGREEN2 & 1) << 6) | ((novoBLUE2 & 1) << 7));  

Outra coisa é que o deslocamento com 4 (… << 4) não está sendo feito em nenhum momento. Deveria?

Att.

Deu certo assim:

int byteValue= ((R & 3)) | ((G & 1) << 2) | ((B & 1) <<3 | ((R2 & 3) << 4) | ((G2 & 1) << 6) | ((B2 & 1) << 7));
Vlws ae pela atenção =)

R = novoRED1
G = novoGREEN1
B = novoBLUE1

R2 = novoRED2
G2 = novoGREEN2
B2 = novoBLUE2