• 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

question about long

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As i try to add a f behind 0xffffffff,the compiler response a error,int is out of range.
right,because int has 32 bits. the output is right .
11111111111111111111111111111111

as i try to add a f behind 0xffffffff,the compiler response a error too,int is out of range.
why does compiler look 0xfffffff as a int?
the output is 1111111111111111111111111111111111111111111111111111111111111111.
since 0xffffffff has 32bits,why the output has 64bits "1"?

the output is right 1111111111111111111111111111111111111111111111111111111111111111.
thanks a lot!

 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hard coded integer type values are considered by the compiler to be ints unless you tell it otherwise. If you add an 'L' to the right hand end the compiler will consider it to be a long ie:
 
xu han
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help!
Hard coded integer type values are considered by the compiler to be ints unless you tell it otherwise.

output:
11111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111111
why the second is
1111111111111111111111111111111111111111111111111111111111111111
not
11111111111111111111111111111111
the hexadecimal value are equal,but the output are not equal. why?
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not assigning a long to the second number. You are assigning an int. Remember all integer literals are unsigned, so you can write 0xffff_ffff as if it were a positive number, but it represents -1.
Now you are assigning the int value -1 to your long; it undergoes widening conversion to -1L. And -1L is sixty‑four 1 bits.
If you want to keep it as thirty‑two 1 bits, you would have to write 0xffff_ffffL. You can only use the _ character in Java7.
 
xu han
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see。
thanks Campbell Ritchie and Tony Docherty。
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic