• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Shift Operator Dought

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What's wrong? Where are you going? Stop! Read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic