Como usar os operadores >> e << no java

2 respostas
guisantogui

olá pessoal gostaria de sabe como funciona esses operadores, pois achei esses métodos, abaixo, e queria saber se eles estão funcionando corretamente.

Segue os metodos:
public byte[] longToBytes(long l) {
        byte[] data = new byte[8];
        for (int i=data.length-1; i >= 0; i--) {
            data[i] = (byte) (l & 0xFF);
            l >>= 8;
        }
        return data;
    }

    public long getLong(byte[] data, int offset, int length) {
        long l = 0;
        int i = 0;
        while (i < length) {
            l <<= 8;
            l += data[offset + i] & 0xFF;
            i++;
        }
        return l;
    }

   public byte[] intToByteArray(int value) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            int offset = (b.length - 1 - i) * 8;
            b[i] = (byte) ((value >>> offset) & 0xFF);
        }
        return b;
    }

   public int getInteger(byte[] data, int offset, int length) {
        int l = 0;
        int i = 0;
        while (i < length) {
            l <<= 8;
            l += data[offset + i] & 0xFF;
            i++;
        }
        return l;
    }

   public byte[] concatenaArrayBytes(byte[] b, byte[]c){
       int size = b.length+c.length;
       byte all[] = new byte[size];

       for (int i=0; i<b.length; i++){
           all[i] = b[i];
       }
       int k=0;
	  
       for (int i=b.length; i><size; i++){
           all[i] = c[k];
           k++;
       }
       return all;
   }
>

2 Respostas

Anime

Oi,

Nunca vi isso em java, em C++ sim, fiquei curiosa rsrs… :wink:

Anime

Não aguentei e pesquisei…

http://www.guj.com.br/java/2725-operadores-bit-a-bit—e-

http://www.criarweb.com/artigos/643.php

http://www.dm.ufscar.br/~waldeck/curso/java/part25.html

http://www.inf.ufsc.br/~bosco/downloads/Livro-Java-Como-Programar-Deitel-Ed6/additional/addnlApps/jhtp6_appI_BitManipulation.pdf

Espero que ajude!

Criado 18 de junho de 2011
Ultima resposta 18 de jun. de 2011
Respostas 2
Participantes 2