There are important differences in signed and unsigned integers.

If you are trying to land your next job, make sure to understand signed and unsigned integers as there are many tricky questions related to or derived from signed or unsigned integers.

Before we examine a tricky interview question, let us define the definition of a **signed** integer and an **unsigned** integer.

A **signed** integer is one that has a *signed* bit in the most significant bit (MSB) to denote if the integer is positive or negative. If the MSB is 0, then remaining bits denote a positive integer. On the other hand, if the MSB is 1, then the remaining bits denote a negative integer.

An **unsigned** integer is one that uses all bits to represent the integer.

Regardless of type, signed or unsigned, the range of the integer is always the same. However, the positive range of the signed integer is effectively half of that of the equivalent unsigned integer because one bit is designated as the sign bit.

As an example, we will use a 32 bit integer to understand the effective range of an unsigned integer and a signed integer. An unsigned integer is 2^32 in range. So, the valid range is 0 to 2^32 or 4294967296. As for the signed integer, the range is -2^31 to 2^31 - 1; in other words, -2147483648 to 2147483647. Both have the same range.

This is a common interview question to gauge the candidate's awareness (ability to notice the unsigned integer) and knowledge (understanding of the unsigned integer limits). See how well you can figure out a solution, it is really simple.

void function(){ //assume short int is 1 byte in size for( unsigned short int i = 256; i >= 0; i-- ){ printf( "%u\n", i ); } }

Vantasy World Copyright 2011 - 2023. Vantasy World is a project developed by Vantasy Online. Privacy Policy.