/********************************************************************** biggestI
ID: 3746127 • Letter: #
Question
/**********************************************************************
biggestInt.c
This file is a program that finds some information about a system's
C data types. It does this by initializing a variable of each type
to 1 and counting or shifting until that type wraps around. When
it discovers the value, it prints it to the screen.
**********************************************************************/
/* include I/O routines. */
#include <stdio.h>
int main() {
/* declare the variables */
unsigned int next_uint, biggest_uint;
long long next_long, biggest_long;
int negative_int, double_negative_int;
/**********************************************************************
TEST UNSIGNED INT DATA TYPE
Here we shift the value 1 left until it becomes zero. This way,
the value just before the shift that zeros the variable has 1 as
its MSB and 0 for all other bits. (i.e., 0x80000...).
**********************************************************************/
/* Start the variable of interest at 1 */
next_uint=1;
/* Test the next value of the varible. If it is still greater than
zero, we haven't wrapped around yet so shift it right and try
again. */
while( next_uint > 0 ) {
/* remember the current value */
biggest_uint = next_uint;
/* shift to find the next value */
next_uint=next_uint<<1;
}
/* Print the final value */
printf(" The most significant bit of an unsigned int is %u ",
biggest_uint);
/**********************************************************************
TEST SIGNED LONG DATA TYPE
Here we shift the value 1 left and OR in a 1 in the LSB until it
becomes negative. This way the value just before the negative
value is the largest positive value. (i.e., 0x7FFFFF...). NOTE:
Counting to this value from 1 would take a long time!
**********************************************************************/
/* Start the variable of interest at 1 */
next_long=1;
/* Test the next value of the varible. If it is still greater than
zero, we haven't wrapped around yet so shift it left and try
again. */
while( next_long > 0 ) {
/* remember the current value */
biggest_long = next_long;
/* shift a 1 into the LSB to find the next value */
next_long=next_long<<1;
next_long=next_long | 1;
}
/* Print the largest value */
printf(" The largest signed long is %lld ", biggest_long);
/**********************************************************************
TEST SIGNED INT DATA TYPE
Here we shift the value 1 left until it becomes negative (i.e.,
0x80000...). This way we can find the most negative representable
value.
**********************************************************************/
/* Start the variable of interest at 1 */
negative_int=1;
/* Test the next value of the varible. If it is still greater than
zero, we haven't reached the MSB yet so shift it left and try
again. */
while( negative_int > 0 ) {
/* shift to find the next value */
negative_int=negative_int<<1;
}
/* Print the largest value */
printf(" The largest negative signed int is %d ", negative_int);
/**********************************************************************
2's Complement number lines have one more negative number than they
do positive numbers. That is, the most negative number does not
have a positive counter part. If you attempt to negate the most
negative number, what do you get?
**********************************************************************/
/* Invert the most negative int */
double_negative_int = -negative_int;
/* Print the answer */
printf(" If you negate the most negative signed int you get %d ",
double_negative_int);
return 0;
}
(Exercise) Number Representation Download biggestlnt.c file from the assignment page. The code works by using 1 and shifting the number until it wraps around or overflows. It exploits the fact C does not really check for these conditions. It allows us to test the limits in a particular system and see the underlying number representation. You may wish to follow the exact steps the code is executing but it is not essential to answer the questions. Q7. First output gives the value of the most significant bit (MSB) of an unsigned int. What is the size (# of bits) of an unsigned int? Q8. Second output gives the value of a long long int. What is the size of a long long? Q9. Third output shows the most negative signed int. Is the size of a signed int the same as an unsigned int? Q10. Fourth line shows the value of the most negative value's negation, ie - most_negative_number. What is the output and the reason for the value to be this way? (Hint: remember the property of 2's complement representation)Explanation / Answer
Output:
The most significant bit of an unsigned int is 2147483648
The largest signed long is 9223372036854775807
The largest negative signed int is -2147483648
If you negate the most negative signed int you get -2147483648
1. To get the size of unsigned int, we take the log of the value of the most significatn bit. Hence, # of bits = log2(2147483648) + 1 = 32
2. Once again, we take the log of the largest value, so the size of long long is log(9223372036854775807) + 1 = 64
3. Since the largest negative is the negative of the largest bit of unsigned int, the value of the signed int is indeed the same as that of the unsigned int.
4. Because of the two's complement representation, the negation of the largest negative int does not fit into the size of the int, and so it overflows, hence giving us the same value.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.