Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Unsigned data types?

Started by dxFoo Aug 28, 2005 at 1:13 PM 4 replies 750+ views
Original Post
dxFoo
dxFoo
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]
Lucidquiet
Lucidquiet
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]
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
pinacolada
pinacolada
>>> 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.
Lucidquiet
Lucidquiet
Yeah, he's right -- see my post -- I didn't read the post fully.
"Education is when you read the fine print; experience is what you get when you don't." -Pete Seegerwww.lucid-edge.net
Blew
Blew
God knows how many times I wished for the unsigned keyword in Java.
blew
dxFoo
dxFoo
Agreed. It's a less tedious task if they included the keyword.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.