Search code examples
javaio

How can I look for patterns in the first 3 bits of an int which is assigned from a FileInputStream.read() which returns an int between 0:255 or -1?


I am reading bytes as follows:

        try(FileInputStream f = new FileInputStream("nameOfFile")){
            int readByte = 0;
            int mask =0b111;
            while((readByte=f.read())!=-1) {
                System.out.println(Integer.toBinaryString(readByte & mask));
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

However, when i try to obtain the 8 bits that forms the number, sometimes I get 7 bits or even more bits than 8. Can anyone help me with this problem? I want to get 8 bits so after that i can look at first 3 bits for example. I need that for returning the charset in this method.

I'll be very gratefull if you help me. I am looking forward to hearing from you soon.

Best regards, WaterKnight


Solution

  • A byte is an 8-bit two's-complement signed number.

    If you think a byte has values 0..255, then you are wrong. The 8 bits are -128..127, because bit 7 is the sign bit.

    When you do bit-manipulation using operators like >>>, the byte is first widened to an int, which means that sign-extension is applied.

    So, unsigned value 199 (example), is bits 11000111, which as a byte is value -57 aka 0xC7, and when sign-extended to an int becomes -57 aka 0xFFFFFFC7.

    When you right-shift that by 5 (>>> 5), you get 0x07FFFFFE aka 134217726.

    Since you're only interested in the lowest 3 bits of that value, you need to mask it: (val >>> 5) & 0x07, which will give you what you're looking for: 0x06 aka 0b110