Sunday, August 2, 2009

What is the use of unsigned and signed integer in c programming?

In computers, all data is a certain number of bits (1s and 0s) and there are 8 bits in a byte. An integer is 32 bits (on most CPUs). With a signed integer the first bit represents whether or not the integer is positive or negative. With a signed integer the range of values are -2,147,483,648 to +2,147,483,647. However, lets say we were making a computer game and we wanted to keep score but we want to account for the fact that our player is a very good gamer and could get a very high score. Well, since our score will never be negative (only zero), we can use an unsigned integer. What this will do is create a number that is only positive (like absolute value) and use that extra bit (previously used to describe whether or not a number is positive or negative) as part of the number. Thus are new range of integers is 0 to 4,294,967,295.


No comments:

Post a Comment