Java Bits CS 3114 1 Data versus Information Bits are just bits (no inherent meaning) -conventions define relationship between bits and numbers -given a.

Slides:



Advertisements
Similar presentations
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Random Access Files Sandeep Patel. Random Access Files Random access files are files in which records can be accessed in any order –Also called direct.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
String Escape Sequences
Binary numbers and arithmetic. ADDITION Addition (decimal)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
Binary, Decimal and Hexadecimal Numbers Svetlin Nakov Telerik Corporation
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Introduction to Programming
Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
By Mr. Muhammad Pervez Akhtar
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Data Types and Control Structures CS3250. Java Data Types Everything is an Object Except Primitive Data Types –For efficiency –Platform independent Portable.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
C is a high level language (HLL)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
August 6, Operators. August 6, Arithmetic Operators.
Object Oriented Programming Lecture 2: BallWorld.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Programming and Data Structure
Introduction to programming in java
Yanal Alahmad Java Workshop Yanal Alahmad
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
EPSII 59:006 Spring 2004.
OBJECT ORIENTED PROGRAMMING II LECTURE 21_1 GEORGE KOUTSOGIANNAKIS
IDENTIFIERS CSC 111.
Operators August 6, 2009.
Fundamentals 2.
Bits and Bytes Topics Representing information as bits
OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS
Homework Homework Continue Reading K&R Chapter 2 Questions?
Comp Org & Assembly Lang
Bitwise Operators.
Names of variables, functions, classes
Lecture 22: Number Systems
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

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