Results 1 to 4 of 4
i wrote a program just trying to learn and bumped into somethin weird i take an unsigned short and gave it a value of 1 then took away 1 which ...
- 10-19-2007 #1Just Joined!
- Join Date
- Oct 2007
- Posts
- 9
confused about unsigned in gcc ubuntu
i wrote a program just trying to learn and bumped into somethin weird i take an unsigned short and gave it a value of 1 then took away 1 which made it 0 took away 1 again and it displayed its max value of 65535 which is correct just for measure i then gave it the value of 65535 and added 1 to make it 0
the same is not true for an unsigned int which confuses me i gave it the value of 1 took away 1 to make it 0 then took away 1 again and suprizingly it gives me the value of -1?
and to try to figure out why i gave it its max value which on my pc is 4294967295 and added 1 and it gave me the value of 0
i dont understand how an unsigned int can show the characteristics of a signed int when put below its boundries yet it acts as a unsigned int when put over its bounderies unless im overlooking something heres my code
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned short short_=1;short_--;
printf("Short=1--:\t\t%d\n",short_);
short_--;
printf("Short--:\t\t%d\n",short_);
short_=65535;short_++;
printf("Short=65535++:\t\t%d\n",short_);
unsigned int int_=1;int_--;
printf("Int=1--:\t\t%d\n",int_);
int_--;
printf("Int--:\t\t\t%d\n",int_);
int_=4294967295;int_++;
printf("Int4294967295++:\t%d\n",int_);
exit(0);
}
and my output:
fritch@fritch-laptop:~/Programming$ ./data_ranges
Short=1--: 0
Short--: 65535
Short=65535++: 0
Int=1--: 0
Int--: -1
Int4294967295++: 0
fritch@fritch-laptop:~/Programming$
- 10-19-2007 #2Just Joined!
- Join Date
- Oct 2007
- Posts
- 13
you are using %d in printf("Short=1--:\t\t%d\n",short_); that is the problem. Use %u because is the correct modifier for unsigned int. What is happening is that when u are subtracting 1 from 0 then it becomes 1111 1111 1111 1111 with another 1 as carry over which overflows, but by %d you are telling printf that it is actually a signed int. So, it interprets the no. as 111 1111 1111 1111 with the 1 at MSB as the sign bit. Now since negative nos. are stored in 2's complement form so it finds the 2's complement of the no. again to get the actual no., which evaluates to as
=> ~111 1111 1111 1111 ->000 0000 0000 0000 = A
=>A-1
=>000 0000 000 0001
=>Now because of the 1 in the MSB this is interpreted as -1.
BTW when I compiled I indeed got 65535 on my gcc 4.1.2 (Ubuntu 4.1.2-0ubuntu4). Maybe somehow printf was finding out that it was indeed unsinged not signed. I had to forcibly cast it into signed to simulate your case. Anyway checkout the manpage of printf for full info by typing man 3 printf in bash.
- 10-19-2007 #3Just Joined!
- Join Date
- Oct 2007
- Posts
- 9
yah thanks i get the unsigned a little better but it still doesnt make sense why unsigned shorts are treated different then unsigned int's but maybe i just need some sleep ill read that man page and look into it more after a nap if the prob doesnt keep me up lolz
- 10-19-2007 #4Just Joined!
- Join Date
- Oct 2007
- Posts
- 13


Reply With Quote
