Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

store 0xFFFFFFFF  RSS feed

 
Mahesh x Bogadi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I need to stored 0xFFFFFFFF as an integer.
I tried using long without success.

Please help.
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so why don't you store it as an integer. It does not exceed the allowed value for an int.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that depends. Remember that although ints are 32 bits (4 bytes, like the number above), they are always signed so we only have the range of approximately +/- 2^31
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possible to store the bit pattern consisting of 32 ones as an int, but Java will interpret that to mean "-1", because all numbers having the most significant bit set are negative.


I tried using long without success.


What does this mean? Have you tried:

long num = 0xffffffffl;

(That's a leading zero before the x, and a trailing lowercase letter L).
[ August 12, 2005: Message edited by: Ulf Dittmer ]
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to get what java might consider as negatively signed data into an int without the spreading of the sign bit is by ANDing it with the mask: 0xFF to insure the high order bits are set to 0.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(That's a leading zero before the x, and a trailing lowercase letter L).
It doesn't have to be lowercase; in fact it's preferable to use the uppercase "L" so you don't confuse it with the number "1"; I believe that's one of Bloch's puzzlers...
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Norm Radder:
Another way to get what java might consider as negatively signed data into an int without the spreading of the sign bit is by ANDing it with the mask: 0xFF to insure the high order bits are set to 0.

Maybe I'm not understanding what you're proposing, but that will mask off everything but the 8 lowest bits. Is that your intended behavior? How is that getting negatively signed data into an int without "spreading the sign bit"?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mahesh Bogadi:
Hi All,

I need to stored 0xFFFFFFFF as an integer.
I tried using long without success.

Please help.


int x = 0xFFFFFFFF;

What's wrong with this? As noted above, Java will interpret this as -1 if you simply use it as an int, but if your are doing bit manipulations why does this matter? Maybe I don't understand your question. If you can explain what you have tried and why it didn't seem to work, we can help a lot more easily. At the moment all we can do is guess what your problem is, but that isn't a very efficient way of answering your question.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!