You could use a while loop with a break statement if the numbers before and after multiplication and division aren't equal. Here is an example for int:
int i = 1,tmp=0,count=0;
while (true){
tmp = i;
i = i*2;
i = i / 2;
if (tmp != i)
{
cout <<"The number n multiplied with 2 can't be saved in
an integer type:"<<endl;
cout <<"n = "<<tmp<<" (2^"<<count<<") ---> 2xn = "<<tmp*2;
break;
}
i=i*2;
count++;
}
tmp -> value of i before multiplication and division;
count -> counts how many times i can be multiplied by 2
my results after this code are:
i = 10737741824 = 2^count (count = 30) = 2^30
2^31 would be 2147483648 , but the max number on my system that can be saved in an signed int is 2147483647 so the loop breaks here