sign changes when going from int to float and back

Your program is invoking undefined behavior because of an overflow in the conversion from floating-point to integer. What you see is only the usual symptom on x86 processors. The float value nearest to 2147483584 is 231 exactly (the conversion from integer to floating-point usually rounds to the nearest, which can be up, and is up … Read more

bitwise not operator

You are actually quite close. In binary , not 0 should be 1 Yes, this is absolutely correct when we’re talking about one bit. HOWEVER, an int whose value is 0 is actually 32 bits of all zeroes! ~ inverts all 32 zeroes to 32 ones. System.out.println(Integer.toBinaryString(~0)); // prints “11111111111111111111111111111111” This is the two’s complement … Read more

What is “two’s complement”?

Two’s complement is a clever way of storing integers so that common math problems are very simple to implement. To understand, you have to think of the numbers in binary. It basically says, for zero, use all 0’s. for positive integers, start counting up, with a maximum of 2(number of bits – 1)-1. for negative … Read more

How are integers internally represented at a bit level in Java?

Let’s start by summarizing Java primitive data types: byte: Byte data type is an 8-bit signed two’s complement integer. Short: Short data type is a 16-bit signed two’s complement integer. int: Int data type is a 32-bit signed two’s complement integer. long: Long data type is a 64-bit signed two’s complement integer. float: Float data … Read more