unsigned integer behaver | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

unsigned integer behaver

Still new here trying to write a programm with an Input 1-4, everything else is staying in the while loop now instead of writing while(PlcHold>5||PlcHold<0) i tried to use an unsigned integer unsigned int PlcHold; while(PlcHold>5) 'cause i tought an -3 Input would be +3, a positive number but instead i get high numbers as if the PlcHold is not declared before its use So i'm asking, how exactly does the "unsigned int" behave?

18th Feb 2017, 9:31 PM
Vodolle
Vodolle - avatar
5 Answers
+ 2
I think I got the concept, seems really useful in an enviroment with positive numbers only, saving some memory instead of using a long integer atleast thats how I understand it with the knowledgelevel I reached now Thank you really much, made thinks more clear for me ^^
18th Feb 2017, 10:20 PM
Vodolle
Vodolle - avatar
+ 1
i think its because an unsigned integer only stores positive numbers a negative input is = no input am I right?
18th Feb 2017, 9:37 PM
Vodolle
Vodolle - avatar
+ 1
This is because you're getting an integer overflow wrapping. Basically when you go beyond the upper or lower bounds of the numeric range (unsigned int in this case) it will wrap around to the other end of that types range. So lets use the range of 0-100 as an example. In this case a -3 is beyond the lower bounds of the confined range. So it will wrap around to the upper end of the range and you would get a answer of 98. -1 -> 100, -2 -> 99, -3 -> 98 etc. It works similar going the other direction. Where 101 -> 0, 102 -> 1, etc. A 2 byte unsigned int has a range of 0 to 65,535 -3 -> 65,533 while a 4 byte unsigned int has a range of 0 to 4,294,967,295 -3 -> 4,294,967,293 <- this is what you're seeing if you used the code playground since an unsigned int here is 4 bytes.
18th Feb 2017, 9:39 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@Moksh You're close, but you are off by 1 (-3 would be 65533) . Also the OP was talking about an unsigned int, not just a regular int, but concept is the same either way. ;-)
18th Feb 2017, 9:59 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
This helps me alot, thanks ^^ but I'm still a little confused about unsigned int compared to int I mean I cant see the use in unsigned integer All I read is, it stores only positive numbers The cycle explanation helps me In understanding integers memory side more but not exactly the unsigned part still thanks to the explanations, answered a question I had long carried with me
18th Feb 2017, 10:00 PM
Vodolle
Vodolle - avatar