• 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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shift operator
hi everybody,
just try 2 compile this.

class A{
public static void main(String [] args){

byte x,y,z;
x=2<<5;
System.out.println(x);
}
}

when you run the above code x will return 64. if 2<<10 then it will not run b'cos of compile time error(possible loss of precision). then i 2<<32. but this time it runs perfectly & return 2. so can anyone know why dis happen?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the issue here is a narrowing primitive conversion as part of an assignment conversion.

2 << 5 is known at compile-time, and will fit into a byte.

2 << 10 is known at compile-time, and will not fit into a byte.

The second operand to the shift operator will be reduced modulo 32 before the shift is carried out. So 2 << 32 is converted to 2 << 0 before the shift is carried out.
[ August 13, 2006: Message edited by: Keith Lynn ]
 
shaan nimaz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thankx buddy
 
Can't .... do .... plaid .... So I did this tiny ad instead:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic