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
some about data types
-
- Experienced Member
- Posts: 117
- Joined: Tue Oct 18, 2011 3:28 pm
-
- XCore Expert
- Posts: 968
- Joined: Thu Dec 10, 2009 9:20 pm
- Location: Bristol, UK
Correct, 32bitjagspaul 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.
They are the same..jagspaul wrote: what is unsigned and how it is differ with unsigned int?
in which cases unsigned is used?
jags
-
- Experienced Member
- Posts: 117
- Joined: Tue Oct 18, 2011 3:28 pm
Thanks ross.
jags
jags
-
- Experienced Member
- Posts: 82
- Joined: Fri Nov 05, 2010 2:59 pm
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:
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
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;
{
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
-
- XCore Expert
- Posts: 844
- Joined: Sun Jul 11, 2010 1:31 am
It might on some implementation, but it is allowed to do anything when bitNumMaxFlashrom wrote:This code loops forever as the bit counter is never less than zero.Code: Select all
unsigned int bitNum; unsigned int mask; for(bitNum = 31; bitNum >= 0; bitNum--) { mask = 1 << (unsigned int)bitNum; {
has reached UINT_MAX, since the shift gives undefined behaviour then. Terminating
the loop is perfectly fine :-)
Yeah, or use bitNum < 32 .Using a signed int for bitNum would make this code work.
In a comparison? I have a hard time seeing where that would change anything,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.
got an example?
Cheers,
Segher
-
- Experienced Member
- Posts: 82
- Joined: Fri Nov 05, 2010 2:59 pm
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.segher wrote:In a comparison? I have a hard time seeing where that would change anything,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.
got an example?
Cheers,
Segher
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;
}
-
- XCore Expert
- Posts: 968
- Joined: Thu Dec 10, 2009 9:20 pm
- Location: Bristol, UK
This has gone a bit off topic now, the original question was vs
Lets not confuse anyone please! :)
Code: Select all
unsigned
Code: Select all
unsigned int