Sitemap
rakibul rushel

rakibul rushel

Signed Integer-Primitive Data Types

6 min readOct 13, 2023

--

In this article we are going to discuss signed integer and java support for primitive data types to represent signed integer.

Previous Chapter
First Chapter

Binary Number

A binary number is a number in binary number system which consists of only 1’s and 0’s as digits. Each digit in a binary number is called bit. For example, The binary number 01001011 consists of 8 bits. Each of those 8 bits has a positions associated with it.

7 6 5 4 3 2 1 0 → bit positions
0 1 0 0 1 0 1 1 → binary number

In the above figure the left most bit is 0 and its position is 7 and the right most bit is 1 and its position is 0. The above binary number is a 8 bit binary number.

Most Significant Bit (MSB) and Least Significant Bit(LSB)

Consider a 4 bit binary number B = 1011. The most significant bit is the left most bit which is 1 and the least significant bit is the right most bit which is 0.

3 2 1 0 -> bit positions
1 0 1 1 -> binary number consists of 4 bits

The bit at position 3 is the MSB (most significant bit) and the bit at position 0 is the LSB (least significant bit). If we consider a binary number with n bits then bit at the position (n-1) is the most significant bit (MSB) and bit at the position 0 is the least significant bit (LSB).

3 2 1 0 -> bit positions
1 0 1 1 -> Least significant bit is 1
1 0 1 1 -> Least significant 2 bits are 11
1 0 1 1 -> Least significant 3 bits are 011

3 2 1 0 -> bit positions
1 0 1 1 -> Most significant bit is 1
1 0
1 1 -> Most significant 2 bits are 10
1 0 1
1 -> Most significant 3 bits are 101

What is a Signed Integer?

A signed integer is an integer in decimal number system which is either positive, negative or zero. Java provides four primitive data type for a variable to hold signed integer in signed 2’s complement. Those four are:

  1. byte
  2. short
  3. int
  4. long

Primitive Data Type — byte

byte data type specifies that variable of type byte can holds signed integer in 8 bit 2’s complement. The minimum negative signed integer the variable can hold is -2⁷ (-128 in decimal) and the maximum positive signed integer the variable can hold is 2⁷-1 (127 in decimal).

8 bit 2’s complement is binary number consists of 8 bits. The leftmost bit that is the bit at position 7 (the MSB) is called the sign bit. If the sign bit is 0 then decimal equivalent of the binary number represented by the 8 bit 2’s complement form is positive otherwise negative. 0 is a special signed integer which is neither considered negative or positive. You can call 0 a non-negative signed integer.

Lets B = 01010111 be a binary number in 8 bit 2’s complement.

7 6 5 4 3 2 1 0 → bit positions
0 1 0 1 0 1 1 1 → binary number in 8 bit 2’s complement form

The sign bit is 0 (in bold). So, the signed integer represented by B is positive. Now if we convert binary number B in 8 bit 2’s complement to decimal number system. We have to do following:
-(0 x 2⁷) + 1 x 2⁶ + 0 x 2⁵ + 1 x 2⁴ + 0 x 2³ + 1 x 2² + 1 x 2¹ + 1 x 2⁰ = 64 + 16 + 4 + 2 + 1 = 87.

So the binary number B = 01010111 in 8 bit complement form represents positive signed integer 87 in decimal number system.

So, we can say that the binary number 01111111 in 8 bit 2’s complement gives the maximum positive signed integer.

7 6 5 4 3 2 1 0 → bit positions
0 1 1 1 1 1 1 1 → binary number in 8 bit 2’s complement.

Get rakibul rushel’s stories in your inbox

Join Medium for free to get updates from this writer.

If we convert 01111111 to decimal number system we have the following:
-(0 x 2⁷) + 1 x 2⁶ + 1 x 2⁵ + 1 x 2⁴ + 1 x 2³ + 1 x 2² + 1 x 2¹ + 1 x 2⁰ = 2⁶ + 2⁵ + 2⁴ + 2³ + 2² + 2¹ + 2⁰ = 2⁷- 1 (by using geometric progression sum formula).

Now lets consider a negative signed integer -125 which can express using 8 bit 2’s complement binary number. The process following:

  1. First take the positive equivalent of -125 which is 125.
  2. Express 125 using 8 bit 2’s complement form. We get 01111101 binary number in 8 bit 2’s complement.
  3. Now invert or flip all the bits of 01111101. Inverting a bit means if a bit is 0 then it will be changed to 1 and if a bit is 1 then it will be changed to 0. So, by inverting all the bits of 01111101 we get 10000010.
  4. Add binary number 00000001 (represents as 1 in decimal number system) in 8 bit 2’s complement to 10000010. The result of adding 00000001 to 10000010 is 10000011. So, 10000011 is 8 bit 2’s complement form of the negative signed integer -125. Do not worry how adding binary number works. However, if you are interested you can read this.

If we want to convert the binary number 10000011 in 8 bit 2’s complement to its decimal equivalent we have to write:
-(1 x 2⁷) + 0 x 2⁶ + 0 x 2⁵ + 0 x 2⁴ + 0 x 2³ + 0 x 2² + 1 x 2¹ + 1 x 2⁰ = -2⁷ + 2¹ + 2⁰ = -128 + 2 + 1 = -125. This is basically -128 + (decimal equivalent represented by the binary number defined by the least significant 7 bits.

So if binary number is given in 8 bit 2’s complement and it has sign bit 1 we can find the negative signed integer of that binary number by following formula:
-2⁷ + (decimal equivalent of the binary number defined by the least significant 7 bits)

So, binary number 10000000 in 8 bit 2’s complement gives the minimum negative signed integer. Lets verify that by formula : -128 + (decimal equivalent of the binary number defined by the least significant 7 bits) = -128 + (decimal equivalent of the binary number 0000000 is 0) = -128 + 0 = -128. So, -128 is the minimum negative signed integer for the data type byte

0 is represented using binary number 00000000 in 8 bit 2’s complement.

So, byte data type enables a variable to hold integer which is greater than or equal to -2⁷ and smaller than or equal to (2⁷ — 1).

Fig: Variable example with data type byte
Press enter or click to view image in full size
Fig: Output of the above program

There are two variable named minimumValueInByte and maximumValueInByte of type byte. The minimumValueInByte holds -128 and maximumValueInByte holds 127.

If we now try to modify by assigning value 128 to variable maximumValueInByte we will get compile error as 128 is not in range of values provided by byte data type which is -2⁷ to 2⁷-1 (inclusive).

Fig: Out of range error

In the above code we are trying to assigning 128 to variable maximumValueInByte by the statement at line 12. When you try to compile the program following compile error thrown by the java compiler. Do not worry about the compile error now. I wanted show you that if you want to store integer to a variable of type byte, your integer have to be in the range of signed integers provided by byte.

Press enter or click to view image in full size
Fig: Error caught by compiler for above program

The other data types short, int, long are similar to byte except there are 16 bit, 32 bit and 64 bit respectively.

Primitive Data Type — short

short data type specifies that variable of type short can holds signed integer in 16 bit 2’s complement. The minimum signed negative integer the variable can hold is -2¹⁵ (-32768 in decimal) and the maximum signed positive integer the variable can hold is 2¹⁵-1 (32767 in decimal).

Primitive Data Type — int

int data type specifies that variable of type int can holds signed integer in 32 bit 2’s complement. The minimum signed negative integer the variable can hold is -2³¹ (-2147483648 in decimal) and the maximum signed positive integer the variable can hold is 2³¹-1 (2147483647 in decimal).

Primitive Data Type — long

long data type specifies that variable of type long can holds signed integer in 64 bit 2’s complement. The minimum signed negative integer the variable can hold is -2⁶³ ( — 9223372036854775808 in decimal) and the maximum signed positive integer the variable can hold is 2⁶³-1 (9223372036854775807 in decimal).

Next Chapter

References

  1. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

--

--