-1

I heard this is a question asked in an interview, given two bytes, return true if they are symmetric

public boolean isSym(Byte firstByte, Byte secondByte);

01101000 and 00010110 are symmetric, but 01100000 and 11000000 are not. need to write the code in Java. Any ideas what is the best way to do so?

Marko Gresak
  • 7,950
  • 5
  • 40
  • 46
Dan The Man
  • 1,835
  • 6
  • 30
  • 50

1 Answers1

2
public boolean isSym(Byte firstByte, Byte secondByte)
{

    for (int i = 0; i< 8 ; i++){
        if (bitAt(firstByte, i) != bitAt(secondByte, 7 - i))
            return false;
    }

    return true;
}

public byte bitAt(byte num, int position)
{
   return (byte)((num >> position) & (byte)1);
}
brianxautumn
  • 1,162
  • 8
  • 21