
For this laboratory, you are to experiment with the bit pattern used to represent an int in computers that use 2's complement to represent 32-bit integers. (See text, Section 4.2, pages 210-220. In C/C++ on Sun hardware the int type is a 32-bit 2's complement signed integer.)
Given either an "ordinary" integer (in decimal) or a bit pattern, it is easy to get the other representation, say, with a simple program like the following in C. (A similar program in Java is here.)
#include <stdio.h>
#include <ctype.h>
void main(void){
int i;
char ch;
for( ; ; ) {
while (isspace(ch = getchar()))
;
if (ch == 'x') {
scanf("%x", &i);
printf(" Decimal: %10i\n", i);
printf(" Bits: %08x \n", i);
}
else if (ch == 'i') {
scanf("%i", &i);
printf(" Decimal: %10i\n", i);
printf(" Bits: %08x \n", i);
}
else break;
}
}
Typical output looks like the following (user input in boldface):
i 47 Decimal: 47 Bits: 0000002f i -3 Decimal: -3 Bits: fffffffd x ffffffff Decimal: -1 Bits: ffffffff qFor this laboratory, experiment with the program by entering various integers, including 0, 1, 2, -2, -3, 2147483647, 2147483648 (overflow), -2147483648, and -2147483649 (underflow).
Base conversions in C: here.
Base conversions in Java: here.