Wednesday 10 April 2013

DETERMINE HOW INTEGERS ARE STORED IN YOUR COMPUTER MEMORY

Have you ever wondered, how any integer is actually stored in computer memory?
The signed integers are stored in the computers memory using the 2's complement method.
Here is a program that verifies that, computers for storing integers, use 2's complement method.

This program is specifically designed for a compiler that stores the integers in 16 bits, that is 16-bit compilers.

SOURCE CODE:-
/********************************************************************************/

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int main()
{
clrscr();
int arr[16];
int num;
cout<<"ENTER THE INTEGRAL NUMBER OF WHICH YOU WANT TO FIND THE BIT PATTERN : ";
cin>>num;
int i=(num);
int z;
for(int k=15;k>=0;k--)
{
z=i>>k;
int j=1;
arr[k]=z&j;

}
for(k=15;k>=0;k--)
cout<<arr[k];

getch();
return 1;
}
/********************************************************************************/
Here the following images show how the output of above program varies when we find bit pattern for a positive and a negative integer.
From the above output we can easily infer that signed integers are stored in computer's memory using 2's complement method.

No comments:

Post a Comment