some about data types

Technical questions regarding the XTC tools and programming with XMOS.
jagspaul
Experienced Member
Posts: 117
Joined: Tue Oct 18, 2011 3:28 pm

some about data types

Post by jagspaul »

what is the bit size of unsigned int.

as per my thinking as xmos is a 32 bit processor so size of unsigned int is 32 bit only.

what is unsigned and how it is differ with unsigned int?
in which cases unsigned is used?


jags


User avatar
Ross
XCore Expert
Posts: 966
Joined: Thu Dec 10, 2009 9:20 pm
Location: Bristol, UK

Post by Ross »

jagspaul wrote:what is the bit size of unsigned int.

as per my thinking as xmos is a 32 bit processor so size of unsigned int is 32 bit only.
Correct, 32bit

jagspaul wrote: what is unsigned and how it is differ with unsigned int?
in which cases unsigned is used?
jags
They are the same..
jagspaul
Experienced Member
Posts: 117
Joined: Tue Oct 18, 2011 3:28 pm

Post by jagspaul »

Thanks ross.


jags
MaxFlashrom
Experienced Member
Posts: 82
Joined: Fri Nov 05, 2010 2:59 pm

Post by MaxFlashrom »

In C/XC, when the internal representation of an int is 32 bits, the difference between a signed int and an unsigned int is one of interpretation.

An unsigned int can only represent positive integers, in the range 0 to ((2^32)-1)
0 to 4,294,967,296

A signed int can represent signed integers in the range -(2^31) to ((2^31)-1)
-2,147,483,648 to 2,147,483,647

Notice that the signed integer range is not symmetrical about 0. This is a result of the internal 2's complement representation.

Incidentally, C does not mandate a specific internal representation like 2's complement: some early computers used 1's complement(a Boolean NOT)). 2's complement is by far the most common representation used these days.

Wikipedia has an interesting article: http://en.wikipedia.org/wiki/Signed_num ... sentations

An unsigned int can never represent a negative number; subtracting 1 from 0 gives the largest positive value. This can lead to unexpected results. Consider the following code:

Code: Select all

unsigned int bitNum;
unsigned int mask;

for(bitNum = 31; bitNum >= 0; bitNum--)
{
	mask = 1 << (unsigned int)bitNum;
{
This code loops forever as the bit counter is never less than zero.
Using a signed int for bitNum would make this code work.
Care must be taken when comparing signed and unsigned values as a result of type promotion rules. In particular, sometimes explicitly suffixing a constant with U is necessary.

Regards
Max
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

MaxFlashrom wrote:

Code: Select all

unsigned int bitNum;
unsigned int mask;

for(bitNum = 31; bitNum >= 0; bitNum--)
{
	mask = 1 << (unsigned int)bitNum;
{
This code loops forever as the bit counter is never less than zero.
It might on some implementation, but it is allowed to do anything when bitNum
has reached UINT_MAX, since the shift gives undefined behaviour then. Terminating
the loop is perfectly fine :-)
Using a signed int for bitNum would make this code work.
Yeah, or use bitNum < 32 .
Care must be taken when comparing signed and unsigned values as a result of type promotion rules. In particular, sometimes explicitly suffixing a constant with U is necessary.
In a comparison? I have a hard time seeing where that would change anything,
got an example?

Cheers,


Segher
MaxFlashrom
Experienced Member
Posts: 82
Joined: Fri Nov 05, 2010 2:59 pm

Post by MaxFlashrom »

segher wrote:
MaxFlashrom wrote: Care must be taken when comparing signed and unsigned values as a result of type promotion rules. In particular, sometimes explicitly suffixing a constant with U is necessary.
In a comparison? I have a hard time seeing where that would change anything,
got an example?

Cheers,


Segher
Signed and unsigned comparisons are performed differently. If mixing signed and unsigned arguments(probably by mistake) an unsigned comparison is performed. Similarly for other binary(diadic) operations.

Unsuffixed decimal literals are treated as signed unless they wont fit into a long int, in which case they are treated as unsigned long int.
Unsuffixed hex literals are treated as int unless they don't fit, then unsigned int if they fit. Then (back to signed!) long int or unsigned long int if they won't fit.

(See K&R2 A.2.5.1 Integer Constants)

The moral of the lesson is there's no harm suffixing u and lu to your unsigned constants to make everything clear.

Consider the following code.

Code: Select all

	signed int sval = -10;
	unsigned int uval= 10;


	if (sval < 0xffff) { // 0xffff treated as int. Performs signed comparison using lss
		sval = 0;
	}

	if (sval < 0xffffu) { // 0xffff treated as unsigned int. sval is cast to unsigned. Performs unsigned comparison using lsu
		sval = 0;
	}

	if (sval < 0xfffffffb) { // 0xfffffffb treated as unsigned int, not as -5. sval cast to unsigned int. Performs unsigned comparison using lsu
		sval = 0;
	}

	if (sval < 0xfffffffbu) { // 0xfffffffb treated as unsigned int. sval cast to unsigned. Performs unsigned comparison using lsu
		sval = 0;
	}

	if (uval < 0xffff) { // 0xffff treated as int but then cast to unsigned int. Performs unsigned comparison using lsu
		uval = 0;
	}

	if (uval < sval) { // sval cast to unsigned int. Performs unsigned comparison using lsu
		uval = 0;
	}
Regards, Max.
User avatar
Ross
XCore Expert
Posts: 966
Joined: Thu Dec 10, 2009 9:20 pm
Location: Bristol, UK

Post by Ross »

This has gone a bit off topic now, the original question was

Code: Select all

unsigned
vs

Code: Select all

unsigned int
Lets not confuse anyone please! :)