• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shift Operator Dought

 
Dhanesh Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends

class EBH019 {
public static void main (String args[]) {
int i1 = 0xffffffff, i2 = i1 << 1;
int i3 = i1 >> 1, i4 = i1 >>> 1;
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3) + ",");
System.out.print(Integer.toHexString(i4));
}}

I know only how the int value is shifted by using any one of shift operator

Above pgm is hex value. That hex value first converted to int than it going to be shifted .am I right?

Convert to hex to int and than shifted is take more time to find the answer.

If suppose the real exam having like this means how to find a answer very fast as well as by easiest way

Please replay as soon as possible.

Preparing SCJP(1.4)

(removed shouting from topic title)
[ May 10, 2005: Message edited by: Barry Gaunt ]
 
deepu Bhalotia
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i1 = 0xffffffff

Hi friends,

I think the Number being assigned to int variable is out of range. So in this case it should throw a compile time error.

Deepak
 
Dhanesh Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no compiletime error.
i checked it .
it will give ans in hex

-----------
SCJP(preparing)
 
deepu Bhalotia
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends

class EBH019 {
public static void main (String args[]) {
int i1 = 0xffffffff, i2 = i1 << 1;
int i3 = i1 >> 1, i4 = i1 >>> 1;
System.out.print(Integer.toHexString(i2) + ",");
System.out.print(Integer.toHexString(i3) + ",");
System.out.print(Integer.toHexString(i4));
}}

Hi,

Here the compile will first convert theHex number into Int and check for the number. whether this number is fit in int.

But while solving this there is no need for us to convert in int. We can directly cenvert hex number into binary and shift it.

Like...

int a=ffffffff;
So binary number for this is
1111 1111 1111 1111 1111 1111 1111 1111.

Now you can do the shifting. and again can convert in to hex easily. Since the options are also given in Hex. So i don't think that it's going to take more time.

Deepak.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic