Jump to content
  • Advertisement
Sign in to follow this  
dxFoo

[java] Unsigned data types?

This topic is 4776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible with Java? I heard there was a way with the >>> operator, but I never understood how its done. Can anyone provide any code samples? Thanks. Edit: I looked in my book and it's something like this... The following code fragment demostrates the >>>. Here, a is set to -1, which sets 32 bits to 1 in binary. The value is then shifted right 24 bits, filling the top 24 bits with zeros, ignoring normal sign extension. This sets a to 255. int a = -1; a = a >>> 24; 11111111 11111111 11111111 11111111 // -1 in binary as an int >>>24 00000000 00000000 00000000 11111111 // 255 in binary as an int [Edited by - dxFoo on August 28, 2005 1:55:21 PM]

Share this post


Link to post
Share on other sites
Advertisement
You're using the wrong operator. It should be '>>' rather than '>>>'. See also this: Summary of operators.

As far as I can tell that is your problem here.

L-

Oh, I'm sorry -- I didn't read your post fully. As far as unsigned types are concerned: do a search on this topic for java unsigned types here at GameDev, there was a Thread not to long ago about this, and a work around (some what) a lot of bit shifts, packing and unpacking really. Unsigned types are not really supported. IMHO, there are some only special cases for unsigned types.

[Edited by - Lucidquiet on August 28, 2005 4:52:35 PM]

Share this post


Link to post
Share on other sites
>>> is correct. There's two kinds of right shift, arithmetic shift (where the sign bit is extended), and logical shift (where a bunch of 0's are put on the left, this is the one you want). In C++, the compiler figures out which kind you want based on whether the number is signed or not: it does arithmetic shift on signed numbers and logical shift on unsigned. In Java, you *tell* it which kind you want: >> is arithmetic shift and >>> is logical shift. So if you want to treat a number as unsigned, then >>> is the right thing to use.

For reference, all numbers in Java are technically signed. So to treat a number as "unsigned", you just have to know which operations are affected by signedness, and handle them correctly.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!