How Are Integers Internally Represented at a Bit Level in Java

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 type is a single-precision 32-bit IEEE 754 floating point.

double: double data type is a double-precision 64-bit IEEE 754 floating point.

boolean: boolean data type represents one bit of information.

char: char data type is a single 16-bit Unicode character.

Source

Two's complement

"The good example is from wiki that the relationship to two's complement is realized by noting that 256 = 255 + 1, and (255 − x) is the ones' complement of x

0000 0111=7 two's complement is 1111 1001= -7

the way it works is the MSB(the most significant bit) receives a negative value so in the case above

-7 = 1001= -8 + 0+ 0+ 1

Positive integers are generally stored as simple binary numbers (1 is 1, 10 is 2, 11 is 3, and so on).

Negative integers are stored as the two's complement of their absolute value. The two's complement of a positive number is when using this notation a negative number.

Source

Since I received a few points for this answer, I decided to add more information to it.

A more detailed answer:

Among others there are four main approaches to represent positive and negative numbers in binary, namely:

  1. Signed Magnitude
  2. One's Complement
  3. Two's Complement
  4. Bias

1. Signed Magnitude

Uses the most significant bit to represent the sign, the remaining bits are used to represent the absolute value. Where 0 represents a positive number and 1 represents a negative number, example:

1011 = -3
0011 = +3

This representation is simpler. However, you cannot add binary numbers in the same way that you add decimal numbers, making it harder to be implemented at the hardware level. Moreover, this approach uses two binary patterns to represent the 0, -0 (1000) and +0 (0000).

2. One's Complement

In this representation, we invert all the bits of a given number to find out its complementary. For example:

010 = 2, so -2 = 101 (inverting all bits).

The problem with this representation is that there still exist two bits patterns to represent the 0, negative 0 (1111) and positive 0 (0000)

3. Two's Complement

To find the negative of a number, in this representation, we invert all the bits and then add one bit. Adding one bit solves the problem of having two bits patterns representing 0. In this representation, we only have one pattern for
0 (0000).

For example, we want to find the binary negative representation of 4 (decimal) using 4 bits. First, we convert 4 to binary:

4 = 0100

then we invert all the bits

0100 -> 1011

finally, we add one bit

1011 + 1 = 1100.

So 1100 is equivalent to -4 in decimal if we are using a Two's Complement binary representation with 4 bits.

A faster way to find the complementary is by fixing the first bit that as value 1 and inverting the remaining bits. In the above example it would be something like:

0100 -> 1100
^^
||-(fixing this value)
|--(inverting this one)

Two's Complement representation, besides having only one representation for 0, it also adds two binary values in the same way that in decimal, even numbers with different signs. Nevertheless, it is necessary to check for overflow cases.

4. Bias

This representation is used to represent the exponent in the IEEE 754 norm for floating points. It has the advantage that the binary value with all bits to zero represents the smallest value. And the binary value with all bits to 1 represents the biggest value. As the name indicates, the value is encoded (positive or negative) in binary with n bits with a bias (normally 2^(n-1) or 2^(n-1)-1).

So if we are using 8 bits, the value 1 in decimal is represented in binary using a bias of 2^(n-1), by the value:

+1 + bias = +1 + 2^(8-1) = 1 + 128 = 129
converting to binary
1000 0001

Which bit is the higher order bit in int type in java?

If you'll print the binary representation of 5and -5:

System.out.println (Integer.toBinaryString (5));
System.out.println (Integer.toBinaryString (-5));

You'll get:

101
11111111111111111111111111111011

or, if we add the leading 0s:

00000000000000000000000000000101
11111111111111111111111111111011

As you can see, the 2 representations differ in more than just the sign bit (which is the left most bit). Therefore your code is incorrect.

Setting the sign bit of the binary representation of 5:

System.out.println (5|(1<<31));

doesn't result in -5, it results in:

-2147483643

How are signed integers values represented in computer memory?

Consider 4 bits;

0   0000    0
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7

8 1000 -8
9 1001 -7
A 1010 -6
B 1011 -5
C 1100 -4
D 1101 -3
E 1110 -2
F 1111 -1

If the number is unsigned humans understand the bit pattern as the left column.

If the number is signed humans understand the bit pattern as the right column.

take decimal 10 - A 1010 -6 for example;

if signed 3 - 6 = -3 will look like this:

(3)     0011 +
(-6) 1010
---- ----
(-3) 1101

if the number is unsigned the exact same operation looks like this 3 + 10 = 13:

(3)     0011 +
(A) 1010
---- ----
(D) 1101

Notice the bit patterns stay the same, the only thing i'm changing is the human readable representation depending if the number is signed or unsigned, even the operation is the same internally (i.e addition).

Why negative signed integer with bitwise AND 0xFF will result in positive signed integer?

signed is promoted to int when you apply the & operator, because of binary numeric promotion.

It's not 10000111 & 11111111, it's 11111111111111111111111110000111 & 00000000000000000000000011111111, the value of which is 00000000000000000000000010000111 (still an int).

The MSB here is zero, hence it's positive.

If you cast it back to a byte, which would take just the 8 LSBs, that byte would be negative again.

Write a negative number in binary (2's complement) without using the positive number

In signed two's complement the bit values are exactly the same as for unsigned, except the top bit is negated. So

-1*bitn-1*2n-1 + bitn-2*2n-2 + ... + bit1*21 + bit0*20



Related Topics



Leave a reply



Submit