Java Bits CS Data versus Information Bits are just bits (no inherent meaning) -conventions define relationship between bits and numbers -given a sequence of bits, it is impossible to provide an interpretation outside of a particular context Consider interpreted as an integer: interpreted as a IEEE float: x interpreted as a MIPS32 instruction: addi $29, $29, -4 -interpreted as extended ASCII*: #╜ ⁿ * non-standard
Java Bits CS Text versus Binary Files In a text file, data is represented using some sort of text-relevant encoding scheme, such as ASCII or Unicode. In a binary file, data is represented in precisely the same way it would be represented in memory. "Fred" 42 memory text memory text* *ASCII
Java Bits CS Hexdump View Viewing data in pure binary form is often not very illuminating; here’s a view of part of a binary file in hex form: A 0B 0C 0D 0E 0F C F9 00 A AA 00 6E DF CD B E5 00 C F 00 C4 00 FD 01 3A D3 01 0E F D 00 5B F A7 00 8D A 00 BD 00 DA AF EB 01 1A 00 B D E F E E E E F... 1 byte == 2 nybbles: bytes == short int in big-endian order
Java Bits CS Example of Binary File Layout The binary file shown before is logically divided into two parts. The first part is a header that specifies the number of records in the file and then the offsets of those records within the file: A 0B 0C 0D 0E 0F C F9 00 A AA 00 6E DF CD B E5 00 C F 00 C4 00 FD 01 3A D3 01 0E F D 00 5B F A7 00 8D A 00 BD 00 DA AF EB 01 1A 00 B The number of records is specified using a 1-byte integer value. The offsets of those records are specified as 2-byte integer values.
Java Bits CS Example of Binary File Layout The of the file consists of a sequence of two-part records: The number of characters is specified using a 1-byte integer value. The characters are specified using ASCII codes. # charssequence of that many chars A 0B 0C 0D 0E 0F D E F E E E E F...
Java Bits CS Example: File Navigation and Binary Input The file actually contains a quotation. Each record specifies a "word" that makes up part of the quotation, and we must create the proper quotation from the file. Logically, we need to: -get the # of records in the file -get the offset of the first record -get the record from that offset and print it -go back and get the offset of the second record -get the second record and print it -... until we've processed the specified number of records
Java Bits CS Java Solution public class FileReader { public static void main(String[] args) { try { byte numWords = 0; short wordOffset = 0; byte wordLength = 0; RandomAccessFile raf = new RandomAccessFile("1.Data.bin", "r"); // Get the number of words in the quotation: numWords = raf.readByte(); System.out.println("The number of words is: " + numWords);...
Java Bits CS Java Solution... for (int pos = 0; pos < numWords; pos++) { // Get the offset of the first word: wordOffset = raf.readShort(); // Save offset to return for next offset: long offsetOfNextOffset = raf.getFilePointer(); // Go to that position. raf.seek(wordOffset); // Get the length of the word: wordLength = raf.readByte(); // Get the word (in ASCII): byte Word[] = new byte[wordLength]; raf.read(Word); // Make Java happy: String sWord = new String(Word);...
Java Bits CS Java Solution... // Report results: System.out.println("The next word is at offset: " + wordOffset); System.out.println("The length of the next word is: " + wordLength); System.out.println("The next word is: " + sWord); // Seek back to position of next offset: raf.seek(offsetOfNextOffset); } raf.close(); } catch (FileNotFoundException e) { System.err.println("This shouldn't happen: " + e); } catch (IOException e) { System.err.println("Writing error: " + e); }
Java Bits CS Results The number of words is: 44 The next word is at offset: 297 The length of the next word is: 8 The next word is: Breathes The next word is at offset: 98 The length of the next word is: 5 The next word is: there The next word is at offset: 249 The length of the next word is: 3 The next word is: the The next word is at offset: 162 The length of the next word is: 4 The next word is: man,... The next word is at offset: 178 The length of the next word is: 2 The next word is: -- The next word is at offset: 104 The length of the next word is: 5 The next word is: Burns
Java Bits CS Bit Manipulation in Java Java provides a number of operators for manipulating operands at the bit level: &bit-wise AND |bit-wise OR ^bit-wise XOR <<shift bits to left 1 >>shift bits to right 1, preserving sign >>>shift bits to right 1, shifting in 0...
Java Bits CS Example: Printing the Bits public class PrintBits { public static void main( String args[] ) { // get input integer Scanner scanner = new Scanner( System.in ); System.out.println( "Please enter an integer:" ); int input = scanner.nextInt(); // display bit representation of an integer System.out.println( "\nThe integer in bits is:" ); // create int value with 1 in leftmost bit and 0s elsewhere int displayMask = 1 << 31;
Java Bits CS Example: Printing the Bits... // for each bit display 0 or 1 for ( int bit = 1; bit <= 32; bit++ ) { // use displayMask to isolate bit System.out.print( ( input & displayMask ) == 0 ? '0' : '1' ); input <<= 1; // shift value one position to left if ( bit % 8 == 0 ) System.out.print( ' ' ); // display space every 8 bits } // end for } // end main } // end class PrintBits mask off all but the high bit of input move next bit into high position for next masking Example adapted from Deitel JHTP7