Ok, was Java intentionally designed to make low level coding nearly impossible?

Started by
4 comments, last by Iftah 15 years, 4 months ago
I'm trying to make a game in J2me and I'm trying to load a map in my own format. The problem is the map is super compressed and Java is giving me a hard time working with byte data. First of all there seems to be no way to convert a unsigned byte to int, which is a serious problem since Java wont let me do ANY bitwise operations on a byte. I mean wtf? Whats the point of having a byte type if you're going to intentionally make it damn near useless? Maybe I'm missing something. In c++ I can do just about any damn thing I want with data. Why is Java so restricted?
Simplicity is the ultimate sophistication. – Leonardo da Vinci
Advertisement
Quote:Original post by ForeverNoobie
First of all there seems to be no way to convert a unsigned byte to int

byte b = 100;int i = b & 255;

Does this work?
Quote:Original post by ForeverNoobie
First of all there seems to be no way to convert a unsigned byte to int,

int my_integer = (int) my_byte; ?

Quote:In c++ I can do just about any damn thing I want with data. Why is Java so restricted?
C++ and Java both make attempts to restrict what the user does. The main difference is that, if you do something you're not allowed to do, Java complains and C++ chooses what it thinks fits best (which is, sadly, not what you indended most of the time).

hmmm... Ooops... I must have misplaced some parentheses... Sorry for the rant...
Simplicity is the ultimate sophistication. – Leonardo da Vinci
Quote:Original post by ToohrVyk
int my_integer = (int) my_byte; ?

First of all, byte can implicitly coerced to int, so you don't need the explicit cast.

The real problem is that Java's byte is signed, so converting 10010010 to int will yield 11111111111111111111111110010010. That's why you have to mask with 255 to get the correct "unsigned" result.
edit: yet again, the extra verbose message was beaten by the quick and short one! (DevFred wins)

You can do bit operations, but there are no unsigned numbers

So if you have a byte (8 bit) its a number between -128...127
You can cast it to int just fine, but don't expect to find int with numbers above 127.

note when you look at it bitwise, converting a negative byte (eg. 11111101) to short will result in a negative short, that is the extra bits are filled with ones (eg. 1111111111111101 not 0000000011111101)

converting it to integer and then and'ing (&) the result with 255 will produce correct values.

the only concept of unsigned number in java is char (0..65535) but its not the same as integer and don't use it as such.

This topic is closed to new replies.

Advertisement