# How integers are stored in memory

--

All these three data types int, signed int and float, 32 bits are allocated in memory.

Let us try to understand one by one.

**1. int a = 456;**

RHS 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

If the system allocate the address as 3000 for variable a. Then the values are stored as follows.

3000–01100100

3001–00000001

3002–00000000

3003–00000000

**virtual memory addresses** available per process for unsigned integers

**2.** **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 is

00000000 00000000 00001001 00110101

If the system allocate the address as 4000 for variable a. Then the values are stored as follows.

4000–00110101

4001–00001001

4002–00000000

4003–00000000

**3. float a=34.125;**

This data type is called float, because the decimal point will be moving (floating). The other data type is called fixed point, because the decimal point does not move. C does not support fixed point

The number 34.125 can be converted to binary as follows

34 = 32 *1 + 0 * 16 + 8 * 0 + 4 * 0 + 2 * 1 + 1 * 0 = 100010

.125 = 0.5 * 0 + 0.25 * 0 + 0.125 * 1 = 2^-3

So the binary representation of 34.125 can be 100010.001

RHS — 34.125 = 100010.001

= 1.00010001 x ²⁵

(The decimal point is moved left 5 position)

Now as per the standard 1. will be removed.

00010001 is called mantissa (significand)

101(5) is called exponent

Now let us see how to construct a 32 bit binary number using these values.

1 bit represent sign bit

8 bits represents exponent

23 bits represents mantissa

In this case, the number is positive, so sign bit is 0

In this case, the exponent is 5 , add 127 (exponent bias) we get 132

= 10000100

In this case, the mantissa = 00010001000000000000000

so the 32 bit binary value of 34.125 is

0 10000100 00010001000000000000000

= 01000010 00001000 10000000 00000000–4 bytes stored in memory.

If the system allocate the address as 5000 for variable a. Then the values are stored as follows.

4000–00000000

4001–10000000

4002–00001000

4003–01000010

This is the way the values are stored in Memory in C language.

main source:https://www3.ntu.edu.sg/home/ehchua/programming/cpp/c1_Basics.html