Lannie

Is there a way for the value of an int to rollover to 0 after reaching 2147483648
How does unsigned int work I tried tracking down its values and it turns out that it's pretty much the same with int.
Am I right or did I do something wrong


Re: Visual C++ General Rollover data type Values after reaching max

Pintu Shukla

Lannie wrote:
Is there a way for the value of an int to rollover to 0 after reaching 2147483648
How does unsigned int work I tried tracking down its values and it turns out that it's pretty much the same with int.
Am I right or did I do something wrong



take a Normal Example the range of int is -2,147,483,648 to 2,147,483,647 .which means when it will reach upto 2,147,483,647 after that it will start printing negative value like -2,147,483,648 ,-2,147,483,647 and so on.
have a look on this code
Code Block

int iVal = 2147483646;
for(int i=0;i<4;i++)
std::cout<<iVal++<<std::endl;


output of the above code will be

2147483646
2147483647
-2147483648
-2147483647

So i think now it's clear to you.So you have to put a Condition here for rollover that is either your int value reached upto 2147483648 make it 0 or second if your int value comes negative or less then 0 make it zero .That all you have to do
Thanx





Re: Visual C++ General Rollover data type Values after reaching max

Lannie

Thanks for that.

How about unsigned int Does Visual C++ support it Is its value supposed to be 0 - 4294967296




Re: Visual C++ General Rollover data type Values after reaching max

Pintu Shukla

you always Welcome .Regarding next question .Will suggest you to gave a try by yourself . So easily you can understand what is upported or what is not you also can use MSDN or Google if still Problem Please let us Know.
Thankyou





Re: Visual C++ General Rollover data type Values after reaching max

einaros

Lannie wrote:
Thanks for that.

How about unsigned int Does Visual C++ support it Is its value supposed to be 0 - 4294967296


Signed integers use 31 bits for the number, and one for the sign. That leaves you with a maximum number of 2^31 = 2147483648, positive and negative. Unsigned integers, on the other hand, use all 32 bits for the number, so the total is 4294967296. Since there's no sign bit, the lowest possible value is 0.

If you want to cap a number between 0 and 2147483648, you should use an unsigned int and compare it against 2^31 + 1.