-1

Here is the bit of code I'm working with:

byte[] encodedBytes1 = null;
byte[] encodedBytes2 = null;
try {
    Cipher c = Cipher.getInstance(encryptationMode);
    c.init(Cipher.ENCRYPT_MODE, key);
    encodedBytes1 = c.doFinal(TestText1.getBytes());
    encodedBytes2 = c.doFinal(TestText2.getBytes());
} catch (Exception e) {
    //Log.e(TAG, "AES encryption error");
}
byte[] encodedHomo = null;
boolean encoded1_2 = TestText1.getBytes() || TestText2.getBytes();

So I'm trying to do bitwise operations on encodedBytes1 and encodedBytes2 and make that new value encodedHomo (my project is homomorphic encryption on AES algorithm). Why can't I perform bitwise operation operations on type byte[]? What is, if any, the difference between the type byte and byte[]?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
John
  • 65
  • 1
  • 1
  • 10

3 Answers3

1

If you want to do a bitwise OR between two byte arrays you need to do this for each byte.

public static byte[] bitwiseOr(byte[] bytes1, byte[] bytes2) {
    if (bytes1.length < bytes2.length)
       return bitwiseOr(bytes2, bytes1);
    // bytes1 is never shorter than bytes2
    byte[] ret = bytes1.clone();
    for (int i = 0; i < bytes2.length; i++)
        ret[i] |= bytes2[i];
    return ret;
}

Note: there is no || operation for two byte or two byte[] There is only | for two byte

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
-1

Bitwise operations work on primitive data types, an array is not a primitive data type.

The bitwise OR operation, for example, has a well defined behavior for numbers but not for compound objects (which are not simply 'made of bits').

Jack
  • 131,802
  • 30
  • 241
  • 343
-1

Java ain't C. Your byte[] objects aren't pointers to lots of bits.

You have to do your bitwise ops on each byte in the byte[]s spearately.

Bohemian
  • 412,405
  • 93
  • 575
  • 722