Download presentation
Presentation is loading. Please wait.
Published byBrendan Johns Modified over 9 years ago
1
JAVA—Bitwise
2
Compiling/Executing a Java Application Source Code Java Compiler ByteCode > Java Interpreter Machine Code >
3
Question 1 Write a program in Java to print the sum of numbers passed as command line arguments.
4
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
5
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
6
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 Hello1030 1020 Hello20is my age
7
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
8
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
9
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
10
Question 2 Write a program in java which prints all prime numbers from a list of numbers passed as command line arguments
11
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
12
>> [Right Shift] << [Left Shift] >>> [Unsigned Right Shift] Q3 Use of >>, >> operators
13
>> 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
14
<< 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
15
>>> 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
16
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); -10 -5 -10 -5
17
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); 0 0 0 0 -10 -5
18
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); } } -2560 -5120 -655360 -1310720 0 -10 -20 -2560 -5120 -655360 -1310720 -10 -20 2560 655360 5120 1310720 -42949672960 -85899345920 -327680 -655360 0 -10 -20
19
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
20
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); }
21
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 = -32768; System.out.println(--s1); }
22
Q4(b) Predict The output for(byte b=0; b< 300 ; b++) System.out.println(b);
23
(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
24
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); }
25
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”); } }
26
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
27
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
28
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
29
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
30
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
31
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.