How data is stored in 32-bit?

Let’s consider we need to store 10 in memory. First, we need to convert 10 into 32-bit binary. The binary value of 10 (decimal) is 1010. Now to convert it to 32-bit binary add 28 zeros before 1010.

        10 (decimal) = 00000000   00000000   00000000   00001010
                          A          B          C          D

These 32 bits will not be stored in the allocated 4-byte memory directly. it will be stored according to endianness.

Representation of Int Variable in Memory in C++

To understand the Representation of int variable in memory in C/C++ below is the basic program to create a variable and its size. Usually, the value of an int variable is stored in a series of bytes that are represented as the variable’s representation as memory. Depending on the hardware and compiler being used, an int variable may be represented by a different number of bytes.

Example:

C++




// C++ Program to create a variable and show its size.
#include <iostream>
using namespace std;
  
int main()
{
  
    int x = 10;
  
    cout << x << endl;
    cout << "size of x is : " << sizeof(x) << endl;
    return 0;
}


Output

10
size of x is : 4

Explanation of the above program

In the above program, we just created an int variable and assigned a value of 10 to it, then we just printed its value and the size of a variable into the output stream. Int variable allocates 4 bytes or 32 bits of memory by default. Out of these 32 bits, only one bit is a sign bit and the other 31 bits are data bits (this is only valid for signed int but in the case of unsigned int all 32 bits are data bits as it only stores positive numbers). The architecture of the int variable is shown below.

S              

                   A

               

                      B

               

                        C

               

                      D

Out of this 32-bit, the first bit of the first byte is the sign bit. if the signed bit is 0 then the data stored in these other 31 bits are positive but if the signed bit is 1 then the data is negative, so the computer will convert stored data in 2’s complement while accessing it.

Similar Reads

How data is stored in 32-bit?

...

What is endianness?

Let’s consider we need to store 10 in memory. First, we need to convert 10 into 32-bit binary. The binary value of 10 (decimal) is 1010. Now to convert it to 32-bit binary add 28 zeros before 1010....

Contact Us