Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

question about long  RSS feed

 
xu han
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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!

 
Tony Docherty
Bartender
Posts: 3268
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I see。
thanks Campbell Ritchie and Tony Docherty。
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!