1

I'm trying to find a good elegant way to bubble sort a 24 bits input. it seems I have an issue with my code but I can't figure out the problem. I'm new to Java, so please be compassionate. thanks!

public static void sortTriBytes(RandomAccessFile file) throws IOException {
    for (long i = 1; i < file.length()/3; i=i+3){
        for (long j = 0; j < file.length()-(3*(i-1)); j=j+3){
            String leftStr = "";
            String rightStr = "";
            file.seek(j);
            int left1 = file.readUnsignedByte();
            int left2 = file.readUnsignedByte();
            int left3 = file.readUnsignedByte();
            leftStr+= Integer.toBinaryString(left1);
                    leftStr+=Integer.toBinaryString(left2);
                        leftStr+=Integer.toBinaryString(left3);
            int actualleft = Integer.parseInt(leftStr,2);

            int right1 = file.readUnsignedByte();
            int right2 = file.readUnsignedByte();
            int right3 = file.readUnsignedByte();
            rightStr+= Integer.toBinaryString(right1);
            rightStr+= Integer.toBinaryString(right2);
            rightStr+= Integer.toBinaryString(right3);
            int actualright = Integer.parseInt(rightStr,2);
        if (actualleft > actualright) {
            file.seek(j);
            file.write(right1);
            file.write(right2);
            file.write(right3);
            file.write(left1);
            file.write(left2);
            file.write(left3);
            }

        }
    }
}

thanks again!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
yuval
  • 13
  • 3
  • 1
    I'm sorry, but *"it seems I have an issue with my code"* is not very specific. Does it compile? Does it run? Does it throw an error? Does it produce incorrect results? Please clarify your question, because at this point, anyone trying to help you needs to open their IDE, create a class around your code, try to create a binary file with 24 bit values that somehow matches your input, guess what you're actually trying to achieve, and then start debugging to figure out why you don't get the desired result. – Robby Cornelissen Dec 24 '19 at 15:36
  • Hi Robby, thanks for the quick reply, Yes it runs but doest give me the right result. I tried to debug it for 3.5 hours... and yes, I gave it an input I created myself. this question comes after a serious headache. – yuval Dec 24 '19 at 15:49

1 Answers1

0

There are no leading zeros in a binary string, so your concatenated version will be skewed by varying lengths. For example, "1" + "101" + "1" might look larger than "110" + "1" + "0".

You can neatly pack bytes into a larger word like an int or long using a bit-shift operator (<<) and avoid using strings altogether.

int word = (byte1 << 16) | (byte2 << 8) | byte3;

You can also use math to perform the same operation on unsigned bytes, but bit-shifting does a better job of communicating intent.

int word = byte1 * 256 * 256 + byte2 * 256 + byte3;

phatfingers
  • 9,770
  • 3
  • 30
  • 44