Your question is "Why doesn't Java support unsigned ints"?
And my answer to your question is that Java wants that all of it's primitive types: byte, char, short, int and long should be treated as byte, word, dword and qword respectively, exactly like in assembly, and the Java operators are signed operations on all of it's primitive types except for char, but only on char they are unsigned 16 bit only.
So static methods suppose to be the unsigned operations also for both 32 and 64 bit.
You need final class, whose static methods can be called for the unsigned operations.
You can create this final class, call it whatever name you want and implement it's static methods.
If you have no idea about how to implement the static methods then this link may help you.
In my opinion, Java is not similar to C++ at all, if it neither support unsigned types nor operator overloading, so I think that Java should be treated as completely different language from both C++ and from C.
It is also completely different in the name of the languages by the way.
So I don't recommend in Java to type code similar to C and I don't recommend to type code similar to C++ at all, because then in Java you won't be able to do what you want to do next in C++, i.e. the code won't continue to be C++ like at all and for me this is bad to code like that, to change the style in the middle.
I recommend to write and use static methods also for the signed operations, so you don't see in the code mixture of operators and static methods for both signed and unsigned operations, unless you need only signed operations in the code, and it's okay to use the operators only.
Also I recommend to avoid using short, int and long primitive types, and use word, dword and qword respectively instead, and you are about call the static methods for unsigned operations and/or signed operations instead of using operators.
If you are about to do signed operations only and use the operators only in the code, then this is okay to use these primitive types short, int and long.
Actually word, dword and qword don't exist in the language, but you can create new class for each and the implementation of each should be very easy:
The class word holds the primitive type short only, the class dword holds the primitive type int only and the class qword holds the primitive type long only. Now all the unsigned and the signed methods as static or not as your choice, you can implement in each class, i.e. all the 16 bit operations both unsigned and signed by giving meaning names on the word class, all the 32 bit operations both unsigned and signed by giving meaning names on the dword class and all the 64 bit operations both unsigned and signed by giving meaning names on the qword class.
If you don't like giving too many different names for each method, you can always use overloading in Java, good to read that Java didn't remove that too!
If you want methods rather than operators for 8 bit signed operations and methods for 8 bit unsigned operations that have no operators at all, then you can create the Byte class (note that the first letter 'B' is capital, so this is not the primitive type byte) and implement the methods in this class.
About passing by value and passing by reference:
If I am not wrong, like in C#, primitive objects are passed by value naturally, but class objects are passed by reference naturally, so that means that objects of type Byte, word, dword and qword will be passed by reference and not by value by default. I wish Java had struct objects as C# has,
so all Byte, word, dword and qword could be implemented to be struct instead of class, so by default they were passed by value and not by reference by default, like any struct object in C#, like the primitive types, are passed by value and not by reference by default, but because that Java is worse than C# and we have to deal with that, then there is only classes and interfaces, that are passed by reference and not by value by default. So if you want to pass Byte, word, dword and qword objects by value and not by reference, like any other class object in Java and also in C#, you will have to simply use the copy constructor and that's it.
That's the only solution that I can think about. I just wish that I could just typedef the primitive types to word, dword and qword, but Java neither support typedef nor using at all, unlike C# that supports using, which is equivalent to the C's typedef.
About output:
For the same sequence of bits, you can print them in many ways: As binary, as decimal (like the meaning of %u in C printf), as octal (like the meaning of %o in C printf), as hexadecimal (like the meaning of %x in C printf) and as integer (like the meaning of the %d in C printf).
Note that C printf doesn't know the type of the variables being passed as parameters to the function, so printf knows the type of each variable only from the char* object passed to the first parameter of the function.
So in each of the classes: Byte, word, dword and qword, you can implement print method and get the functionality of printf, even though the primitive type of the class is signed, you still can print it as unsigned by following some algorithm involving logical and shift operations to get the digits to print to the output.
Unfortunately the link I gave you doesn't show how to implement these print methods, but I am sure you can google for the algorithms you need to implement these print methods.
That's all I can answer your question and suggest you.
someUInt32=someUInt16;
would be legal, butif (someInt32==someInt16)
would not be legal without a typecast) but included operator overloads that made sense (e.g. adding a signed value to a uint value cycles the group by that amount). – Stowebyte
not being able to give a straight140
gray level but a-116
that you need to& 0xff
to get the correct value. – Churchwellbyte b = 0x99;
. – Jemmie&0xff
. Loss of computing power... – ChurchwellInputStream.read()
return unsigned bytes if unsigned bytes don't exist in Java? Since we have everywhere just bits, not signs, we have the two complement, a special interpretation of those bits which requires “special” treatment; that is, also you need to know extra rules, which sometimes you don't want to have in the way. E.g., using bytes read withInputStream.read()
to build a 32 bit value,(b[3] << 24)|(b[2] << 16)|(b[1] << 8)|b[0]
… this works always iff you have unsigned bytes. Otherwise, sign extension gets in the way and you need masking. – Dehliaread()
returns anint
type which has the range of an unsigned byte (0 to 255) unless the end of file is reached. A more efficient way to read anint
is as a 32-bit value rather than byte by byte. Note: you have to handle running out of bytes e.g. there is only 2 bytes when you actually want 4. – Gehleniteread
: written the previous comment withread(byte b[], ...)
in mind. Holding unsigned values into a signed bigger “container” is what I currently do — except in case oflong
. Still, when converting C code into Java, you have to watch yourself more than it should be if there were unsigned types. – Dehlia