# Finding Pos/Neg Integers Unsigned Can Hold

• 01-25-2013
trial123
Finding Pos/Neg Integers Unsigned Can Hold
Consider a new data type, the mikesint, which can hold 9 bits.
(a) What is the largest integer that an unsigned mikesint can hold?
(b) What is the largest positive integer that a signed mikesint can hold?
(c) What is the largest negative integer that a signed mikesint can hold?

--->
• 01-25-2013
nonpuz
Quote:

Originally Posted by trial123
Consider a new data type, the mikesint, which can hold 9 bits.
(a) What is the largest integer that an unsigned mikesint can hold?
(b) What is the largest positive integer that a signed mikesint can hold?
(c) What is the largest negative integer that a signed mikesint can hold?

--->

It's quite simple..think about what number would be if all 9 bits were 1 (on)? Just count up binary, thats the largest 'unsigned' number that 'mikesint' can hold.

Now consider that a signed type takes (at least) 1 bit to store the 'signedness' of the number and that leaves you 8 bits to store the number (again all set to 1 gives the largest number, negative and positive). There is a second piece to consider however, and that is WHICH bit to use for the signedness. If you use the 'least significant bit' or the 'rightmost' ie the 1 switch in binary. Then that is going to dramatically increase the maximum number you could store in a mikesint, as compared to if you used the 'most significant bit' or leftmost bit to store the signedness (which would be a bad idea for this reason).

• 01-25-2013
trial123
Quote:

It's quite simple..think about what number would be if all 9 bits were 1 (on)? Just count up binary, thats the largest 'unsigned' number that 'mikesint' can hold.
So, that would be... 2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6 + 2^7 + 2^8 + 2^9 = 1023?
• 01-25-2013
iMalc
You're adding ten terms there. Nine bits cant hold that.
• 01-25-2013
nonpuz
(Posted by iMalc)
• 01-25-2013
megafiddle
8 bits can code 256 different values - 2^8 = 256
Unsigned value range is 0 - 255

9 bits can code 512 different values - 2^9 = 512
Unsigned value range is 0 - 511

10 bits can code 1024 different values - 2^10 = 1024
Unsigned value range is 0 - 1023

Since one of the codes is zero, the maximum unsigned value for N bits is (2^N)-1.

If you use one of the bits for the sign, you lose one bit for the value magnitude.
So a signed 8 bit value has the same maximum value as an unsigned 7 bit value, etc.
Therefore the maximum positive signed value is ( 2^(N-1) ) -1.

For a signed value, the codes '0', through the maximum positive value, will use up 1/2 of the possible codes for N bits,
( one bit for sign, N-1 bits for magnitude ).
This leaves the remaing half of possible values to code the negative values. So the maximum negative value is -( 2^(N-1) ).