• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Shift Operator Gurus !!

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have included some shift operator questions from JQ Plus which
I have troubles with. I know this should be a "breeze through" for the Gurus on this issue, but these questions have given me some headaches. Can anybody please make some comments on how the answers were derived
Question ID :952739435490
What would be printed during execution of the following program?
public class TestClass
{
public static void main(String args[ ] )
{
shiftTest(1, "A" ) ;
shiftTest(1<<31, "B") ;<br /> shiftTest(1<<30, "C") ;<br /> shiftTest(-1, "D" ) ;<br /> }<br /> public static void shiftTest(int i , String str) <br /> {<br /> if (( i >> 1) != (i >>> 1)) System.out.println(str) ;
}
}
***************************************************************
b, d Can you please explain the concept
***************************************************************
Question ID :955935425650
Consider :
class A {}
class B extends A {}
class C extends B {}
Which of these boolean expressions correctly identifies when an object 'o' acutally refers to an object of class B and not A or C?
***************************************************************
(o instanceof b) && (!(o instanceof a))
(o instanceof B) && (!(o instanceof c)) //correct
!(!(o instanceof B) | | (o instanceof c)) // correct
(o instanceof B)&&!((o instanceof A)| |(o instanceof c))
***************************************************************

Question ID :954698538220
Which statments about the output of the following programs are true?
public class TestClass
{
public static void main(String args[ ] )
{
int i = 0 ;
boolean bool1 = true;
boolean bool2 = false;
boolean bool = false;
bool = (bool2 & method1("1")); //1
bool = (bool2 && method1("2")); //2
bool = (bool1 | method1("3")); //3
bool = (bool1 | | method1("4")); //4
}
public static boolean method1(String str)
{
System.out.println(str);
return true;
}
}
*************************************************
1 and 3 will be part of the output?
Question:
*************************************************
Question ID :952739436700
What will be the outpout of the following program?
public class TestClass
{
public static void main(String args[ ] )
{
int i = 0 ;
boolean bool1 = true ;
boolean bool2 = false;
boolean bool = false;
bool = ( bool2 & method1(i++) ); //1
bool = ( bool2 && method1(i++) ); //2
bool = ( bool1 | method1(i++) ); //3
bool = ( bool1 | | method1(i++) ); //4
System.out.println(i);
}
public static boolean method1(int i)
{
return i>0 ? true : false;
}
}
*************************************************
it will print 2
*************************************************
Question ID :961883785420
What would be printed during execution of the following program?
public class TestClass
{
public static void main(String args[])
{
System.out.print(" "+(1<<33)+" "+(1<<32)+" "+ Integer.toHexString(1<<31) );
}
}
*************************************************
what will this output?
*************************************************

 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think most of the questions are explained pretty well in Khalid's books.Also, I had discussed about bit shifting in a recent post posted by Payal.
If you still face any problems, please do let us know.
- Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Left Shift ( << ) --<br /> The << moves all of the bits in the specified value to the left by the number of bit positions specified . also note that for each shift left , the high order bit is shifted out (i.e. lost) & a zero is brought in on the right .<br /> Here's how 1 << 3 will be evaluated<br /> 00000000 00000000 00000000 00000001 --- 1<br /> Shift left 3 times & you get<br /> 00000000 00000000 00000000 00001000 ---- 8<br /> <br /> Right Shift --<br /> The >> shifts all of the bits in a value to the right a specified number of times .<br /> Try 8 >> 3<br /> 00000000 00000000 00000000 00001000 ----> 8<br /> Shift right 3 times<br /> 00000000 00000000 00000000 00000001 ---> 1
The tricky part when you are shifthing right ( >> ) is that the leftmost(top) bits exposed by the right shift are filled in with the previous contents of the top bit & not just zero's .
Try -8 >> 2
11111111 11111111 11111111 11111000 ----> -8
Shift right 2 times & you get
11111111 11111111 11111111 11111110 ---> -2
Another interesting thing is that no matter how much you shift -1 right the result will always remain zero .
11111111 11111111 11111111 11111111 ---> -1

Unsigned Right Shift -
>>> is like >> except that it always shifts zero's into the high order bit i.e. to say that the leftmost bits exposed by the right shift are filled in with zero's .
-8 >>> 2
11111111 11111111 11111111 11111000 ----> -8
00111111 11111111 11111111 11111110 ----> 1073741822
Extend this whole thinghy to your first code & you'll get the answer & here's something i do to get me the binary equivalents
System.out.println(Integer.toBinaryString(1<<3));
Experiment with this for a while & you'll get a hang of the concept .
Ashish
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your 2nd code
Let's assume that o is of type B & the heirarchy is "C extends B" & "B extends A"
And we'll figure this one out first
(o instanceof b) && (!(o instanceof a))
(o instanceof b) = true
Quote from JLS
The && operator is like & , but evaluates its right-hand operand only if the value of its left-hand operand is true. It is syntactically left-associative (it groups left-to-right) .( JLS 15.23)
o is an instance of B & B extends A so o is an instance of A aswell .
Hence
(o instanceof a) = true &
(!(o instanceof a)) = false
So
(o instanceof b) && (!(o instanceof a)) = false
If o is of type A then ----
(o instanceof b) = false & the right side is never evaluated & the net result is
(o instanceof b) && (!(o instanceof a)) = false
If o is of type C then ---
(o instanceof b) = true
So we evaluate the right hand side
(o instanceof a) = true
(!(o instanceof a)) = false
Net result
(o instanceof b) && (!(o instanceof a)) = false
What we are looking for is that the expression return a definitive true or false when o is of type B which it doesn't do . So not correct .
(o instanceof b) && (!(o instanceof a)) = false //when o of type B/A/C

----------------------------------------
Now the next line
(o instanceof B) && (!(o instanceof c))//presumably correct
Let's say that o is of type B then ----
(o instanceof B) = true
Since true the right hand side is evaluated
(o instanceof c) = false
(!(o instanceof c)) = true
(o instanceof B) && (!(o instanceof c)) = true
If o is of type C -----
(o instanceof B) = true
Since true the right hand side is evaluated
(o instanceof c) = true
(!(o instanceof c)) = false
Net result
(o instanceof B) && (!(o instanceof c)) = false
If o is of type A ----
(o instanceof b) = false
& now the right hand side of the expression is never evaluated so ,
(o instanceof B) && (!(o instanceof c)) = false
With this expression you are sure to get a true whenever o is an object of type B . And so i'd say that this is correct .
(o instanceof B) && (!(o instanceof c)) = true //when o is type B
(o instanceof B) && (!(o instanceof c)) = false //when o is of type C
(o instanceof B) && (!(o instanceof c)) = false //when o is of type A
Correct me if i'm wrong .
Well , with this cryptic explanantion i'm sure you'll decipher the rest .
If i don't get something like this , here's what i do
-See what the operands do
-Stare at the code for an hour or so
-And then i get the picture . Always works for me .
The other codes are also on the same lines . Try to work on them yourself .
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the last code toHexString() creates a string representation of the integer argument as an unsigned integer in base 16 . You can always convert a binary form to a hexadecimal form easily . Here's how you do it -
10000000 00000000 00000000 00000000 ---> -2147483648
To convert a binary number to it's hexadecimal equivalent , make groups of four bits starting from the least significant bit(LSB) i.e. the rightmost bit moving towards the most significant bit(MSB) i.e. the leftmost bit . So we do that
1000 0000 0000 0000 0000 0000 0000 0000
And now we replace each group of four bits by it's hexadecimal representation .
80000000
Just in case you don't have binary & decimal equivalents of hexadecimal numbers , let me know , i'll post it for you . I'm sure you can find it on the net aswell .
[This message has been edited by Ashish Hareet (edited July 28, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mo,
In future would you please limit your questions to one per post? Makes them a little easier to deal with
Thanks
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure that I completely understand this comment:

Another interesting thing is that no matter how much you shift -1 right the result will always remain zero .
11111111 11111111 11111111 11111111 ---> -1
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kris ,
I'll try to exlain it again
-8 >> 2
11111111 11111111 11111111 11111000 ----> -8
Shift right 2 times & you get
11111111 11111111 11111111 11111110 ---> -2
As you notice the void left behind by shifthing right is filled in by the top bits(leftmost bits) & not zero's .
Keepin this in mind try
-1 >> 2
11111111 11111111 11111111 11111111 ---> -1
Now shift right 2 times keeping in mind that the void is filled by 1's(since 1 is the top bit) .
You see that no matter how much you right shift ( >> ) -1 the result will always be -1.
Note that I'm talkin bout right shift( >> ) & not the unsigned right shift( >>> ) . The unsigned right shift fills the void with zero no matter what the top bits were .
Ashish
 
Kris Decker
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it! Thanks soooo much! Bit shifting has been a nuisance for me...
Kris
 
reply
    Bookmark Topic Watch Topic
  • New Topic