Why is the range of signed byte is from -128 to 127 (2’s complement) and not from -127 to 127?

Why is the range of unsigned byte is from -128 to 127? It’s not. An unsigned byte (assuming 8-bit) is from 0 to 255. The range of a signed byte using 2’s complement is from -128 to 127, directly from the definition of 2’s complement: 01111111 = +127 01111110 = +126 01111101 = +125 … … Read more

why Integer.MAX_VALUE + 1 == Integer.MIN_VALUE?

Because the integer overflows. When it overflows, the next value is Integer.MIN_VALUE. Relevant JLS If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two’s-complement format. If overflow occurs, then the sign of the result is not the same as the sign of … Read more

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

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

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

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)