19-SE-49 NEHA ASIF Lab4oop

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

19-SE-49

NEHA ASIF
OOP LAB # 04
Ma’am Sidra Shafi
1. Write a Java Program to find the result of the following Expressions. (Assume
a=10 and b=5).
i) (a<<2)+(b>>2)
ii) (b>0)
iii) (a+b*100)/10
iv) a&b

CODE:
public class taskno1 {
public static void main(String arg[])
{
int a= 10;
int b=5;
/////////////////Part a////////////////////;
int first , secound;
first=a<<2;
secound=b>>2;
int ans=first+secound;
System.out.println("(a<<2)+(b>>2) =" + ans);
/////////////////Part b/////////////////////;
System.out.println("(b>0) = " + (b>0));
/////////////////Part c/////////////////////;
System.out.println("(a+b*100)/10 = " + (a+b*100)/10);
////////////////part d//////////////////////;
int AND= a&b;
System.out.println("a&b = "+ AND);

OUTPUT:
VERIFICATION:
PART(a):
In this part we have to find the right_shift and left_shift of a and b then add
them.
As a= 10 and b=5
In binary form
a =00001010
b=00000101
for a<<2 we have to shift the bits of a towards left by 2
a<<2 = 0 0 1 0 1 0 0 0
for b>>we have to shift the bits of a towards right by 2
b>>2 = 0 0 0 0 0 0 0 1.
Finally, we add a<<2 and b>>2
a<<2 = 0 0 1 0 1 0 0 0
b>>2 = 0 0 0 0 0 0 0 1
(a<<2)+(b>>2) = 0 0 1 0 1 0 0 1
Now we convert (a<<2)+(b>>2) = 0 0 1 0 1 0 0 1 into decimal form to verify
the
final result.
0 + 0 + 32 + 0 + 8 + 0 + 0 + 1 = 41
PART(b):
It is given that b=5 which is greater than 0, so it gives the result as “True”.
Part(c):
First we multiply b with 100(b*100). As b=5 so, 5*100=500. Then
We add it with a(a+500=510). then we divide 510 by 10 (510/10) and the
answer will be 51.
Part(d):
First convert a and b into binary
a=00001010
b=00000101
now a&b which will give
a&b = 0 0 0 0 0 0 0 0
hence the answer is “0”
2. Find the result of following expressions. Assume a=5 and b=4.
i. a & (-a);
ii. (a++ != b++) && (a++ ==b++);
iii. (--a != --b) | (--a == --b);
iv. (a < 0 ? -a : a);
CODE:
public class taskno1 {
public static void main(String srg[])
{
int a=5;
int b=4;
//Part a;

int x= a&(-a);
System.out.println("a&(-a) = " + x);
//Part b;

System.out.println("(a++ != b++) && (a++ ==b++) = "+ ((a++ != b++) && (a++
==b++)));
//Part c;

System.out.println("(--a != --b) | (--a == --b) = "+ ((--a != --b) | (--a == --b)));


//Part d;

System.out.println("(a < 0 ? -a : a) = "+ ((a < 0 ? -a : a)));


}}
OUTPUT:
Verification:
Part a:
First convert a and –a into binary
a= 5= 0 0 0 0 0 1 0 1.
–a =-5 =1 1 1 1 1 0 1 1.
Now find a&(-a)
00000101
1 1 1 1 1 0 1 1.
a&(-a)= 0 0 0 0 0 0 0 1
Part b:
The first bracket an increment and b increment are compared and they are
not equal which verify the given statement and we get TRUE .in this
increment a is 6 and b is 5. In 2nd bracket increment is done in a way that
a is equal to 7 now and b is 6. this bracket will give answer as false. Finally,
we perform && between true and false and the final answer is false.
Part c:
In the first bracket first we perform pre decrement on both a and b so a
becomes
6 and b becomes 5. Then we compare a and b in such a way that a not
equals to
b which is true. In the 2nd bracket first we perform pre decrement operation
on both a and b so, a becomes 5 and b becomes 4 and the saying a equals
to b which is false. OR | operator has its one operand true on left side and
one operand false on the right side. So the final result is true.
Part d:
It is a Conditional statement with condition that a<0. If true
it will assign a to -a otherwise it will assign a to a. here, a=5 which is
greater than 0 so this operator assigns a to a
3.Convert a negative integer -37 into a binary form. (Do it on
paper).

You might also like