Why prefer two’s complement over sign-and-magnitude for signed numbers?

It’s done so that addition doesn’t need to have any special logic for dealing with negative numbers. Check out the article on Wikipedia. Say you have two numbers, 2 and -1. In your “intuitive” way of representing numbers, they would be 0010 and 1001, respectively (I’m sticking to 4 bits for size). In the two’s … 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