What’s so special about 0x7f?

This is a form of bit-packing where the most significant bit of each byte is used to determine if another byte should be read. Essentially, this allows you to encode values in a fewer amount of bytes than they would normally require. However, there is the caveat that, if the number is large, then more than the normal amount of bytes … Read more

Why is the sizeof(int) == sizeof(long)?

A long, as far as I know, is 8 bytes. Is this correct? No, this is not correct. The C and C++ specifications only state that long must be greater than or equal to 32 bits. int can be smaller, but on many platforms, in C and C++, long and int are both 32 bits. This is a very good reason … Read more

Division of integers in Java

Converting the output is too late; the calculation has already taken place in integer arithmetic. You need to convert the inputs to double: Note that you don’t actually need to convert both of the inputs. So long as one of them is double, the other will be implicitly converted. But I prefer to do both, for symmetry.

How to implement infinity in Java?

double supports Infinity Add New Post Add titleHow to implement infinity in Java? double supports Infinitydouble inf = Double.POSITIVE_INFINITY; System.out.println(inf + 5); System.out.println(inf – inf); // same as Double.NaN System.out.println(inf * -1); // same as Double.NEGATIVE_INFINITY printsInfinity NaN -Infinity prints note: Infinity – Infinity is Not A Number.

Unsigned long in Java

Currently, I am using signed values, -2^63 to 2^63-1. Now I need the same range (2 * 2^64), but with positive values only. I found the java documentations mentioning unsigned long, which suits this use. I tried to declare 2^64 to a Long wrapper object, but it still loses the data, in other words, it … Read more

A long bigger than Long.MAX_VALUE

That method can’t return true. That’s the point of Long.MAX_VALUE. It would be really confusing if its name were… false. Then it should be just called Long.SOME_FAIRLY_LARGE_VALUE and have literally zero reasonable uses. Just use Android’s isUserAGoat, or you may roll your own function that always returns false. Note that a long in memory takes a fixed number of bytes. From Oracle: long: The long data type is a … Read more