Win a copy of Spring Boot in Practice this week in the Spring forum!
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
• Tim Cooke
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Liutauras Vilda
• Henry Wong
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Mikalai Zaikin
• Himai Minh

# Shift operators

Ranch Hand
Posts: 56
• Number of slices to send:
Optional 'thank-you' note:
Hello,

I am unable to understand the output of the following code:

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));
}}

Output:

fffffffe,ffffffff,7fffffff

can anyone provide me the sequence of steps as to know how this output is generated?

Regards,

Mathangi.

Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
These are the octal numbers, they are converted into binary andd then left or right shifted and the results are converted back in hex 0x.

Try removing the Integer.toHexString statements and you get the integer values .

Mathangi Shankar
Ranch Hand
Posts: 56
• Number of slices to send:
Optional 'thank-you' note:
hI,

The given number that is 0xffffffff is hexadecimal number. I really do not know to convert hexadecimal number to binary.

If that part of help is provided I would continue doing the left shift and right shift.

Thanks,

Ranch Hand
Posts: 7729
• Number of slices to send:
Optional 'thank-you' note:

Thanks
-Barry

(Hopefully two birdies with one stone...)

Barry Gaunt
Ranch Hand
Posts: 7729
• Number of slices to send:
Optional 'thank-you' note:
Each hexadecimal digit represents four bits.

So 0x7ffffff is "0111 1111 1111 1111 1111 1111 1111 1111"

[ November 03, 2004: Message edited by: Barry Gaunt ]
[ November 03, 2004: Message edited by: Barry Gaunt ]

Mathangi Shankar
Ranch Hand
Posts: 56
• Number of slices to send:
Optional 'thank-you' note:
Thanks a lot I have understood the output.

------------

Mathangi.

 Don't get me started about those stupid light bulbs.