Why can’t the Java byte data type store 128?
Welcome to yet another short article. I hope you all are doing exceptionally well.
In this article, I’m gonna talk about the byte data type in Java. The byte data type is a primitive data type. Generally, the byte data type is used for storing small integer values. It can store integer values in the range of -128 to 127 or -2⁷ to 2⁷-1. But the question is why can’t it store 128?
Well, we know that the byte data type allocates 1-byte (8-bit) memory. When we declare a variable and assign a value to it, it will store the binary representation of this value in the memory. Binary numbers can be represented in two ways. One is unsigned and another is signed. Unsigned binary numbers don’t have any sign bit. Whereas, signed binary numbers have a sign bit. In a signed binary number, the MSB bit (Most Significant Bit-left most bit of a binary number) is called the sign bit. The sign bit defines if a binary number is positive or negative. If the sign bit is unset (0), then the binary number is positive. If the sign bit is set (1), then the binary number is negative.
The largest integer the byte data type can store is 127. The binary representation of 127 is-
The binary representation of 127 -
127 (Base 2) = 0 1 1 1 1 1 1 1
^
Sign Bit
Here sign bit is unset(0) because 127 is a positive number. Without the sign bit, we can represent 127 by the rest of the 7 bits.
Let’s talk about negative numbers. How Java stores negative numbers in memory? When Java stores a negative integer number in memory, it stores the 2’s complement of the positive equivalent value of this number. The positive equivalent value of -128 is 128. So, the 2’s complement of 128 –
The binary representation of 128 -
128 (Base 2) = 1 0 0 0 0 0 0 0
First, calculate the 1's complement of 128 -
1's complement of 128 = 0 1 1 1 1 1 1 1
To calculate 2's complement, just add 1 at the LSB position of the 1's complement -
0 1 1 1 1 1 1 1
+ 1
-----------------------------------------------
2's complement of 128 = 1 0 0 0 0 0 0 0
Here sign bit is set(1) means the number is negative. Without the sign bit, the the 2’s complement is fit for the rest of the 7 bits. We can see that, the 2’s complement of 128 can easily be represented by the 8 bit signed byte data type.
But what happens when we try to store 128 in the byte data type? The binary representation of 128 is-
The binary representation of 128 -
128 (Base 2) = 1 0 0 0 0 0 0 0 - equivalent to the 2's complement of 128 (Binary representation of -128).
^
Sign Bit
Here, the sign bit is set(1). That means the binary representation of 128 is now negative and it is equivalent to the 2’s complement of 128 (Also we can call it the binary representation of -128). How does it make sense that 128 will be stored as -128? This will create the ambiguity and Java doesn’t know what we mean whether -128 or 128. So, in order to maintain consistency the Java byte data type can store -128 although, it can’t store 128.
Thanks for reading this article.
Why can’t the Java byte data type store 128? was originally published in Javarevisited on Medium, where people are continuing the conversation by highlighting and responding to this story.
This post first appeared on Read More