Monday, July 27, 2009

In c++,an unsigned integer can be twice as large as the signed int.explain how.?

Let us assume that the machine is 32bit wide. For a signed integer, when 32bits are allocated, 1 bit has to be used for storing the sign information. So the actual number can onlly be 31bit wide.





For an unsigned integer, the whole 32bits can be used.





So the range of signed int will be -(2^31) to (2^31)-1 and the range of unsigned int will be 0 to (2^32)-1.





For ease of calculation, drop the '-1' part. Now, obviously, 2^31 is two times as big as 2^32. So unsigned int is twice as large as signed int.





I hope this is what you meant by 'large'.

In c++,an unsigned integer can be twice as large as the signed int.explain how.?
The sign needs one bit, and the rest are used for the integer. Unsigned uses this bit also as part of the integer. And as one extra bit available is equivalent to doubling the capacity, the unsigned integer has a maximum limit double that of a signed integer. Anyway, the total range available (0 to max in case of unsigned and -min to +max in case of signed) will still remain the same.
Reply:Count up the bits (ones and zeros). How many bits does it take to represent the sign? How could the fact there is an extra bit to use affect the value of a stored number?
Reply:Every int takesup 2 byts in memory...........signed integers range frm(-32768 to 32767)


Unsigned integers are NEVER negetive.Thats why their range is(0 to 32767+32768).
Reply:In c++ an integer is dependent on the machine running it. For this question I will consider a 8 bit machine for ease.





For a 8 bit integer the range of numbers that can be expressed are 00000000 through 11111111. Hence in decimal number systems there are enough corresponding binary 8 bit symbols to represent positive numbers from 0 to 255. For a negative number the first bit is always 1 whereas for a positive number it is always 0. This brings down the combination of number of bits to 7 from 8 and hence we can represent the actual number itself from 0 to 127 only.





Hence in c++ and unsigned integer can represent twice the number the decimal integers than a signed one can.
Reply:The sign consumes one of the 32 bits. A signed integer ranges from -(2^31 - 1) to + (2^31-1). An unsigned integer ranges from 0 to +(2^32-1).


No comments:

Post a Comment