JAVA—Bitwise. Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Java Syntax Primitive data types Operators Control statements.
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Hello, world! Dissect HelloWorld.java Compile it Run it.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
JAVA (something like C). Object Oriented Programming Process orientated – code acting on data Object oriented – data controls access to code Encapsulation.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 3, page 1 Sun Certified Java 1.4 Programmer Chapter 3 Notes Gary Lance
August 6, 2009 Data Types, Variables, and Arrays.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
3: Controlling Program Flow Using Java operators Mathematical operators Relational operators Logical operators –Primitive type: ALL (the same with C) –String:
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 2 Variables.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1.2 Built-in Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.
Programming in java Lecture-3 (a). Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64.
Reference Learning Java DDC publishing Herst, Yamauchi, Adler COP 3331 Object Oriented Analysis and Design Java Part I.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Java Part I By Wen Fei, HAO. Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Object Oriented Programming Lecture 2: BallWorld.
Java Programming Language Lecture27- An Introduction.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
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.
Chapter 2 Variables.
Chapter 2 Basic Computation
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Sachin Malhotra Saurabh Choudhary
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 2 Basic Computation
An Introduction to Java – Part I
مفاهیم اولیه زبان جاوا Java Basic Concepts
CS Week 6 Jim Williams, PhD.
Escape Sequences What if we wanted to print the quote character?
An Introduction to Java – Part I, language basics
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
OOP With Java/ course1 Sundus Abid-Almuttalib
Java Programming with BlueJ Objectives
Names of variables, functions, classes
Chapter 2 Variables.
Agenda Types and identifiers Practice Assignment Keywords in Java
Introduction to java Part I By Shenglan Zhang.
Presentation transcript:

JAVA—Bitwise

Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >

Question 1 Write a program in Java to print the sum of numbers passed as command line arguments.

class commandSum { public static void main(String x[ ]) { double sum =0; for(int i=0; i<x.length;i++) sum = sum + Double.parseDouble(x[i]); System.out.println("Sum is"+sum); } // End of main } // End of CommandSum

System.out.println() Prints/Displays output and shifts the print control to new line (Similar printf(“\n”) in C) Displays output only in String form If parameter to it is not in String form then it will be converted to string form by internally calling toString() + operator can be used to concatenate data from different types

Examples System.out.println(“Hello”+10); System.out.println(10+20); System.out.println(“10”+20); System.out.println(“Hello: ”+20+”is my age”); Note : + opeartor is used for dual purpose addition,concatenation Hello Hello20is my age

System.out.print() Prints/Displays output starting from the same line (Similar printf() in C) Displays output only in String form If parameter to it is not in String form then it will be converted to string form by internally calling toString() + operator can be used to concatenate data from different types

Examples class test104 { public static void main(String args[]) { System.out.print("Hello"); System.out.print("I am fine"); System.out.println(" It is OK"); } D:\java\bin>java test104 HelloI am fine It is OK

Example 2 class test105 { public static void main(String args[]) { System.out.print("Hello"); System.out.print("I am fine"); System.out.println(" It is OK"); System.out.println(" It is OK Again"); } D:\java\bin>java test105 HelloI am fine It is OK It is OK Again

Question 2 Write a program in java which prints all prime numbers from a list of numbers passed as command line arguments

class commandPrime { public static void main(String x[ ]) { for(int i=0; i<x.length;i++) { int number = Integer.parseInt(x[i]); boolean flag = true; for (int j=2; j< number/2 ;j++) { if( number % j ==0) { flag = false; break; } } // End of inner for if(flag) System.out.println(" "+number); } // End of outer for } // End of main } // End of CommandPrime

>> [Right Shift] << [Left Shift] >>> [Unsigned Right Shift] Q3 Use of >>, >> operators

>> Signed Right Shift >> Signed Right Shift operator shifts the operand right by preserving the sign of the number Usage : x >> k ; If x is byte,short or int type then bits of x is shifted k % 32 times. If x is long then bits of x is shifted k % 64 times. Note : Shifting x right each bit will divide the x by 2 Sb0b2 bn-1 ……… 012n-1 Sb1b2b3S ……… Discarded

<< Left Shift << Left Shift operator shifts the operand left (bit positions) Shifting is done into the sign bit. Earlier sign bit is lost Usage : x << k ; [ Each Left shift multiplies x by 2] If x is byte,short or int type then bits of x is shifted k % 32 times. If x is long then bits of x is shifted k % 64 times. Sb0b1b2bn-1 ……… 012n-1 bn-10b0b1bn-21 ……… Discarded

>>> Unsigned Right Shift For Positive Numbers Both >> and >>> work same Unsigned right shift operator shifts the operand right by inserting 0 in the sign bit. Sb0b1b2bn-1 ……… 012n-1 0b1b2b3S Discarded

class rttest { public static void main (String args[ ]) { byte b = -10; System.out.println(b>>8); System.out.println(b>>9); System.out.println(b>>16); System.out.println(b>>17); System.out.println(b>>31); System.out.println(b>>32); System.out.println(b>>33); short s = -10; System.out.println(s>>8); System.out.println(s>>9); System.out.println(s>>16); System.out.println(s>>17); System.out.println(s>>32); System.out.println(s>>33);

b = 10; s = 10; System.out.println(b>>8); System.out.println(s>>16); System.out.println(b>>9); System.out.println(s>>17); long l = -10; System.out.println(l>>32); System.out.println(l>>33); System.out.println(l>>15); System.out.println(l>>16); System.out.println(l>>63); System.out.println(l>>64); System.out.println(l>>65);

class lttest { public static void main(String args[ ]) { byte b = -10; System.out.println(b<<8); System.out.println(b<<9); System.out.println(b<<16); System.out.println(b<<17); System.out.println(b<<31); System.out.println(b<<32); System.out.println(b<<33); short s = -10; System.out.println(s<<8); System.out.println(s<<9); System.out.println(s<<16); System.out.println(s<<17); System.out.println(s<<32); System.out.println(s<<33); b = 10; s = 10; System.out.println(b<<8); System.out.println(s<<16); System.out.println(b<<9); System.out.println(s<<17); long l = -10; System.out.println(l<<32); System.out.println(l<<33); System.out.println(l<<15); System.out.println(l<<16); System.out.println(l<<63); System.out.println(l<<64); System.out.println(l<<65); } }

class leftRight { public static void main(String args[]) { int x =40; System.out.println(x>>2); System.out.println(x<<2); System.out.println(x>>>2); int x1 =-40; System.out.println(x1>>2); System.out.println(x1<<2); System.out.println(x1>>>2); int x2 =-256; System.out.println(x2>>32); System.out.println(x2<<32); System.out.println(x2>>>32); int x3 =256; System.out.println(x3>>32); System.out.println(x3<<32); System.out.println(x3>>>32); Predict The Output for this code

int x4 = 32; System.out.println(x4>>7); System.out.println(x4<<7); System.out.println(x4>>>7); int x5 = 1024; System.out.println(x5>>31); System.out.println(x5<<31); System.out.println(x5>>>31); int x6 = 1024; System.out.println(x6>>33); System.out.println(x6<<33); System.out.println(x6>>>33); int x7 = -1024; System.out.println(x7>>33); System.out.println(x7<<33); System.out.println(x7>>>33); }

Q4(a) Predict Outputs class typetest { public static void main(String args[]) { byte b = 127; System.out.println(++b); byte b1 = -128; System.out.println(--b1); short s = 32767; System.out.println(++s); short s1 = ; System.out.println(--s1); }

Q4(b) Predict The output for(byte b=0; b< 300 ; b++) System.out.println(b);

(i) byte b =0 (ii) byte b = 200; (iii) boolean b = 1; (iv) int a = 10.56; (v) float f = 12.67; (vi) short s = 23; (vii) short s1 = 33000; Q5(a) Find the invalid assignments from the following

Q 5(b) Predict the output of the code class typecasting { public static void main(String args[]) { int a = 400; byte b = (byte) a; System.out.println(b); int a1 = (int) 10.56; System.out.println(a1); char x = (char) a; System.out.println(x); float f = (float)10.65; System.out.println(f); }

Q6 Consider the following program and predict output for following command line executions (i) java test (ii) java A(iii) java B // test.java class A { public static void main(String args[ ]) { System.out.println(“Hello This is class A”); } } class B { public static void main(String args[ ]) { System.out.println(“Hello This is class B”); } }

What’s output for the following byte b1 = 10; byte b2 = 5; byte b3 = b2 * b1; System.out.println("b3="+b3); short s1 = 10; short s2 = 20; short s3; s3 = s1 * s2; System.out.println("s3="+s3); float f = 10.00f; int a =10; if( a == f) System.out.println("Hello"); else System.out.println("Hi"); possible loss of precision found : int required: byte byte b3 = b2 * b1; possible loss of precision found : int required: short short s3 = s1 * s2; Hello

float a1 = 4.57f; double b1= 4.57; if( a1 == b1) System.out.println("Hello"); else System.out.println("Hi"); float a2 = 4.5f; double b2= 4.5; if( a2 == b2) System.out.println("Hello"); else System.out.println("Hi"); Hi Hello

Instance Fields vs Local Variables Local Variable is any variable which is declared inside the method Local variable has to be initialized before use Instance Fields belongs to objects and are allocated space when object is created Instance fields are by default initialized to some initial values depending upon their type

Example (Local Variable) class lttest { public static void main(String args[ ]) { int x; int y = x + 10; System.out.println("y="+y); } E:\Java Programs> javac lttest.java lttest.java:6: variable x might not have been initialized int y = x + 10; ^ 1 error

Example (Instance Fields) class X{ } class Y{ } class Z{ } class XYZ { private int x; private float y; private double z; private char a; private boolean b; private byte by; private short sh; private long lo; privateXx1; privateYy1; privateZz1; void show() { System.out.println("x="+x); System.out.println("y="+y); System.out.println("z="+z); System.out.println("a="+a); System.out.println("b="+b); System.out.println("by="+by); System.out.println("sh="+sh); System.out.println("lo="+lo); System.out.println("x1="+x1); System.out.println("y1="+y1); System.out.println("z1="+z1); }// End of show() }// End of class XYZ // test.java

class test { public static void main(String[ ] x) { XYZ x1 = new XYZ(); x1.show(); } E:\Java Programs>java test x=0 y=0.0 z=0.0 a= b=false by=0 sh=0 lo=0 x1=null y1=null z1=null