How integers are stored in memory using two’s complement.

What is an Integer?

The size of int is usually 4 bytes (32 bits). And, it can take 232 distinct states from -2147483648 to 2147483647.

Example.

int a = 456;
The value is 456. Now let us convert it to binary.
256+0+64+32+0+0+4+0+0
1 0 1 1 0 0 1 0 0
Now you get 9 bit number. Since int allocates 32 bits, fill the remaining 23 bits with 0.
So the value stored in memory is
00000000 00000000 00000001 01100100

Signed and Unsigned.

signed int a=2357;RHS value is 2357. Not let us convert it to binary.
2048+0+0+256+0+0+32+16+0+4+0+1
1 0 0 1 0 0 1 1 0 1 0 1
Now you get 12 bit number. Since int allocates 32 bits, fill the remaining 23 bits with 0.So the value stored in memory is00000000 00000000 00001001 00110101

For example.

unsigned int x;
int y;

Here, the variable x can hold only zero and positive values because we have used the unsigned modifier.

Considering the size of int is 4 bytes, variable y can hold values from -231 to 231-1, whereas variable x can hold values from 0 to 232-1.

Store Data in the computer.

00000000 00000000 00000000 00000100
0x0A 0x0B 0x0C 0x0D memory address

The C standard doesn’t mandate any particular way of representing negative signed numbers.

In most implementations that you are likely to encounter, negative signed integers are stored in what is called two’s complement. The other major way of storing negative signed numbers is called one’s complement.

The two’s complement of an N-bit number x is defined as 2^N - x. For example, the two's complement of 8-bit 1 is 2^8 - 1, or 1111 1111. The two's complement of 8-bit 8 is 2^8 - 8, which in binary is 1111 1000. This can also be calculated by flipping the bits of x and adding one.

For example.

1 = 0000 0001
~1 = 1111 1110 (1’s complement)
~1 + 1 = 1111 1111 (2’s complement)
-1 = 1111 1111

21 = 0001 0101
~21 = 1110 1010
~21 + 1 = 1110 1011
-21 = 1110 1011

The one’s complement of an N-bit number x is defined as x with all its bits flipped, basically.

1 = 0000 0001
-1 = 1111 1110

21 = 0001 0101
-21 = 1110 1010

Two’s complement has several advantages over one’s complement. For example, it doesn’t have the concept of ‘negative zero’, which for good reason is confusing to many people. Addition, multiplication and subtraction work the same with signed integers.

Signed magnitude.

In 8-bit signed magnitude, the value 8 is represented as 0 0001000 and -8 as 1 0001000.

One’s complements.

Two’s complements.

In 8-bit two’s complement, the value 8 is represented as 00001000 and -8 as 11111000.

EXAMPLES.

int a = -2056;
Binary of 2056 will be calculated which is:00000000000000000000100000001000 (32 bit representation, according of storage of int in C)
2’s complement of the above binary is:
11111111111111111111011111111000.

So finally the above binary will be stored at memory allocated for variable a.

When it comes on accessing the value of variable a, the above binary will be retrieved from the memory location, then its sign bit that is the left most bit will be checked as it is 1 so the binary number is of a negative number so it will be 2’s complemented and when it will be 2’s complemented will be get the binary of 2056 which is:
00000000000000000000100000001000

The above binary number will be converted to its decimal equivalent which is 2056 and as the sign bit was 1 so the decimal number which is being gained from the binary number will be represented with a minus sign. In our case -2056.

Int number : -4
Binary representation (no sign): 100
Binary representation in memory (no sign): 00000000 00000000 00000000 00000100Now we add the sign using 2’s complement:1 step: flip — 11111111 11111111 11111111 111110112 step: add 1–11111111 11111111 11111111 11111100

If you want more information about this please click here. I hop this blog can helps you!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store